Simple Array Class for TorqueScript
by Emil Diego · 04/10/2007 (9:09 am) · 6 comments
Download Code File
Since Torque Script does not implement arrays in a standard fasion, I found myself unable to do certain things like pass them as function parameters. To get around this limitation I created this small light weight class that I could use in my script files.
To start using the file, simply copy it to the \RTSStarterKit\engine\console directory and include it in the Project File (Visual Studio Project file / Makefile). Rebuild the executable and you should be able to start using the Array class in TorqueScript.
Here is an example of using the Array class. In this example I am loping through all of the units that I currently have selected and I am grouping them together by their unit id. Each array holds a different piece of information. the iUnitId stores the unique unit id, the iUnitCount stores the number of each unit in the group and the iUnitHealth stores the health of all the units in the group.
Since Torque Script does not implement arrays in a standard fasion, I found myself unable to do certain things like pass them as function parameters. To get around this limitation I created this small light weight class that I could use in my script files.
To start using the file, simply copy it to the \RTSStarterKit\engine\console directory and include it in the Project File (Visual Studio Project file / Makefile). Rebuild the executable and you should be able to start using the Array class in TorqueScript.
Here is an example of using the Array class. In this example I am loping through all of the units that I currently have selected and I am grouping them together by their unit id. Each array holds a different piece of information. the iUnitId stores the unique unit id, the iUnitCount stores the number of each unit in the group and the iUnitHealth stores the health of all the units in the group.
%iUnitId = new Array();
%iUnitCount = new Array();
%iUnitHealth = new Array();
for (%i = 0; %i < %this.getSelectionSize(); %i++)
{
// Get the ID of the unit
%idx = %this.getTypeID(%i);
// Check to see if the unit is already in the array.
%iCurrentIndex = %iUnitId.findValue( %idx );
if (%iCurrentIndex < 0)
{
// The unit id was not found in the array
%iCurrentIndex = 0;
// Lets add the unit id to the array
%iUnitId.add(%idx);
// lets let's update the count.
%iUnitCount.add(1);
// Update the health
%iUnitHealth.add(%this.getHealthPercent(%i));
}
else
{
// The unit does already exist int he array, i'll update it.
// Get the current unit count.
%iTmpCount = %iUnitCount.getValue( %iCurrentIndex );
// Update it
%iUnitCount.setValue(%iCurrentIndex, (%iTmpCount + 1));
// Now update the health counter
%iTmpHealth = %iUnitHealth.getValue( %iCurrentIndex );
%iUnitHealth.setValue(%iCurrentIndex, %iTmpHealth + %this.getHealthPercent(%i));
}
}
#2
Btw, have you seen this: www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=4711 resource?
04/11/2007 (4:25 am)
Thanks for resource Emil. This is a nice one, could be very useful at some points.Btw, have you seen this: www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=4711 resource?
#3
I like the simplicity of the Class, but I'm failing to see the benefits of it over the SimSet ... and, is the STL Vector platform friendly with OSX and Linux?
04/12/2007 (11:31 am)
What are the benefits of using this Array class over using something like the SimSet class that is already in the engine code?Array.getValue(idx) == SimSet.getObject(idx) Array.setValue(idx, value) == (SimSet.getObject(idx) = value) Array.length() == SimSet.getCount() ... ...
I like the simplicity of the Class, but I'm failing to see the benefits of it over the SimSet ... and, is the STL Vector platform friendly with OSX and Linux?
#4
It's upon to you what to use regardless the project needs.
P.S. Not sure about THIS resource to be safe on linux/macos - havn't tried/tested it yet, but the other one (I've mentioned one post up) is safe to use in linux/macos. And again - it depends on your needs...
04/12/2007 (6:34 pm)
David, the benefits of using Array is: memory. It takes less memory to handle. But if you use the simSet/simObjects you have more "flexible" behaviour.It's upon to you what to use regardless the project needs.
P.S. Not sure about THIS resource to be safe on linux/macos - havn't tried/tested it yet, but the other one (I've mentioned one post up) is safe to use in linux/macos. And again - it depends on your needs...
#5
I can see that the code for the Array class is much smaller, but I don't necessarily see the overall memory advantage ...
I'm also not trying to say that this class is useless, I'm just trying to better understand it's usefulness ...
04/12/2007 (7:22 pm)
bank -- what memory overhead exists with SimSet? doesn't it just store a pointer to the SimObject in a Linked List or Vector?I can see that the code for the Array class is much smaller, but I don't necessarily see the overall memory advantage ...
I'm also not trying to say that this class is useless, I'm just trying to better understand it's usefulness ...
#6
%iNmbs = new Array();
%iNmbs.setValue(1, 1);
%iNmbs.setValue(2, 2);
%nmb = 1;
%ettan = %iNmbs.getValue( %nmb );
echo("1an: " @ %ettan);
seems nifty though.
09/16/2007 (10:11 am)
It does not seem to work on 1.5, unless the following is faulty script:%iNmbs = new Array();
%iNmbs.setValue(1, 1);
%iNmbs.setValue(2, 2);
%nmb = 1;
%ettan = %iNmbs.getValue( %nmb );
echo("1an: " @ %ettan);
seems nifty though.

Torque Owner Suryadiputra Liawatimena
so with this array object, we can pass the variable as an argument into another function ? did i get this right ? if so thats cool :D
i'll try this resource tonight. tq for the resource.