Game Development Community

Arrays

by Sam3d · in Torque Game Engine · 04/08/2005 (12:21 pm) · 10 replies

I'm trying to manipulate arrays.

This works fine:
%pos[0,0]= "42 6 16";
%pos[0,1]= "70 6 16";
position = %pos[%i,%j];


Now I need to add / erase on an array.
I have arrayobject.cc in my build.

This works fine:
$a = new array();
$a.add(0,"testing");
echo("$a.getValue(0) is " @ $a.getValue(0));


How can I add more keys? Make it multidimensional ?
This doesn't work:
$a.add([0,0],"testing");

#1
04/08/2005 (2:10 pm)
I might be able to use SimGroup, if I'm using it properly...

new SimGroup(mySimGroup);

function ....
{

%this = new StaticShape("Block0"){...}
mySimGroup.add(%this);

}


So, if I can look at all the shapes in mySimGroup, I can loop thru it and get their names and positions... ?
I don't care about the order, but I need to know how many shapes are in mySimGroup so I can use a loop.

mySimGroup.getCount() doesn't return anything... ?


I'm using SimGroup because most of the shapes are deleted during gameplay, but some of the shapes may still exist on exit.
#2
04/08/2005 (2:55 pm)
From your example I derive that

$a.add("0,1", "testing");

should work...

I'm not sure tho, but give it a go :)
#3
04/08/2005 (3:28 pm)
Well it adds it, but as a new element in the array.

So this:

$b = new array();
$b.add("0,0","asdf");
$b.add("0,1",200);

$b.echo();
echo(".............................$b.getValue(0,0) is " @ $b.getValue("0,0"));
echo(".............................$b.getValue(0,1) is " @ $b.getValue("0,1"));


Results in this:

Array Listing:
Index Key Value
0 [0,0] => asdf
1 [0,1] => 200
.............................$b.getValue(0,0) is asdf
.............................$b.getValue(0,1) is asdf



I think maybe SimGroups are a more 'native' approach, but still trying to figure that out.



.
#4
04/08/2005 (5:56 pm)
Yeah, suspected as much. Doesn't look like the getValue member likes multidimensional arrays much. Maybe you have to create your own extended function to make it happen like that.

Anyway, good luck in figuring this out, I now think it's out of my league already :)
#5
04/09/2005 (7:25 am)
This has been discussed several times in various threads, but TorqueScript does not implement arrays--it uses string manipulation to replace your array index syntax with a derived variable name.

This allows for very quick and very easy basic array capabilities, but breaks down when you start using assumptions about the underlying implementation to try to do "neat" things, like you might with a C/C++ based array implementation.

For most implementation needs, you have a couple of different options: the basic array concept, SimSets (SimGroups are specialized SimSets, and you may want to wait on using SimGroups until you understand the specialized requirements), and even script objects (see various forum discussions on these as well) for advanced requirements.
#6
04/09/2005 (8:09 am)
>TorqueScript does not implement arrays--it uses string manipulation to replace your array index syntax with a derived variable name.


I thought the purpose of the arrayobject.cc resource was to allow arrays in TorqueScript.

Thanks for the pointers re SimSets and script objects.
#7
04/09/2005 (2:54 pm)
ArrayObject does help shore up that area of the language, true. It's not the same as proper array support in the language tho.
#8
04/09/2005 (7:43 pm)
Ok, it's quite straightforward... finally, lol.

For any other newbie scriptors out there...

Say you create / delete a bunch of objects at various times and you want to access existing ones with a loop and you don't care about the order.

new SimGroup(mySimGroup);

function .... 
{

%this = new StaticShape("somename"){...}
mySimGroup.add(%this);

}

You can loop thru everything in that particular SimGroup and get, for instance, their positions.

function checkmySimGroup()
{
   %dataGroup = "mySimGroup"; 
   for(%i = 0; %i < %dataGroup.getCount(); %i++)
   {
      %obj = %dataGroup.getObject(%i);
      echo(%i SPC "Found object! Name is:" SPC %obj.getName() SPC "POSITION:" SPC %obj.position SPC "ClassName:" SPC %obj.getClassName());
   }
}


Using a SimGroup means you don't have to worry about MissionCleanup, all SimGroups are deleted on exit.

And if you have the editor open, objects won't be popping on and off the object list in the Inspector window, so you don't have to worry about inadvertently saving a shape into your mission file.


The tork.beffy.de/ site gave me the info I needed and it has lots of other useful and educational snippets.
#9
04/09/2005 (8:00 pm)
Caveat on SimGroups (and probably one of the reasons Stephen said they were more advanced) is that an object can be in only one SimGroup at a time. If it is in 1 group and you add it to another it is removed from the first transparently.

Just want everyoen to be clear on that becuase it bites a LOT of people in the bum so to speak. An object can exist in multiple SimSets however.
#10
04/09/2005 (9:18 pm)
Right, thanks.