Game Development Community

Array size

by John Doppler Schiff · in Torque Game Engine · 08/07/2006 (2:20 am) · 7 replies

Hi folks,
I'm setting up a multi-dimensional array like so:

%aTemp[0,0] = "Gizmo";
%aTemp[0,1] = "Widget";
%aTemp[0,2] = "999";

%aTemp[1,0] = "Gadget";
%aTemp[1,1] = "Doohickey";
%aTemp[1,2] = "512";

etc...

Is there a way to obtain the total number of items in the array? How about the size of a particular element (e.g., 15 rows, or 12 columns)? Do I need to stuff these into ScriptObjects to get a count?

Any help would be much appreciated!

-- JohnDopp

#1
08/07/2006 (4:12 am)
Personally I don't know a way of getting the array length. The only way I know would be to count it yourself or, if data structure allows it, using SimSet's as container. SimSet offers the size method you want to have.

Note that SimSet's is kind a list or like a Java Vector - so only 1 dimension and not the 2 you requested. You could find a way around by putting ScriptObjects into a SimSet - just a thought. Anyway :-)
#2
08/07/2006 (4:44 am)
You could also have a SimSet of SimSets which would be the same as a 2d array.
#3
08/07/2006 (5:40 am)
Hey, or a SimSet with ScriptObjects inside with each ScriptObject having multiple SimSets in it. That would be a 1d n-dimensional array... :-)
#4
08/07/2006 (4:07 pm)
That's what I was thinking -- one SimSet + ScriptObject per dimension, with an encompassing SimSet to keep everything tidy. Heh... sounds like Tolkein:

One SimSet to rule them all,
One ScriptObject to find them,
One SimSet to bring them all
and in the darkness bind them...


I was hoping I'd overlooked a simple array.count(), but this will work.

Thanks guys!
#5
08/07/2006 (5:26 pm)
OK, for the sake of future generations, here's what I wound up with:

warn("____________________________");

// create a SimSet to serve as a pseudo-table
new SimSet(ssTable);

// create the table rows and populate them
new ScriptObject(row1);
	row1.objName = "Chair";
	row1.objPos = "57.5 2328 447.5";
	row1.objTex = "mahogany128";

new ScriptObject(row2);
	row2.objName = "Table";
	row2.objPos = "80.5 2328 447.5";
	row2.objTex = "pine128";

new ScriptObject(row3);
	row3.objName = "Moosehead";
	row3.objPos = "72 2320 452.5";
	row3.objTex = "moosefur128";

new ScriptObject(row4);
	row4.objName = "Broken Chair";
	row4.objPos = "60.3 2318 447.5";
	row4.objTex = "mahogany128";

// add the rows to the "table"
ssTable.add(row1);
ssTable.add(row2);
ssTable.add(row3);
ssTable.add(row4);

echo("This pseudo-table has" SPC ssTable.getCount() SPC "rows.");
echo("Row 2's data =" SPC ssTable.getObject(2).objName @ "," SPC ssTable.getObject(2).objPos @ "," SPC ssTable.getObject(2).objTex );

// delete the second row
ssTable.remove(row2);
echo("row2 deleted");

echo("The pseudo-table now has" SPC ssTable.getCount() SPC "rows.");
echo("The 2nd row's data is now" SPC ssTable.getObject(2).objName @ "," SPC ssTable.getObject(2).objPos @ "," SPC ssTable.getObject(2).objTex );

warn("____________________________");

Output:
____________________________
This pseudo-table has 4 rows.
Row 2's data = Moosehead, 72 2320 452.5, moosefur128
row2 deleted
The pseudo-table now has 3 rows.
The 2nd row's data is now Broken Chair, 60.3 2318 447.5, mahogany128
____________________________


Works well enough! I suppose with a few functions you could turn this approach into a pretty flexible array system... something to look into. =)
#6
08/07/2006 (6:05 pm)
I believe there's an array resource somewhere ?
#7
11/21/2012 (1:57 pm)
@John: this future generation thanks you deeply!