Game Development Community

Call" and "Eval

by Eric Robinson · in Torque Game Builder · 03/14/2007 (8:25 pm) · 4 replies

How exactly do these functions work?

I'm trying to create a dynamic callback in script using these such that the programmer would send in an appropriately command a-la:

%command = "$myObject.updateScore(5)"

I would like to allow the programmer to send in a predefined string to get executed upon completion. Anyway to do this?

#1
03/14/2007 (10:01 pm)
@Eric, I do just that in my Blog Reader demo, found here
#2
03/14/2007 (10:28 pm)
@David: I assume that you mean the following block of code:
function performAction(%object, %action)
{
   if(!(%action $= ""))
   {
      echo("COMMAND WORD COUNT: " @ getWordCount(%action));
      if(getWordCount(%action) > 2)
      {
         %command = getWord(%action, 0) @ "(" @ getWord(%action, 1) @ ", \"" @ getWords(%action, 2, getWordCount(%action) -1) @ "\");";
      } else if(getWordCount(%action) > 1) { 
         %command = getWord(%action, 0) @ "(" @ getWord(%action, 1) @ ");";
      } else {
         %command = getWord(%action, 0) @ "();";
      }

      if(isObject(%object))
      {
         %command = %object.getId() @ "." @ %command;
      } 
      echo("COMMAND: " @ %command);
      eval(%command);
   } else {
      echo("EMPTY ACTION: " @ %this.Action);
   }
}

Is that right? Is "Call()" used similarly?

Oh, nice! I just noticed that you do method calls by using the object's ID. Is that how you're supposed to do it? Does calling by name simply not work?
#3
03/14/2007 (10:46 pm)
It does work with names! Perfect! It appears that I was forgetting the ";" at the end of my statements before.

Thanks a ton!

You can even do nested evals:
eval("eval(\"echo(\\"this is a string\\");\";");
That will print "This is a string" to the console. The back slashes get to be a pain in the butt but it's doable. Awesome, thanks!
#4
03/14/2007 (11:23 pm)
@Eric, I believe call and eval are used differently -- eval is for evaluating entire script parts, where as call is designed to call a function and optionally pass in a list of parameters to it -- I opted for eval in my example above, just because I felt like it ... I'm more familar with 'eval' style syntax then I am with call syntax, so it makes more sense to me and the codes more readable ... depending on your needs, call may be a cleaner solution for you though ...

And, I'm not 100% positive, but 'names' yes -- such as t2dSceneObject(ObjectsName) ... but %obj = new t2dSceneObject() { }; ... pretty sure '%obj' would be 'out of scope' in an eval call ... but not positive, haven't had a need to test it, honestly ;)