- Home /
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-/
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!
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.
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.
the answer i linked you to is aggresively avoiding the use of eval, the whole point is to not use eval
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);
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?
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.
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.
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!
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
Follow this Question
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