I am having a lot of trouble on my code, I am trying to make some sort of parser. Basically I am trying to get data from a JSON file, and use that data to call methods from my code.
Here's a basic example of my JSON file { "story": { "Talk": [ "me", 1, 1 ] } }
Now I have a class called DialogueSystem, it contains a function called Talk with three parameters, string, int, int.
I am using SimpleJSON to get my JSON data, but I am guessing it's similar to other JSON parsers.
Also, I have other functions which have different parameters which is why I am forced to use reflection
Anyways, here's the code that gets the json data, and tries to use Reflection to call the Talk method.
// Gets the JSON and parses it
JSONNode Node = JSONNode.Parse(jsonFile());
var method = sys.GetType().GetMethod(""); // Reflection stuff
foreach (JSONNode item in Node["story"].Keys) // the Keys just gives me every key that's in the story node/key
{
List<object> parameters = new List<object>(); // List containing the parameters, to be used when invoking the method
for (int i = 0; i < Node["story"][item].Count; i++)
{
//This part tests if it's a string or int and adds it as such
string data = Node["story"][item][i];
int n;
bool isNum = int.TryParse(data, out n);
if (isNum)
{
parameters.Add(n);
}
else
{
parameters.Add(data);
}
}
// Invoke the method using it's method name and the method parameters
method.Invoke(item, parameters.ToArray());
}
Btw, my talk class just prints a text based on the given input.
For some reason, I get this error NullReferenceException: Object reference not set to an instance of an object Dialogue.StoryTeller.ReadStory (Dialogue.DialogueSystem sys) (at Assets/Dialogue System/Scripts/StoryTeller.cs:57)
If you have any idea how to fix it, or maybe make it better, that would be awesome!
Thanks!