Game Development Community

Using a %Var to get a Object field?

by Chad Kilgore · in Torque Game Builder · 01/25/2009 (12:31 pm) · 2 replies

Is it possible to use a %variable to access an object's field?

Using [url="http://www.garagegames.com/community/resources/view/15077"TScriptArray[/url], I am constructing an array of objects with 2 fields: player and size. I would like to be able to expand TScriptArray's sort functionality and add an extra parameter.
function TScriptArray::sort( %this, %compare, %ascending, %field )
{
   ...
   %result = call( %compare, %this.array[%i][b].%field[/b], %this.array[%i+1][b].%field[/b] );
}
Then the array could be sorted using the field for comparison, such as:
%Array.sort(%compare, %ascending, "player");
%Array.sort(%compare, %ascending, "size");

Is this possible? Or is is just a matter of writing a special case?

#1
01/25/2009 (8:19 pm)
i'm not totally familiar with the TScript resource,
but if array[%i] is a ScriptObject (or a SimObject of any type),
then the array[%i].%field syntax will give you trouble.

fortunately, you can use eval():

// %foo is a SimObject with a field named "bar",
// and coincidentally, %fieldName = "bar".

%cmd = "%fieldVal = %foo." @ %fieldName @ ";";
eval(%cmd);

// %fieldVal should now contain the contents of %foo.bar
#2
01/25/2009 (8:59 pm)
You can also do %this.array[%i][%field] IIRC.