Game Development Community

Lists and multi dimentional arrays, a little help?

by Kyrah Abattoir · in Torque 3D Professional · 08/07/2011 (4:10 pm) · 13 replies

SO what exactly is the trick there, lets say i want to loop through all the connected players, or build an inventory structure on a player, how do i loop through the different elements of those structures, knowing that torque doesn't provide a "true" list and array structure.

Is there a trick to it?

I heard something about peoples loading stuffs into simgroups or something.

But more generally, when you need to put stuffs in some form of ordered list or array, how do you proceed?

#1
08/07/2011 (4:47 pm)
you move it to C++ instead. make your own class or whatever and then just use TS to talk to it.
#2
08/07/2011 (9:47 pm)
Use an ArrayObject. You can use push_back to push the elements on the ArrayObject and use the method ArrayObject.getKey(%index) to retrieve the element at that index position. Do a search for ArrayObject in the Template projects and you will see how they are manipulated.
#3
08/07/2011 (11:40 pm)
TorqueScript provides arrays. Also multidimensional arrays.
Example:

person[0] = "Asterix";
person[1] = "Obelix";
person[2] = "Miraculix";

// multidimensional array

matrix[0,0] = 5;
matrix[0,1] = 7;
matrix[1,8] = 34;
matrix[23,48] = 3;

To loop through the array you use a for-loop.
Example:

for (%i=0; %i<4;%i++)
{
   echo(person[%i];
}
#4
08/08/2011 (4:28 am)
I think what Kyrah is getting at over 'true' arrays is that torque gathers a few elements and treats them partially like an array, theres no contiguous memory space usage and large 'arrays' dont have the same kind of wrapper in TS as they do in source, making them far less safe to use, and somewhat slower if you use larger arrays.
#5
08/08/2011 (6:20 am)
Quoting Stephen Zepp:
Quote:SimSets are the container class of choice for 90-ish% of all container needs.

Whats exactly the situation that renders simSets unusable or not enough?
#6
08/08/2011 (7:15 am)
could someone explain me more about those "sim sets?" is this what peoples tend to use as lists?
#7
08/08/2011 (7:26 am)
http://www.garagegames.com/community/blogs/view/10949
#8
08/08/2011 (7:27 am)
It's pretty much that. a simset is a central storage container for additional torque objects.

usually scriptobject or additional simset objects are stored within one simset.
#9
08/08/2011 (7:30 am)
Yes Kyrah, this is the multipurpose primary container used in Torque.

Here you have a good starting point as well: TDN Article
#10
08/08/2011 (7:34 am)
Read up on simsets and simgroups in the docs (or better yet in the source). There is a very concise description of usage there.

To iterate through a set in script:

for(%idx = 0; %idx< MySimSet.getCount(); %idx++)
{
      
   %theobject =  MySimSet.getObject(%idx);
   %theobject.dosomestuff(); //... do some stuff w the object

}

ArrayObject is useful for manipulating lists of simple variables within script... although it can be used, I would avoid for any sim object as the simset/group have features for managing and removing the objects properly.
#11
08/08/2011 (8:06 am)
I see that basic lists are space separated strings, but is there another structure for lists available that doesn't risk breaking up if there is a space in one of the values?

Like, if one want to make a chat log, one message per entry, what structure would you use to store each messages in the chatlog?
#12
08/08/2011 (8:11 am)
A simSet of strings.
new SimSet(ChatLog);

function addMessageToLog(%message)
{
   ChatLog.add(%message);
}

Edit: %message has to be an object, with an string field. There cannot be a literal simSet of strings.
#13
08/08/2011 (8:31 am)
That.... would work perfectly!