Game Development Community

Array/scripting help?

by 2dGamer · in Torque 2D Beginner · 02/06/2014 (8:11 am) · 11 replies

I'm not really finding anything that explains array creation at least in the way I want to create one.

Instead of defining each variable in a array like so:

$tempArray[0] = 1;
$tempArray[1] = 2;
$tempArray[2] = 3;

I'm wanting to do something like this:
PHP:
$tempArray = array(1,2,3);
C#:
List<int> tempArray = new List<int>();
functiony stuffs(){
tempArray.Add("evil chipmunks");
}

Basicly,I want a Dynamic array?

Where I can add and remove variables with ease.

#1
02/06/2014 (9:08 am)
I strongly suggest reading the following document, which covers variables, arrays, etc.

Wiki Guide
#2
02/06/2014 (9:20 am)
Yeah,no....

I'm not seeing any way to construct array without pre-defining all the variables in it.
#3
02/06/2014 (9:44 am)
Forget arrays, they're gimped. Use SimSets which are basically linked lists.
#4
02/06/2014 (9:47 am)
Could you be kind enough to give me a example of a SimSet?

Also ,There is no foreach in t2d?
#5
02/06/2014 (10:01 am)
%SimSet_Tree_Banana=new SimSet();

%ScriptObject_Banana=new ScriptObject();

%SimSet_Tree_Banana.add(%ScriptObject_Banana);

Check out my game for more code examples: github.com/practicing01/Dots_and_Crits
#6
02/06/2014 (1:02 pm)
That's because in TorqueScript arrays don't actually exist as:
$myVar[2] is the same as $myVar2.
$Hello["World"] is the same as $HelloWorld.
$Matrix[2,3] is same as $Matrix2_3.

So the [] operator acts as an accessor to manipulate and use on-the-fly variable names.

Also note that $ sigil means global static variable and % sigil means temporary variable that is normally used in local scope such as in function bodies and function definition parameter variables.
#7
02/06/2014 (2:46 pm)
SimSets contain objects though - if you just want to store integer values or strings they're sub-optimal. You have to create objects and store the values in fields on those objects (like the ScriptObject example) - which would be cool if we're keeping track of large collections of information (like rpg character stats/inventory).

There is no auto-population of arrays in TorqueScript - but it's not crazy difficult to write an init function:
function initArray(%arrayName, %size, %value, %increment)
{
    eval(%arrayName@"="";");
    for(%i = 0; %i < %size; %i++)
    {
        if(%increment)
            eval(%arrayName@"["@%i@"]="@%i@";");
        else
            eval(%arrayName@"["@%i@"]=0;");
    }
    eval("return "@%arrayName@";");
}

// test
initArray("$array", 10, 0, true);
for(%i = 0; %i < 10; %i++)
    echo(" -- " @ $array[%i]);

Weird, but it works....

<edit>
Sort of - really only works when creating global arrays.
#8
02/06/2014 (2:48 pm)
Weird how this type of manipulation quickly becomes second nature after a while of working with Torque.
#9
02/06/2014 (2:50 pm)
No kidding...

You should have seen the 3SS Tower Defense Wave Editor scripts.
#10
02/06/2014 (9:08 pm)
I came up with my own solution before practicing responded...

function pick(%count){
%winningNumbers = new SimSet();
for(%i = 0;%i < %count; %i++){
%rand = getRandom(1,80);
%number = new ScriptObject(%rand){
internalName = %rand;
};
if(!%winningNumbers.findObjectByInternalName(%number.getName())){
%winningNumbers.Add(%number);
}else{
%i--;
}
}
%Picked = "";
for(%ii = 0; %ii < %count; %ii++) 
{
%tNum = %winningNumbers.getObject(%ii);
if(%ii > 0){
    %Picked = %Picked @  "," @ %tNum.getName();
    }else{
    %Picked = %tNum.getName();
    }
}
echo(%Picked);
}

I'm already missing c#:

static void pick(int count)
        {
            List<int> winningNumbers = new List<int>();
            for (int i = 0; i < count; i++)
            {
                int rand = new Random().Next(1, 80);
                if (!winningNumbers.Contains(rand))
                {
                    winningNumbers.Add(rand);
                }
                else
                {
                    i--;
                }
            }
            string Picked = "";
            int Squirrel = 0;
            foreach (int i in winningNumbers)
            {
                if (Squirrel > 0) {
                    Picked += "," + i.ToString();
                }else{
                    Picked = i.ToString();
                }
                Squirrel++;
            }
            Console.WriteLine(Picked);
        }
#11
02/06/2014 (11:02 pm)
<shrug> C#, C++, Lua, TorqueScript, Assembly ... whatever gets the job done....