Game Development Community

Can I access using dynamically created var name?

by Shawn C. · in Torque Game Builder · 01/18/2006 (4:36 pm) · 10 replies

I didn't see this mentioned in the Torque Script primer so I thought I would ask.

Is it possible to create a variable name dynamically (at run time) and access the data it contains? Let me give an example of what I mean.

Given that my script contains the following vars:
$coor0 = "1 4";
$coor1 = "3 6";
$coor2 = "2 4";

can I access their data in a for loop as such?
for (%i = 0; %i < 3 %i++)
{
// create a sprite
sprite.setPosition ("$coor" @ %i); <--- I realize this doesn't work
}

Question is, can this even be done? For instance, Flash Actionscript will allow you to do: _root ["$coor" + 1]

#1
01/18/2006 (4:57 pm)
If you use arrays, there is no problem.

$coor[0] = "1 4";
$coor[1] = "3 6";
$coor[2] = "2 4";

for (%i = 0; %i < 3; %i++)
{
     sprite.setPosition ($coor[%i]);
}

TorqueScript arrays are pretty flexible.
#2
01/18/2006 (5:28 pm)
I did think about using arrays, and that is probably what I will end up doing. Does your answer mean then that there is no way to build a string and then use it as a variable name?
Thanks for the reply.
#3
01/18/2006 (5:36 pm)
You can also use eval()... like this..

for (%i = 0; %i < 3 %i++)
{
   // create a sprite
   eval("sprite.setPosition ( $coor" @ %i @ " );");
}

With eval you can execute a string as script, so you can peice many combinations together.
#4
01/18/2006 (6:05 pm)
Thanks, Matthew, that seems like what I am looking for, but when I use it I get an error telling me that setPosition has an invalid number of parameters.
#5
01/18/2006 (6:29 pm)
Try doing this...

for (%i = 0; %i < 3 %i++)
{
   // create a sprite
   %eval = "sprite.setPosition ( $coor" @ %i @ " );";
   echo(%eval); 
   eval(%eval);
}

Thats one of the best things about bringing a function call into one string, you can echo it out easily to see if its working.
#6
01/18/2006 (6:31 pm)
If the above echo's out that its not passing the right values you can try this as well

for (%i = 0; %i < 3 %i++)
{
   // create a sprite
   %eval = "%coor = $coor" @ %i @ ";";
   eval(%eval);
   sprite.setPosition( %coor );
}
#7
01/18/2006 (6:32 pm)
Note: I added proper semi-colons to in the last two posts so double check that.
#8
01/18/2006 (6:34 pm)
I'm out of time tonight, but I'll give it a shot tomorrow, and thanks for the suggestions.
#9
01/19/2006 (12:28 am)
Your biggest problem in your original code is this:

sprite.setPosition ("$coor" @ %i)

Losing the quote marks would do the trick :)
#10
01/19/2006 (9:34 am)
Philip, are you sure, because when I try it that way the coordinates just evaluate to whatever %i equals and I get the error "Invalid number of parameters."


Matthew, this is a condensed version of the block of code you posted in your last post, and it does work.
%sprite.setPosition (eval("%coor = $coor" @ %i @ ";"));


I'm glad I asked, because I wouldn't have otherwise known about eval and it seems useful. Much thanks to all who are helping me as I get started here.