Game Development Community

Eval problem

by Richard_H · in Torque Game Engine · 09/12/2006 (2:32 pm) · 4 replies

When using the eval("code") function the code executes but the varibale I give it won't.
For example:
echo($WEAPONS[%x] @ "::evaluateWeapon(" @ %dist @ ");");
%score = eval($WEAPONS[%x] @ "::evaluateWeapon(" @ %dist @ ");");

somewhere in Crossbow.cs
Crossbow::evaluateWeapon(%this,%dist)
{
echo("Started with a dist of " @ %dist);
}
echos:
Crossbow::evaluateWeapon(13.45);
Started with a dist of

Does anyone know why?

#1
09/12/2006 (3:04 pm)
Don't put the eval statement in the %score = line.

Should be something like:

%score = (string to eval);

eval(%score);

Also, when you call a namespace based function like Crossbow::evaluateWeapon manually like that, you must explicitly provide the first argument to be the object id of the object to call it upon, such as:

%score = $WEAPONS[%x] @ "::evaluateWeapon( %this, " @ %dist @ ");";
eval(%score);

Note that I typed this in manually, you will need to play with it to get the %score string absolutely perfect.
#2
09/12/2006 (3:36 pm)
Well, I want %score to contain the value returned by the statment in the eval, but I think you figured out my problem with the %this.
Thx :)
#3
09/12/2006 (7:52 pm)
Ah, didn't quite catch that from your problem statement, sorry. Had never seen it used that way so wasn't sure the TS parser would handle it :)
#4
09/12/2006 (8:49 pm)
Yep;
you can do stuff like this:

function gimmeAnEight()
{
%statement = "%foo = 4 + 4;";
eval(%statement);
return %foo;
}

and get 8.