Game Development Community

Best type of collection for a list of strings or vectors?

by Charlie Patterson · in Torque Game Builder · 06/03/2011 (9:06 pm) · 5 replies

I want to keep about 1000 strings or vectors in T2D. It seems I can't use a SimSet because that only holds "objects".

Set::add: Object "2000 0 M1" doesn't exist

But I'm a little skeptical of TorqueScript arrays. For one, I can't ask for the size (that I can find). Another problem is that I can't force it to clear when I'm done (again, that I can find). Apparently arrays are simply a list of variables named like the array with a number appended. This seems fine for 3 elements, but not 1000.

Am I missing something?

#1
06/04/2011 (3:34 am)
$vectorSet = new SimSet();

%vector = new ScriptObject();
%vector.vector = "2000 0 M1";

$vectorSet.add (%vector);

I don't much like having to use an entire ScriptObject to store a single line of data but this would work. And you can manage the data.

To clear the entire SimSet you can call

while($vectorSet.getCount() > 0)
{
   %vectorObj = $vectorSet.getObject(0);
   $vectorSet.remove(0);
   %vectorObj.delete();
}
$vectorSet.delete();
#2
06/04/2011 (9:11 am)
You are not missing much Charlie, thats how it works.

To add a bit to what Matt recommended, if you use a SimGroup instead of a SimSet, when you delete the SimGroup, you also delete all objects within.
#3
06/04/2011 (9:51 am)
Ahhh! Thanks Matt! It seems I can also go up one in the class hierarchy from ScriptObject and use a SimObject. I'm not sure what I'd lose?! I also split my string-based "vector" into multiple fields, which is cool. It was more of a struct.

%vector = new SimObject();
      %vector.time = getWord(%line, 0);
      %vector.loc = getWord(%line, 1);
      %vector.type = getWord(%line, 2);
]

Anyone know why a ScriptObject may be preferred over a SimObject?

Hmmm. Novack, is that so? I just checked the docs and it doesn't mention a SimGroup deleting its children. I was wondering what they brought to the table that was better than a SimSet in practicality. I'll look into that, too! (Because I was making the mistake of not deleting the contents, anyway!)
#4
06/04/2011 (11:37 am)
A quick review of the code gives me a high-level idea of the diff between a SimObject and a ScriptObject:

A ScriptObject will call its classes onAdd and onRemove callbacks. That seems to be it.

And I believe Novack is correct that a SimGroup will delete its contents when it is deleted! However, I believe you could call clear() on it and it wouldn't delete the items! So it's probably a bit of a hack to use it as a way to auto-delete your objects.

Thanks again!
#5
06/04/2011 (12:04 pm)
All your above comments are correct. Also ScriptObject allows binding of a class (ie: class = "MyClass";), which I cant remember if is feasible with SimObject.