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
0
Question by MountDoomTeam · May 10, 2013 at 09:57 AM · guitextwebplayerruneval

How to convert a string into a function in JS?

I want to write a function that is then called by the program 1000ds times to make your own mesh world for users to roam in.

eval() is great, i have used it to print maths and texts, and also to call a function.

for my game though, i dont need to actively call an action, i need users to write abit of maths, that will then become a function, that the program will then call many times.

i.e. .JS is function:

 function equation(x:float,y:float,z:float):float
 {
    return (x*x+y*y+z*z-1);
 }

users can replace / rewrite above function so that:

GUI Text:

 function equation(x:float,y:float,z:float):float
 {
    return (Mathf.Sin (x)*Mathf.Cos (y)+z*z-5);
 }

is it possible? it's abit confusing arg 8-/

Comment
Add comment · Show 1
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 MountDoomTeam · May 10, 2013 at 10:26 AM 0
Share

at the moment I am calling the same eval() thousands of times, and it's twice as slow as calling the maths from a function, so if there is anyway of calling eval() just once to write a function I would love to know about it!

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Loius · May 11, 2013 at 04:23 AM

If you use Reflection you can get an actual function from its name; depending on how much you trust your user you may need to be creative and, for instance, only allow methods from Mathf to be called.

See the answer here http://answers.unity3d.com/questions/447830/calling-non-applied-scripts.html for how to use a string to find and call a function.

Comment
Add comment · Show 8 · 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 MountDoomTeam · May 11, 2013 at 03:31 PM 0
Share

this one causes an error:

 var st = "function test(){print(1234);}";
 var myfunction: Function=eval(st);

Assets/Plugins/CreateWorld.js(102,39): BCE0031: Language feature not implemented: BuiltinFunction: Custom.

avatar image Loius · May 11, 2013 at 06:22 PM 0
Share

the answer i linked you to is aggresively avoiding the use of eval, the whole point is to not use eval

avatar image Loius · May 11, 2013 at 06:24 PM 0
Share

something like this (meaning if you copy and paste this it will not work, it needs changes to fit in your code):

        var t : Type = Type.GetType("$$anonymous$$athf");
        var method : $$anonymous$$ethodInfo = t.Get$$anonymous$$ethod(userFunctionName, BindingFlags.Static | BindingFlags.Public);
 
        method.Invoke(null, arguments);
avatar image MountDoomTeam · May 11, 2013 at 07:10 PM 0
Share

ok sounds good ill make a study of it ;) thankyou so much! this line works: var t : System.Type = System.Type.GetType("$$anonymous$$athf");

and also i have to say $$anonymous$$ethodInfo doesnt exist in .JS so does that mean it doesnt support reflection at all?

avatar image Loius · May 12, 2013 at 04:26 PM 1
Share

Eval's the only way to let the user execute arbitrary code, but you never want the user to execute arbitrary code because someone will call Game.Win.

For something complex like that, without eval, you have to keep a list of functions the user wants to call and then call them in order. It's not a simple problem, but you can do it through Reflection or you can just have a list of options they can use and let them order them however they want.

Show more comments
avatar image
1

Answer by aldonaletto · May 11, 2013 at 03:56 PM

Maybe this article can help you - at first sight, it shows how to compile some code at runtime, and how to use the resulting assembly.

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 MountDoomTeam · May 11, 2013 at 07:07 PM 0
Share

Thankyou Aldo, that is pretty difficult, it seems there is a createmethod class with eval(), and i dont understand the article so much it is very advanced. perhaps i should not try to make a string to a method in JS and find an example in C that i could call from JS. oh Louis just put up some code for me to try i will be trying to understand that!

avatar image jerichaosymphony · May 30, 2013 at 10:21 PM 0
Share

Changed name! At last!:) probably I would have to write an external DLL, it's a shame because I have a great program and I wanted to do some competitions to see who can write the most crazy looking procedural 3d functions, but it goes over twice as slow from the web codes.

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

17 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Draw GUI Text if clicked out of webplayer 1 Answer

Detect Text in GUI; Print 1 Answer

GUI text font change 3 Answers

Limit on GUI Components? 0 Answers

Using "fonts" that are actually images for a multi-outline effect. 2 Answers


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