- Home /
Access function from variable (sounds weard, i'll explain)
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 + "\n";
// 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 + "\n";
// 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?
I am sorry but i don't understand you. $$anonymous$$aybe 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.
Well you aren't program$$anonymous$$g 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.
You can use Unity's Send$$anonymous$$essage 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.
Answer by whydoidoit · Apr 19, 2013 at 10:12 PM
So in Unity a way of being able to call functions by name is by using SendMessage and it's cousins BroadcastMessage (which includes all objects beneath the one targeted) and SendMessageUpwards which sends it to parents.
SendMessage cannot return a value, but it can take a parameter. If that parameter is a class it can have its members updated, which can act like a return value.
So:
someGameObject.SendMessage("DoThis");
Will call the method DoThis() on every script attached to the GameObject that has such a function.
You can pass a single parameter to the function like this:
someGameObject.SendMessage("DoThis", 6);
Which passes an integer to the function
function DoThis(var parameter : int)
{
}
Now to get a return value you need to define a class and return the value inside it:
class ParameterClass
{
var input : String;
var output : int;
}
Perhaps a function on your script is now:
function CountCharacters(var parameter : ParameterClass)
{
parameter.output = parameter.input.Length;
}
And you do this:
var p = new ParameterClass();
p.input = "This is a string";
someGameObject.SendMessage("CountCharacters", p);
print(p.output);
Thanks a lot, i'm going to test it out right now. Good to know this stuff, i can use it for more things than my current problem! I'll let you know if it works.
Yes, it totally works! I only had to figure out how to change a string to int (because TextField only uses strings, and i needed a int to be send as parameter).