Game Development Community

Clearing array values?

by Isaac Barbosa · in Torque Game Builder · 02/15/2007 (6:45 pm) · 7 replies

Hi:

I have clear the values of tons of values inside my array. I have used:

for (%i=0; %i<$NUM_WORDS; %i++)
   $words[%i++] = "";

And it works. I just want to ask if there is another way to do so, or if this is the right way :)

Thanks

#1
02/15/2007 (6:48 pm)
@Isaac -- I believe "null" is a valid value, and would actually be the proper way to "clear" something, as "" is an empty string ... though, I'm not 100% sure whether or not TorqueScript evaluates "" to NULL internally or not ...

As for looping, seems the only real way to do it with TorqueScript arrays ...
#2
02/15/2007 (8:39 pm)
You can check with a dump() that if you have $array[0] = 2, $array[1] = 3, $array[2] = "", $array[3] = 4

it will show up as

$array0 = 2
$array1 = 3
$array3 = 4

As arrays in torquescript aren't really arrays, there isn't a way to remove/clear them without a loop, as the [] operator is just a concatenation. Setting things to "" will clear them though, and they will no longer be referenced as having values nor will they show up in dump()
#3
02/15/2007 (8:59 pm)
I have a feeling that loop only clears half the values though...
#4
02/16/2007 (2:16 am)
@Isaac and David
In TS a null is the same as "". Setting the values to null seems more proper to me, but whatever works.

@James
You can't do a dump() on an array.

@Richard
Yeah, that code sure looks like it is going to clear only every other value!

@Isaac
You probably don't want to increment %i inside the array operator.
#5
02/16/2007 (7:45 am)
@Ben:

This is the working code -no ++ inside the array operator-:

for (%i=0; %i<$NUM_WORDS; %i++)
   $words[%i] = "";

cool. So this is the only way.

@Richard:
Quote:I have a feeling that loop only clears half the values though...

Why that feeling? This is clearing entirely the values in my game... is there a unknow risk clearing array values with this method?
#6
02/16/2007 (7:54 am)
That feeling was due to the extra postfix operator inside the array operator. Your original posted code DOES clear only half the values.
#7
02/16/2007 (8:16 am)
Ah... I never tried it... it was a mistake when copied