Game Development Community

Array's

by Chris "DiGi" Timberlake · in General Discussion · 09/29/2005 (8:44 pm) · 7 replies

Anyone know the command to get the size of an array? Is there one?

#1
09/30/2005 (6:51 am)
Unfortunately there is none.

Arrays doesn't have a size. When you create one, you create several variables with the index appended to them, e.g. %array[1] becomes %array_1, %array[2,3] becomes %array_2_3 and so on.
#2
09/30/2005 (11:58 am)
Guess i'll have to make one via script, that counts upward, then when it reaches a array number without a varible it uses that one =)
#3
09/30/2005 (12:46 pm)
Just to clarify, I'm pretty sure %array[1] would become %array1. No underscore.
#4
09/30/2005 (2:30 pm)
Chris, i think there's a better way. either store the count in bucket 0, or use another variable to store the count.

i havent done this myself, but logically i think it makes better sence than the workaround you outlined
#5
09/30/2005 (5:11 pm)
Well, the thing is i need to add to the array, so i cant use a different varibles, and i cant store it in slot 0, because its a list that needs to grow.
#6
09/30/2005 (5:30 pm)
$array = new ScriptObject()
{
   n = 0;
};

function push(%a, %what)
{
   %a.array[%a.n] = %what;
   %a.n++;
}
#7
09/30/2005 (6:00 pm)
Ummm.. what Tom said...

i think.

@Chris: you are getting confused. store the total count of the array in a seperate variable (such as 'n' as tom outlines) that way you always know how many items you have in your array.

If this doesnt make sence you should seriously spend some time and figure out why this is an ideal solution, as this type of concept is a pretty core principle you are going to hit again and again in any type of programming.