Game Development Community

Array.count?!?

by Jason Booth · in Torque Game Builder · 01/20/2006 (11:39 pm) · 10 replies

How do you get the .size or .count of an array? For the life of me I can't seem to find a way to do this, and the only docs I can find online don't seem to work without additions to the sdk. Shouldn't I be able to:

%a[1] = "ahga";

%a.count()

How come this doesn't work? I've tried .getCount(), .size(), and pretty much everything else I can think of.

#1
01/22/2006 (7:51 am)
As far as I know it is not possible to count the number of elements within an Array, would love to be proven wrong though. I generally end up using SimSets.
#2
01/22/2006 (10:52 am)
Arrays in Torquescript are more of a naming macro than a storage class. You can put anything in the array index, such as a string, and it'll work (like an associative array). What happens behind the scenes is the variable gets converted to something like VARNAMEINDEX and that's all.

A small snippet from the tge docs (ambiguous enough that I think it's harmless to put here) that explains arrays well:

Quote:
$MyArray0 and $MyArray[0] are the same. It may be surprising, but TorqueScript allows you to access array indices without using the common bracket [] syntax. Again, it is advisable to use a naming convention to identify arrays in your code.

$aryMyArray[0] = "slot 0";
echo ($aryMyArray0);

$aryMyArray[1] = "slot 1";
echo ($aryMyArray1);

$aryMyArray[0,0] = "slot 0-0";
echo ($aryMyArray0_0);

As the output from the above code illustrates, TorqueScript treats bracketed indices the same as appended indices.

The downsides of this approach are there is no way to truly identify what was originally an array element and what's not, and you can have non-obvious variable collisions if you're not careful.
#3
01/22/2006 (11:17 am)
Thats what I was afraid of. Unfortunately, it heavily cripples arrays to not be able to count thier members, etc. What I've done so far is to create my own array object to store things and do the counting. Unfortunately, it makes the code a little ugly, as I end up with code that looks like this:

%player.item[1].weapon.item[2].damage;

instead of just:

%player[1].weapon[2].damage;

but at least I can get a .count for an array.

code for my array class below:


function newArray()
{
%a = new ScriptObject()
{
Class = "Array";
};
%a.count = 0;
return %a;
}

function Array::Add(%this, %item)
{
%this.count++;
%this.item[%this.count] = %item;
}

function Array::Remove(%this, %idx)
{
if (%idx > %this.count)
return;
if (%idx == %this.count)
{
%this.count--;
return;
}

for (%i = %idx; %i < %this.count; %i++)
{
%this.item[%i] = %this.item[%i+1];
}

%this.count--;
}
#4
01/22/2006 (11:48 am)
Http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=4711

I haven't used it in 1.4 or anything, but it is an array class for script

Just wanted to say that personally I usually end out using simGroups for most things that require arrays.
Although you need to make sure that if your placing "real" mission objects in them that they get cleared out at mission end also.
#5
01/22/2006 (2:29 pm)
Why not just use a SimSet? It's rather rare that you need an array specifically, compared to the functionality that a SimSet provides you.
#6
01/22/2006 (3:09 pm)
@Stephen: is there a good explanation ( and maybe tutorial) on simsets and their use? I am a T2D owner, and i havent seen any good info on using them.

-Jason
#7
01/22/2006 (8:25 pm)
A SimSet is simply an abstract datatype (the classic kind) that acts as a container for objects. It has all the functionality listed above, as well as already implemented, such as:

.add()
.remove()
.getCount()
.getObject()
.isMember()

plus quite a bit more.

I'm actually working on a "ClassHierarchy" TDN article, and have just recently finished the SimObject portion--SimSet is next, but if you do a search here and/or in the code I'm sure you'll see dozens of good threads!

IMO the single most important/useful aspect of SimSets is that they use the smart referencing provided by SimObject, in that if a SimObject is part of a SimSet, but then subsequently deleted (properly), it auto-magically updates all of the SimSets it was a part of. Things that will destroy your "normal" self-implemented arrays without a lot of work.

EDIT: added the link to the work in progress TDN Article on Torque Class Hierarchy. As I mentioned I haven't written the SimSet/SimGroup stuff yet, but you might want to keep an eye on it!
#8
01/22/2006 (10:16 pm)
So I converted everything over to simsets tonight and it all seems to work pretty well. I was hoping the syntax would get shorter, but alas this suffers from the same verboseness that my solution did. I suppose the big benifit is that simsets are implimented in C++, and probrably a lot faster than a script based version.
#9
01/22/2006 (10:58 pm)
Well, the big advantage like I said is smart referencing. Pretty nifty (and fundamentally important) benefit of how Torque is structured.
#10
01/23/2006 (11:18 am)
Another advantage of using SimSets (or any other object) is they have a built in .save() function... Here is a post (have to scroll down a bit on the page) I did with more info on saving (if you haven't already explored that) :)