Script $array.count ?
by Peterjohn Griffiths · in Torque Game Engine · 02/24/2006 (11:32 pm) · 7 replies
Sorry about asking something that should be simple. I'm new to torque and I am getting stuck on something that should be very simple.
I need to use the count of an array in a function.
Ive tried the following but no joy....
count($array);
$array.count;
$array.count();
I have tried using your search on the site but no joy there.
Many thanks
I need to use the count of an array in a function.
Ive tried the following but no joy....
count($array);
$array.count;
$array.count();
I have tried using your search on the site but no joy there.
Many thanks
#2
The simple way is to increment an index that keeps the current count.
There is a resource with an array replacement that has more features that can be found here:
Script Array Upgrade
I believe it includes a 'count()' function.
-Jeff
02/25/2006 (12:32 am)
If my understanding of the console array is correct, then they aren't really creating arrays, but using shorthand for unique variables. E.g. $array[5] is really $array5.The simple way is to increment an index that keeps the current count.
There is a resource with an array replacement that has more features that can be found here:
Script Array Upgrade
I believe it includes a 'count()' function.
-Jeff
#3
Thats a big one to miss out of a scripting language and will make life hell when developing. :+{
02/25/2006 (3:23 am)
Do you mean that arrays arn't in torque!. Wow. :+OThats a big one to miss out of a scripting language and will make life hell when developing. :+{
#4
Well they are and arn't in script. As Jeff says, array indexes are really just converted into uniquly named variables. You can do the same with these fake arrays as normal arrays, but theres a good gain in efficiency I think. The only downside is you can't pass these fake arrays to a function as a parameter or returned result.
I don't use arrays all that often. Usually I find simsets can be used instead, at least for changing data. Fixed data I still tend to store in an array. The only time I couldn't straight use a simset was when retained ordering was important after deletions, but a quick mod to the simset class allows that.
When I use the "fake" script arrays I tend to always keep track of the count by using element zero eg
Then anytime I update the number of elements in the array I keep element 0 up to date with the total count. This allows easy looping through the arrays based on %i=1 while %i <= $myarray[2].
Your alternative is to implement the full blown array resource that Jeff linked to above. However for most cases you will probably find that SimSets can be used instead.
02/25/2006 (6:57 am)
Quote:
Do you mean that arrays arn't in torque!. Wow. :+O
Thats a big one to miss out of a scripting language and will make life hell when developing. :+{
Well they are and arn't in script. As Jeff says, array indexes are really just converted into uniquly named variables. You can do the same with these fake arrays as normal arrays, but theres a good gain in efficiency I think. The only downside is you can't pass these fake arrays to a function as a parameter or returned result.
I don't use arrays all that often. Usually I find simsets can be used instead, at least for changing data. Fixed data I still tend to store in an array. The only time I couldn't straight use a simset was when retained ordering was important after deletions, but a quick mod to the simset class allows that.
When I use the "fake" script arrays I tend to always keep track of the count by using element zero eg
Quote:
$myarray[1]="Apple";
$myarray[2]="Pear";
$myarray[0]=2;
Then anytime I update the number of elements in the array I keep element 0 up to date with the total count. This allows easy looping through the arrays based on %i=1 while %i <= $myarray[2].
Your alternative is to implement the full blown array resource that Jeff linked to above. However for most cases you will probably find that SimSets can be used instead.
#5
No, it won't ;) People thought the same about the Model-T (wow, no horses? man, that's gonna make life hell trying to get anywhere!) until they learned the advantages of new technology.
Torque has a completely feature rich SimSet, which not only provides you with just about the same capabilities of an array (all of them from a container perspective, most of them from an ordered set perspective), but also maintains smart referencing--which means that if (used properly) you'll never have to worry about several issues with any type of array implementation.
I highly recommend you research the concept of SimSets both here and in the tutorials, and I'm reasonably confident you'll become a convert!
02/25/2006 (7:32 am)
Quote:
Do you mean that arrays arn't in torque!. Wow. :+O
Thats a big one to miss out of a scripting language and will make life hell when developing. :+{
No, it won't ;) People thought the same about the Model-T (wow, no horses? man, that's gonna make life hell trying to get anywhere!) until they learned the advantages of new technology.
Torque has a completely feature rich SimSet, which not only provides you with just about the same capabilities of an array (all of them from a container perspective, most of them from an ordered set perspective), but also maintains smart referencing--which means that if (used properly) you'll never have to worry about several issues with any type of array implementation.
I highly recommend you research the concept of SimSets both here and in the tutorials, and I'm reasonably confident you'll become a convert!
#6
I was just thinking about that approach the other day. It's like (Turbo) Pascal strings.
@Stephen,
Now I've got to look at the SimSet approach!
Research project for you all: Devo - "Freedom of Choice"
02/25/2006 (1:56 pm)
@Gary,I was just thinking about that approach the other day. It's like (Turbo) Pascal strings.
@Stephen,
Now I've got to look at the SimSet approach!
Research project for you all: Devo - "Freedom of Choice"
#7
I will have a look into SimSets and see how this will help.
Many thanks for all your responces.
02/27/2006 (4:14 pm)
As I said, i'm new and didn't know about SimSets.I will have a look into SimSets and see how this will help.
Many thanks for all your responces.
Torque Owner Peterjohn Griffiths
Default Studio Name
still no joy.