Thats not really possible with C#. Your C# scripts are compiled and executed, C# isn’t an interpreted language like PHP, so you can’t really just ‘execute a string’.
Why not just make a mini-language for the functionality you’ll need at runtime?
“move 3;dir (-1,0,1);”
You can split the number of commands by a delimiter like ; and then split the command into arguments with another delimiter. I used spaces in that example. Have a class / huge function to execute certain operations when it sees certain strings.
if(arg[0].Equals("move"))
{
gameObject.transform.position += blah(arg[1]);// your function to convert a string into a vector
}
Is this something that you’ll need a huge amount of flexibility in? Or is there only going to be a small number (20 or so) of possibilities? If it’s the latter, I’d consider using a dictionary of lambdas.
What I’d like to do is have a function that checks for certain similarities between game objects in a certain group. But I want the function to be able to check a specified variable in a certain script and return the ones that have a certain value for that variable. So in the function call, I want to specify where the variable is (like which script), and then when a variable name is passed in, locate that variable in the earlier specified script. I think Oana’s idea for using reflections could work for this, but if you have a good idea to solve this in a different way, I’d love to hear it.
That’s not really what I need right now, but I really like that idea for other parts of my code. I don’t really have much experience with working with strings in code, though, so I’m not exactly sure how to do this. How do you get a string divided into a String[ ] or array with each word in a different slot? (like how you used ‘arg[×]’)
Just look up the syntax for split because my memory of it is unreliable due to the varying syntax between languages.
String input = "walk 5;run -5;crawl 5";
String[] commands = input.split(";");
for(int i = 0;i < commands.length;i++)
{
String[] args = commands.split(" ");
if(args[0].Equals("walk"))
{
doWalkFunction(int.parse(args[1]));
}
if(args[0].Equals("run"))
{
doRunCode(int.parse(args[1]));
}
/*etc etc check args[0] for the function you want to call and every following index is a variable to pass*/
}
Strings aren’t the fastest things to work with, so I used the system just to read changes from the armor & equipment to set up properties of a spell rather than using that system to determine spell properties per cast. It can be used for anything
That makes a lot of sense now. Thanks for spending your time to explain that to me! I’ll definitely use that for something in the future; maybe a way to call a function in-game for bug testing (or they could be cheat codes).
What’s great about the way it’s set up is that the order doesn’t matter. Since it checks for every possible argument 0, you can have walk and then run or run and then walk or whatever. It’s definitely a great idea for cheat codes