Game Development Community

Data structures

by Jason McIntosh · in Torque Game Builder · 03/07/2005 (11:07 am) · 6 replies

Just in case I missed something in the docs, are there any built-in data structures such as stacks, lists, maps, or queues? I can create them in script, but I don't want to if I don't need to. I guess these could even be implemented in C++ for efficiency (using STL perhaps?) and exposed to script?

#1
03/07/2005 (12:05 pm)
Ok, well, I whipped up a simple ArrayList for something I needed, and maybe it will be useful for others to see what I did.

function ArrayList::newArrayList()
{
	return new ScriptObject()
	{
		class = arrayList;
		length = 0;
	};
}

function ArrayList::pushElement( %this, %element )
{
	%this.contents[ %this.length ] = %element;
	%this.length++;
}

function ArrayList::getLength( %this )
{
	return %this.length;
}

function ArrayList::index( %this, %i )
{
	if ( %i > %this.length )
		echo( "\c1ArrayList::index out of bounds: " @ %i );

	return %this.contents[ %i ];
}

Usage:
$example = ArrayList::newArrayList();

// Let's test it out...
$example.pushElement( 3 );
echo( "Example Length:" SPC $example.getLength() );
	
$example.pushElement( 6 );
echo( "Example Length:" SPC $example.getLength() );

echo( "Element 1 = " @ $example.index( 1 ) );
echo( "Element 10 = " @ $example.index( 10 ) );
#2
03/07/2005 (1:02 pm)
Can't you just use normal arrays?

$arrayOfObjs[0] = obj;
#3
03/07/2005 (1:10 pm)
One good advantage of script objects is they can be passed
#4
03/07/2005 (2:07 pm)
This is a very good example of the power of the scriptObject...
#5
03/07/2005 (5:28 pm)
Oh, arrays aren't passable or returnable? Someone seriously needs to work on the TorqueScript language. :^)
#6
03/07/2005 (6:13 pm)
ScriptGroups are pretty cool too. I am pretty sure they are in T2D as well as TGE. I am not positive though, because I have my T2D and TGE source trees merged. Here is a simple example:
%myGroup = new ScriptGroup();

%myObject = new ScriptObject(MyClass);
%myScriptGroup.add(%myObject);
echo("Example Length:" SPC %myGroup.getCount());

%anotherObject = new ScriptObject(MyClass);
echo("Example Length:" SPC %myGroup.getCount());

for(%c = 0 ; %c < %myGroup.getCount() ; %c++)
{
   %myGroup.getObject(%c).dump();
}
The cool thing about script groups is that you can even sub class them, and give them even more functionality (you could of course to the same thing with the ArrayList example too). The .mis files use ScriptGroups to create missions in TGE.