Hi all,
I’m using LitJSON to read a JSON file, and I was wondering if it’s possible to have the string stored contain a variable that will be replaced by a script. A quick and simple test says no, because I’ve formatted the value as I would a regular line in C#, with “stuff " + variable + " more stuff”
So, I’m wondering if it’s possible, and if it is, how is both the script and the JSON file structured?
If I understood correctly, you want to put code into a variable that gets executed later.
If that’s what you are asking, you’re looking at something like the “eval()” function from Javascript. In C#, I don’t think that is possible. You can’t even have scripts on asset bundles, probably for the same reason.
You can get away with it in Javascript, but it’s not recommended.
What are you trying to accomplish? Probably there is a better and safer way to work it out.
One example of how this will be used, players are able to construct buildings at certain locations in my game, and when they interact with a script, it’ll display text pulled from the JSON file. In other cases, it’ll inform the player of what the locations name is, and who controls it, there will also be cases where the current building will be displayed when the player is confirming if they want to deconstruct it.
I’m in the process of moving text from being hard-coded, like this
NPCspeech = new string[2];
responses = new string[2];
NPCspeech[0] = "This is a small plot. It is empty.";
NPCspeech[1] = "What would you like to do?";
responses[0] = "Build";
responses[1] = "Leave";
...
...
NPCspeech = new string[2];
responses = new string[3];
NPCspeech[0] = "This is a small plot. The current structure is: " + currentBuild + ".";
NPCspeech[1] = "What would you like to do?";
responses[0] = "Demolish";
responses[1] = "Manage Inventory";
responses[2] = "Leave";
to JSON
{ "step" : "emptyStart",
"NPCSpeech" : "This is a small plot. It is empty.*What would you like to do?",
"Response" : "Build*Leave",
"ConvoSteps" : ""
},
{ "step" : "builtStart",
"NPCSpeech" : "This is a small plot. The current structure is: " + currentBuild + ".*What would you like to do?",
"Response" : "Demolish*ManageInventory*Leave",
"ConvoSteps" : ""
}
- will be used as a delimiting character
I still don’t understand the need of storing a script inside a variable.
Aren’t you only showing text? Why can’t you just store the text or the location name on the JSON and have a script that displays it?
Because it’s going to be different and can change if the player constructs or deconstructs a building. In some cases, I can’t create an entry for every single scenario, such as ownership
Just make the part of the string that needs to be “dynamic” a weird value that would never occur normally in your game and then do a replacement on that string at runtime. Something like
"This is a small plot. The current structure is: !!STRUCTURE_NAME!!. What would you like to do?"
Replace !!STRUCTURE_NAME!! with whatever value you’re pulling in from wherever you’re pulling it at runtime.
I had considered that, but couldn’t think of how I’d do a replace right off the top of my head (Never looked into it before) and wondered if there would be a simpler way to do it, but if not then I know what I need to do.
You could also do it by making the speech an array of an object type (IGameText, let’s say) instead of a string, then having one that prints a static string and another that prints the value of a variable. However, I wouldn’t call that simpler; it’s probably significant overkill unless you want to do pretty elaborate behavior.
Personally, I’d probably use the C# 6 style:
“This is a small plot. The current structure is {structureName}. What would you like to do?”
And then have a general format function that reads those and fills in the values - either by reflection, or by a manually defined mapping.
After some fiddling, I think string.format might be the better option. It would seem that I don’t have to check for and replace certain keywords, and can add in as many arguments as needed, and not worry about missing anything or getting an error, so long as things are listed in the right order.
Thanks for your help, everyone!
What’s hard about doing the string replacement?
string jsonData; // populated from wherever you're getting it
string currentBuilding; // again, however you define this
const string structureVarName = "!!STRUCTURE_NAME!!"
string displayValue = jsonData.Replace(structureVarName, currentBuilding);
I’m not saying it’s hard, per se, just hoped for something that would require less scripting (which isn’t the case with either replace or format). It’s a perfectly fine and completely feasible solution.