Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by PimBARF · Apr 19, 2013 at 09:45 PM · variablefunctionconsole

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.

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Apr 19, 2013 at 09:47 PM 0
Share

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?

avatar image PimBARF · Apr 19, 2013 at 09:55 PM 0
Share

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.

avatar image whydoidoit · Apr 19, 2013 at 09:59 PM 0
Share

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.

avatar image whydoidoit · Apr 19, 2013 at 10:01 PM 0
Share

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.

avatar image whydoidoit · Apr 19, 2013 at 10:01 PM 0
Share

Let me know which you would prefer.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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);
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PimBARF · Apr 19, 2013 at 10:24 PM 0
Share

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.

avatar image PimBARF · Apr 19, 2013 at 10:46 PM 0
Share

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).

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What is this C# code in javascript 0 Answers

Variable Doesn't Update in "Update" function 0 Answers

How can I storing the IEnumerator with parameters? 1 Answer

How do I reassign the result of a function back to the original arguments? 3 Answers

++ Changes variable to 121? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges