Game Development Community

Possible to fill an "array" with one line?

by Stephen Triche · in Torque Game Builder · 06/29/2006 (12:49 pm) · 12 replies

Since I'm told TGB arrays are not true arrays it begs the question, is it possible to give a one liner assignment to fill an entire array like it is in C/C++?

Something like...
ExampleArray = {5, 6, 3, 23, 4 , 5 ,3, 2, 4, 5};

If so, does this work with two dimensional arrays?

#1
06/29/2006 (1:03 pm)
No, using the string munging version of arrays (stock), you cannot initalize arrays with this specific technique.

The reason is that there is no such "object" as "ExampleArray"--each array element is a separate object.

Given that negative statement, you could treat your array element initilization as a string, and then do something like:

$arrayInitString = "5 6 3 23 4 5 3 2 4 5";
$arrayElementCount = 10;
for(%xIndex = 0; %xIndex < $arrayElementCount; %xIndex++)
{
   $myArray[%xIndex] = getWord(%arrayInitString, %xIndex);
}

That would accomplish what you seem to be looking for. For multi-dimensional arrays, you would need to make sure your init string is "stacked" properly for your iteration loops.
#2
06/29/2006 (2:01 pm)
Thanks much, helps a lot :)
#3
06/29/2006 (2:04 pm)
I'm more in favor of something like this.

function Array::explode(%this,%arrayInitString )
{
	%arrayInitString = strreplace(%arrayInitString ,","," ");
	for(%xIndex = 0; %xIndex < getWordCount(%arrayInitString); %xIndex++)
	{
		$%this.val[%xIndex] = getWord(%arrayInitString, %xIndex);
	}
}

%array = new scriptObject(ExampleArray)
{
             class=Array;
};

%array.explode("5,6,3,23,4,5,3,2,4,5");

echo(ExampleArray.val[3]);

You should get 23 as the output
#4
06/30/2006 (7:24 am)
What Stephen said will work until your array exceeds the size of the string buffer. Unless TGE 1.4 is wisely using dynamic buffers for strings instead of static buffers as in 1.3. *fingers crossed*
#5
06/30/2006 (8:11 am)
Well honestly, I've yet to see someone give me a case where use of an array is required for their game. Pretty much all container based object requirements can be met via a SimSet, which works much better than many array types.

I don't mean to sound rude about it or anything, but I think mostly it's due to having to use arrays in previous engines/projects, and not knowing about the strengths and benefits of sets.
#6
06/30/2006 (7:07 pm)
Cool. I had heard mention of simsets before but couldn't find any info on how to use them. Today I found: www.garagegames.com/blogs/10513/5705 (not the post itself, but the second comment), which clears things up.
#7
06/30/2006 (10:01 pm)
That is a pretty good writeup, I had forgotten about it!

The one thing though that I keep coming back to is: what does an array provide that you (generic you, as in "anyone thinking they need an array") must have, that isn't in a SimSet? I can think of basically one property, and even that can be mirrored rather easily.

I too used to be an array master--I'd store data in my ANSI C mud code with initialization tables that were hundreds of lines long! But a SimSet is much easier to use once you wrap your hands around it, and automatically takes care of event based deletion of objects that are within the set (something that I've not yet seen an array implementation do).

For example--let's say you keep a single 1 dimensional array that tracks a bunch of sim objects. You write script code that does stuff with these objects, and it works for that one test case, so you leave it be.

You come back a couple of months later and want to re-use the code, but now have a requirement that occasionally some objects may or may not be deleted/removed from the simulation without being aware that they are referenced by your array. With a (properly used) SimSet, that simply doesn't matter--the object is properly cleaned up by the simulation (safeDelete() and it's underlying c++ code), and references to that object could be scattered all around your code in SimSets, yet will be cleaned up for you automatically, maintaining the integrity of your dataset.

With pretty much any script based array implementation (and even C++ ones unless the programmer is aware of the underlying mechanics of smart referencing), your storage objects can be unaware of lost data until it's too late--at the best causing wholes in your array, and at the worst causing improper execution due to unmanaged data that's taken away by another event in the simulation.

Paul's writeup is a great one when it comes to re-implementing an array via Script or SimObjects, but what I'm trying to get across is that with very few exceptions an array is a somewhat outdated dataset.
#8
07/01/2006 (10:28 am)
@stephen, the only value for a real array that I've found (*or real hash, or any non SimObject derived datatype etc) is doing anything where your SimSets go over 5K or so members, and are short lived. There is a performance hit for managment / iteration of each member when dealing with a very large number of members. While those types of things should be done in c++, and by the developer for thier specific requirements, if you wanted to provide something like the ability to do true A* pathfinding on large maps in the binary only version, TS needs a different data type thats not bound to the SimObject derived types probably. Something like an array, binary heap, but its probably a fairly small amount of people that would be needing something like 5K+ node pathfinding, and only bought the binary version of TGB.. so.. I doubt its worth changing anything at this point to get a new datatype in. I doubt anyone is going to be successful in writing the next Baulders Gate with only the binary version of TGB.. But I would be impressed if they did..
#9
07/01/2006 (5:27 pm)
@Rodney: that's feasible I guess, but as you said, that all should be done with a custom class anyway.

And for what it's worth, searching for objects by name is quite fast--it's a hash underneath, not a straight lookup.
#10
07/03/2006 (12:25 pm)
What if you're making a game like Bejeweled, where you need a 2D array? (And this could be global, eliminating the need to pass it to functions.) It seems like a SimSet wouldn't even be possible for this, since the *order* is critical, and SimSets just seem to be containers that don't record order. So the choices would be either ScriptObject, or an "array".

If ScriptObject, it would have to be a ScriptObject of ScriptObjects... the main ScriptObject would contain pointers to each row of ScriptObjects. It would have as many elements as there are rows. Seems kinda messy... Why would you prefer this over the array technique? Even if it's not a "real" array behind the scenes, wouldn't an array still be much easier to access, than a ScriptObject of ScriptObjects?

-Vern
#11
07/03/2006 (7:42 pm)
A SimSet works like a queue where newly inserted items go in at the back. The order of items in the SimSet does not change unless you change it.
#12
07/04/2006 (9:21 am)
You could also do the ScripObject equivalent of faking a 2d array, along the lines of:

array2d[x][y] ==> array1d[ x + y * width]