Game Development Community

executing quoted strings as script

by N.K. · in Torque Game Builder · 10/19/2009 (1:30 pm) · 4 replies

Hi,
I've noticed that the keyboard binding methods take a string holding the snippet of torquescript to execute when that particular key is pressed. Is there a function that allows this to be done in general.

For example can I generate a string holding a function name on the fly and then append semicolons at the end and have the piece of code executed?

Regards

#1
10/19/2009 (1:54 pm)
Yes you can with the function eval();

I use it for debug and log purposes.
#2
10/19/2009 (1:57 pm)
%string = GameObject @ "." @ %somethingElse @ "+=" @ %anotherVal @ ";";
eval(%string);

That's a dumb example, but generally, yes, you can build a procedural string in script and then evaluate that string using eval().

Edit: Ahhh! Beat to it!
#3
10/19/2009 (2:07 pm)
Thanks a lot.
By the way, any idea about how eval() fares in terms of performance since it is in effect performing the functions of a compiler at runtime.
#4
10/19/2009 (2:33 pm)
Necessarily slower than compiled script, depending on how complex the string you feed it.