Game Development Community

Variable Naming

by Jumex · in Torque Game Builder · 09/13/2007 (11:23 am) · 6 replies

Function test()
{
for(%x = 1; %x <= $tfu; %x=%x +1)
{
$@"f"@%x = "f"@%x;
}
}

TFU = Total Friendly Units

Assuming there are 3 units on the field that have the name f1, f2, and f3. This loop should create 3 variables for me to use.

$f1 = f1
$f2 = f2
$f3 = f3

now ="f"@%x works I tested it. However when I try to apply the same concept to the variable I keep getting parse errors.

#1
09/13/2007 (11:30 am)
You need to use the eval() command when constructing syntax strings you wish to execute.

Try:
%str="$"@"f"@%x@" = f"@%x@";"
eval(%str);

For more info see:
tdn.garagegames.com/wiki/TorqueScript_Console_Functions_5#eval.28_script_.29

Gabriel
#2
09/13/2007 (11:39 am)
Eval("$"@"f"@%x@" = f"@%x@";");

That did it, thank you
#3
09/13/2007 (12:49 pm)
For(%z = 1; %z <= $tfu; %z=%z +1)
{
eval(%win@"="@"$"@"fx"@%z@".x;");
eval(%unit@"="$"@"f"@%z@");

if(%win > $selectstart.x && %win < $selectend.x)
{
selectunit(%unit);
}

grr stuck agian. Okay in this one I have 3 variable already created these variables hold the position of the 3 selected objects from before. The variables are $fx1, $fx2, $fx3. This loop is supposed to cylcle through each variable and first associate it X position with %win then run it through the if statment and if it passes it will send the appropriate variable ($f1, $f2, $f3) to the function select unit.

Once agian I am getting Parse Errors :/
#4
09/13/2007 (1:03 pm)
Try
[b]f[/b]or(%z = 1; %z <= $tfu; %z[b]++[/b])
{
   eval(%win@"="@"$"@"fx"@%z@".x;");
   eval(%unit@"="$"@"f"@%z@");

   if([b]([/b]%win > $selectstart.x[b])[/b] && [b]([/b]%win < $selectend.x[b])[/b])
   {
      selectunit(%unit);
   }
}

The 'for' keyword should be lower case and you need brackets when doing compound logical statements. The %z++ instead of %z=%z+1 is optional, but more elegant.

Gabriel
#5
09/14/2007 (10:00 am)
You need a semi-colon in that second eval line.
#6
09/19/2007 (9:47 pm)
Eval is an invaluable tool for making some clever script... as well as for creating some ugly and hard to debug code if not careful =0