Oh, that quirky eval() function!
by Dave Calabrese · in Torque Game Builder · 03/18/2005 (12:58 am) · 6 replies
So I've got my block of code to dynamically create a bunch of datablocks. The idea here is to be able to cache a large amount of graphics in a single datablock statement, so I don't have a script file with hundreds and hundreds of fxImageMapDatablock2D declarations.
The code is:
But it dosen't work!
%themeDirectory is:
But I keep getting an error that it couldn't find the graphic, even though it shows me the exact directory structure where the file is - but it says it cannot find it! So, I get the error of..
However, using the exact same code but without the eval (and of course also without the variable in the datablock declaration), it works just fine. It looks to me at first glance as the eval is making the '~' literal and trying to make it part of the directory structure.
Anyone got any ideas? Am I perhaps just asking WAY too much from the eval function? Or is there something I'm missing to be able to do it this way?
Thanks in advance,
-Dave C.
/Edit: Made code easier to read... was all running together.
The code is:
eval("datablock fxImageMapDatablock2D(" @ %theme @ "_" @ %type @ ")
{mode = full; textureName = %themeDirectory @ %Type;};"
);But it dosen't work!
%themeDirectory is:
"~/client/graphics/"
But I keep getting an error that it couldn't find the graphic, even though it shows me the exact directory structure where the file is - but it says it cannot find it! So, I get the error of..
Could not locate texture: ~/client/graphics/newTexture
However, using the exact same code but without the eval (and of course also without the variable in the datablock declaration), it works just fine. It looks to me at first glance as the eval is making the '~' literal and trying to make it part of the directory structure.
Anyone got any ideas? Am I perhaps just asking WAY too much from the eval function? Or is there something I'm missing to be able to do it this way?
Thanks in advance,
-Dave C.
/Edit: Made code easier to read... was all running together.
About the author
Recent Threads
#2
Is there any way that I can echo out what the eval is doing so I can see what the input is that it's evaluating? I tried using trace and echo but neither displayed anything but blank lines...
03/18/2005 (10:15 am)
I actually had already tried that. Instead of giving me any specific results, I just see:Syntax error in input.
Is there any way that I can echo out what the eval is doing so I can see what the input is that it's evaluating? I tried using trace and echo but neither displayed anything but blank lines...
#3
I was able to track things down with this. I don't think local variables are a problem, but things like the current directory get lost. If you are trying to load files you may need to use:
[code]
expandFileName(%themeDirectory @ %Type)
[code]
04/01/2005 (3:54 pm)
I've got the same problem with using eval. I've looked through the TorqueScript documentation I have and either it isn't listed or I'm not seeing it. (It doesn't help that the functions are never indexed or otherwise listed alphabetically.) What you can at least try for is:%eval = "datablock fxImageMapDatablock2D(" @ %theme @ "_" @ %type @ ") {mode = full; te
xtureName = " @ %themeDirectory @ %Type @ ";};";
echo(%eval);
eval(%eval);I was able to track things down with this. I don't think local variables are a problem, but things like the current directory get lost. If you are trying to load files you may need to use:
[code]
expandFileName(%themeDirectory @ %Type)
[code]
#4
function createDatablock(%name, %image)
{
echo("loading data into variable");
%val = "datablock fxImageMapDatablock2D(" @ %name @ "){mode = full; textureName = \"" @ expandFileName("~/client/images/" @ %image @ "") @ "\";};";
echo("running eval");
eval(%val);
%test = new fxStaticSprite2D() { scenegraph = t2dSceneGraph; };
%test.setPosition("1 1");
%test.setSize( "2 2" );
%test.setImageMap( %name );
}
[code]
this should work
04/01/2005 (4:47 pm)
[code]function createDatablock(%name, %image)
{
echo("loading data into variable");
%val = "datablock fxImageMapDatablock2D(" @ %name @ "){mode = full; textureName = \"" @ expandFileName("~/client/images/" @ %image @ "") @ "\";};";
echo("running eval");
eval(%val);
%test = new fxStaticSprite2D() { scenegraph = t2dSceneGraph; };
%test.setPosition("1 1");
%test.setSize( "2 2" );
%test.setImageMap( %name );
}
[code]
this should work
#5
04/01/2005 (4:49 pm)
Just to reiterate something i've been harping on lately, you shoudl ALWAYS use expandFilename :p It will catch so many little bugs before they even happen :D
#6
04/01/2005 (5:37 pm)
~ and . are generally only expanded when a file is exec();'ed... any other use in a function should use the expandFilename function.
Torque 3D Owner Pat Wilson
I would test it out by doing:
eval( "datablock fxImageMapDatablock2D(" @ %theme @ "_" @ %type @ ") {mode = full; textureName = " @ %themeDirectory @ %Type @ ";};" );