Hello guys,
Since a few days ago i started with Unity. So you can say i’m pretty new to this, though i have a bit of experience scripting with PHP, and i think Javascript looks a lot like it from what i’ve seen now (this is also the first time i scripted with Javascript).
Now i have a little problem i can’t seem to figure out by myself. I am making a little game with a friend of mine, he is doing all the modeling work and i am doing all the scripting. What i am doing is making all kinds of functions, for example changeLevel, changeHealth etc, so these functions can be used later when we have real enemies etc in the game.
But for now i want to test it out, so i’ve built a little console window, in which developers or players can type in commands that will basically call functions and then do stuff. But i have no clue about how i can convert the commmands to the functions. Yes i know, it still sounds weard. I’ll explain more with a bit of code i have:
// Create the send button and send the input to the console window
if(GUILayout.Button("Send")) {
// Add the input to the console window
debugContents += returnedInput + "
";
// Empty the input textfield
returnedInput = “”;
}
As i said in the comments (i love comments in code!), this little piece of code makes a ‘Send’ button, and shows what you have typed in the console window. But i also want the code that is typed to be called. So for example, in the console, you type ‘changeLevel(2)’. I want this to actually execute this so it calls my function ‘changeLevel’ with the parameter ‘2’. But i have no idea how to do it.
Whatever the user types and sends is stored in a variable called ‘returnedInput’. With that i mean that if someone types in ‘changeLevel(2)’ and press the ‘Send’ button, then ‘returnedInput = “changeLevel(2)”’. I have simply tried it this way:
// Create the send button and send the input to the console window
if(GUILayout.Button("Send")) {
returnedInput;
// Add the input to the console window
debugContents += returnedInput + "
";
// Empty the input textfield
returnedInput = “”;
}
So as you can see, i just added ‘returnedInput;’ in the hope that it will execute it. But i get this error: ‘Expressions in statements must only be executed for their side-effects.’. And i don’t know what that means to be honest. I hope you guys understand what i mean, because i’m not too good in explaining it. But if you understand me, could you help me out? Because ‘returnedInput’ is a string, perhaps it should somehow be converted to use as a function?
Anyway, let me know if you have a solution for me, or ask me for more information. And when i finish this little console script, i could give you guys the whole script if someone wants it, when finished it should be very handy to test if all your functions work correctly.
Thanks in advance!
EDIT:
If you don’t know what i am trying to make, think about the console / cheat window in most games.
You need to use reflection to call functions by name, the functions need known signatures to work on things like iOS. What script do the functions exist on?
– whydoidoitI am sorry but i don't understand you. Maybe it's my bad English. What is reflection? And i don't want to use it on iOS, just webplayer and stand alone Windows executable. And the functions are currently in my script called 'debugConsole.js'. However, the functions that i want to be executed like changeLevel will eventually be in a different script, called userFunctions.js or something like that. But that can wait, i just want the string variable to actually call a function.
– PimBARFWell you aren't programming real Javascript and in Unity you use Reflection to find functions and to call them. The signature of the function needs to be known (e.g. what parameters it takes and what it returns). It's a pretty big subject.
– whydoidoitYou can use Unity's SendMessage function to call a method in another script, but it cannot return a value. It can however take a parameter which is a class on which you can set return values. I will post an answer in this vein as it's probably a lot easier to understand.
– whydoidoitLet me know which you would prefer.
– whydoidoit