Game Development Community

Arrays in script

by Petter Holmberg · in Torque Game Engine · 03/06/2005 (8:27 am) · 5 replies

I've implemented an algorithm in Torque script that requires storing of a lot of temporary data about objects in the current mission. For this, I use arrays, and when I index into the arrays I simply use the id number of the objects in the mission. Since arrays do not need to be created prior to usage, this allows some very elegant code. However, if it's really a good idea or not depends on how Torque has implemented the scripting language. When you refer to new elements in an array, are they just treated as new, independent variables, or is there some more advanced memory management going on? Because if there is, using arrays like this is suddenly not very nice. In a conventional language, like C/C++ it would be a really bad idea, considering that the object id numbers are four digits long and you'd have to allocate an array with thousands of elements and only use a few of them. A hash table or something would be the way to go in that case. But if array elements are simply treated as separate variables that are created at the first use, my use of them should not have performance issues and make the code very readable. I know it works and I haven't had any problems with it so I suspect that arrays work like this, but I haven't checked if it's true before now.

#1
03/06/2005 (9:25 am)
Peter,

They are just simple variables. ArrayName[1] is the same as ArrayName1

You should be safe.

Thanks.
#2
03/06/2005 (9:40 am)
As far as I know, the console treats each element of an array that you reference as an independent variable by tacking the subscript on to the end of the variable name and using that as the variable name. So $aVar0 and $aVar[0] refer to the same variable as do $aVarBlah and $aVar[Blah]. Also, I'm pretty sure variables aren't officially 'created' until something other than "" is written to them, although dump() may just skip those that are, so I could be wrong.

So, skipping the overenthusiastic lecture, your guesses are spot on; array elements are treated as separate variables and don't exist until they're assigned a non-trivial value.

EDIT: Heh, guess I shouldn't leave for lunch before clicking the Submit button. Not only did I get scooped, but Derk said it a lot more clearly =)
#3
09/26/2005 (10:24 am)
But can you remove items from an array? Say I want to dynamically add/remove items from the array. How would I do that? In C++, I would just use a linked list to add/remove the items as I please.
#4
09/26/2005 (10:31 am)
SimSet(StoringObject);

you can then store objects in them

like

StoringObject.add(Player);


or something like this

new ScriptObject(dataSet0);

dataSet0.text = "blah";

StoringObject.add(dataSet0);

then you can use

%count = StoringObject.getCount();

and

%obj = StoringObject.getObject(%i);

to iterate through it :)

you can use

StoringObject.remove(Player);

to remove cleanly
#5
09/26/2005 (12:53 pm)
Cool, thanks!

............

I swear, this Torque engine is going to make me into a lazy programmer ;)
This is a far cry from the days that I made my own templated linked-list for a class assignment.