Game Development Community

TorqueScript alternative to Enums?

by Drethon · in Game Design and Creative Issues · 08/06/2009 (7:09 am) · 6 replies

Need a TorqueScript forum to put this in... um anyway...

I'm trying to figure out how best to create a type structure, for example a list of types of minerals available that could be provided by mining or used by manufacturing. In C++ I would create a Mineral class with a member type defining minerals. Not so available in TorqueScript.

The option in Torque appears to be to put the data in an array, it looks like the best places to put this array is either a global array or possibly drop it in a datablock. Are these my only options and is there a reason one might be better than the other?

Thanks!

#1
08/06/2009 (8:37 am)
You can use a vector, and manage it with the capable string and field functions of torquescript.
#2
08/09/2009 (10:32 am)
If you have individual datablocks or ScriptObjects or whatever, you can also put them in a SimSet. A SimSet is an ordered set, and the default order is the order in which the objects were first added to the set. Of course, using a SimSet means you must use sequential numerical indexes, but if that's a problem, you can also set some dynamic properties on your SimSet which point to the correct index:

// Setup
%set = new SimSet(MineralSet);
%set.copper = %set.getCount();
%set.add(new ScriptObject() {
    name = "Copper";
    value = 1;
});

%set.silver = %set.getCount();
%set.add(new ScriptObject() {
    name = "Silver";
    value = 10;
});

%set.gold = %set.getCount();
%set.add(new ScriptObject() {
    name = "Gold";
    value = 100;
});

// Usage
echo(MineralSet.getObject(MineralSet.copper).name);
echo(MineralSet.getObject(MineralSet.silver).value);
echo(MineralSet.getObject(MineralSet.gold).value);
#3
08/09/2009 (3:54 pm)
That looks a bit bloated for an Enum... probably more adecuate for an structure mimic.


See how easily you can mimic the Enum behaviour:
// *** C++ Enum ***
enum ColorT {red, orange, yellow, green, blue, indigo, violet};

// *** TorqueScript Mimic ***
%ColorT = "red orange yellow green blue indigo violet";
Then you can use it like:
// Sets %mycolor to "orange"
%mycolor = GetWord(%ColorT, 1);

// More complex example:
if (%mycolor $= GetWord(%ColorT, 0)
   %mycolor = GetWord(%ColorT, 1)
Also by default, an Enum assign consecutive integer values starting at zero to the constants, something also available by using the string vector. There you have two choices:

To work with the character index, and simply use:
%constantValue = StrStr(%ColorT, "orange");
//Will return 4, as 'orange' starts at the 5th character of the string (starting at 0)
Or to implement a simple function to get the Constant position into the vector:
function getWordPos(%string, %word)
{
	for (%i=0; GetWordCount(%string); %i++)
	{
		if (GetWord(%string, %i) $= %word)
			return %i;
	}
	return -1;
}

%constantValue = getWordPos(%ColorT, "orange");
// Will return 1, as 'orange' is the second member of the vector
// (starting at 0)
#4
08/10/2009 (4:17 pm)
That's true; if you strictly want an enum, then multiple objects are probably not the way to go about it. It sounded in the original post like data would be attached to this information, but I could have been mistaken.

I would caution against StrStr() unless you take appropriate precautions to avoid common substrings, such as putting spaces around all your enum strings.
$enum = "Up UpRight Right DownRight Down DownLeft Left UpLeft";

%loc = StrStr($enum, "Down"); // Incorrect as far as enums are concerned -- gives the same answer as StrStr($enum, "DownRight")

$enum = " Up UpRight Right DownRight Down DownLeft Left UpLeft ";

%loc = StrStr($enum, " Down "); // Correct, but now you need spaces around everything

All in all, though, the array solution is probably best. Whether you do it with dynamic fields on one object or with a bunch of global variables, it's implemented as a hash lookup under the hood (at least for dynamic fields), which is probably as fast as you're going to get.
#5
08/10/2009 (7:11 pm)
Oh, you are right about StrStr(), havent thought about that.

The getWordPos() approach however, solves that issue, and is also more accurate with the enum concept of consecutive integers per constant.

All in all, interesting thread :)
#6
08/12/2009 (5:49 am)
Some great input. For the moment I'm working with an array of strings but I may switch to getWordPos since it better enforces consecutive integers as you point out.