Game Development Community

Basic Array Question

by Martin Mc · in Torque Game Builder · 04/20/2005 (3:44 pm) · 11 replies

Hi all,

I'm fairly new here and am just getting to grips with TorqueScript. I'm feeling a bit on the stupid side cos there's loads of basic things that I take for granted in other programming languages that I need to relearn for TS. Nothing major, just syntax and whatnot. Anyway, enough of the ramble and on with the question :->

I wanted to do something like the following, but torque doesn't seem to like it. can anyone explain how to do what I'm attempting here:

%myArray[0] = 10;
%myArray[1] = 20;
%myArray[2] = 30;

$example_object = new SimObject() 
{
    thisArray = %myArray;
};

Then if I do this:

echo($example_object.thisArray[0]);

I get nothing out.

Can anyone help me out? basically i want to assign an array to a field of an object. I can work round it by doing something like the following:

$example_object = new SimObject() 
{
    thisArray[0] = %myArray[0];
    thisArray[1] = %myArray[1];
    thisArray[2] = %myArray[2];
};

But I would rather not have to do this. On a related note, can anyone tell me how you find the size of an array in TS?

Sorry for the basic-ness of my questions, but I'm finding it difficult to find what i'm looking for in the documentation.

Cheers,
Martin

#1
04/20/2005 (4:23 pm)
TorqueScript doesn't really have a notion of arrays, just variables. The array references $someArray[5] or $someOtherArray[Fred] or $yetAnotherArray["Joe"] are just syntactic sugar for $someArray5, $someOtherArrayFred, and $yetAnotherArrayJoe respectively.

This means you can't pass arrays around like objects and that you have to keep track of their size manually, but get the benefit of fast, cheap associative array-like functionality.

Take a look into the SimSet or SimGroup classes if you want to pass collections of objects around more easily.

Edit: Typoes & grammar fixes. Becoz I type gud first time.
#2
04/20/2005 (4:47 pm)
Nice one Matt. That explains a few things for me. Thanks a lot.
#3
04/20/2005 (6:04 pm)
Quote:TorqueScript doesn't really have a notion of arrays, just variables.

Wait. I thought TScript had the concept of objects of some kind. If so, can't you just allocate up a blank object, add some fields to it in an array-like format, and have a functioning array?
#4
04/20/2005 (6:22 pm)
@Smaug: Good point.

@Everyone: This was most recently discussed [with code solutions] here:
www.garagegames.com/mg/forums/result.thread.php?qt=28819

Gary (-;
#5
04/20/2005 (6:24 pm)
@Smaug: That's what the ScriptObject is for.

function MakeRankArray()
{
    $RankList= new ScriptObject();

    for (%col=0;%col<$Cols;%col++){
        for(%row=0;%row<$Rows;%row++){
            $RankList.Rank[%col,%row]=$Team1Guy[%col,%row].rank;
        }
    }
}
#6
04/20/2005 (6:28 pm)
Here's another way that I use ScriptObject:

function MakeRankList()
{

    %List= new ScriptObject();
    %list.count=0;
    MakeRankItem(%List,%List.count,1,"-.8 -.6");
    MakeRankItem(%List,%List.count,2,"-.6 -.6");
    MakeRankItem(%List,%List.count,3,"-.4 -.6");
    MakeRankItem(%List,%List.count,3,"-.2 -.6");
    MakeRankItem(%List,%List.count,4,"0 -.6");
    MakeRankItem(%List,%List.count,4,"0.2 -.6");
    MakeRankItem(%List,%List.count,4,"0.4 -.6");
    MakeRankItem(%List,%List.count,5,"-.8 -.25");
    MakeRankItem(%List,%List.count,5,"-.6 -.25");
    MakeRankItem(%List,%List.count,5,"-.4 -.25");
    MakeRankItem(%List,%List.count,6,"-.2 -.25");
    MakeRankItem(%List,%List.count,6,"0 -.25");
    MakeRankItem(%List,%List.count,6,"0.2 -.25");
    MakeRankItem(%List,%List.count,6,"0.4 -.25");
    MakeRankItem(%List,%List.count,7,"-.8 0.1");
    MakeRankItem(%List,%List.count,7,"-.6 0.1");
    MakeRankItem(%List,%List.count,7,"-.4 0.1");
    MakeRankItem(%List,%List.count,7,"-.2 0.1");
    MakeRankItem(%List,%List.count,$MinerRank,"0 0.1");
    MakeRankItem(%List,%List.count,$MinerRank,"0.2 0.1");
    MakeRankItem(%List,%List.count,$MinerRank,"0.4 0.1");
    MakeRankItem(%List,%List.count,$MinerRank,"0.6 0.1");
    MakeRankItem(%List,%List.count,$BombRank,"-.8 0.45");
    MakeRankItem(%List,%List.count,$BombRank,"-.6 0.45");
    MakeRankItem(%List,%List.count,$BombRank,"-.4 0.45");
    MakeRankItem(%List,%List.count,$BombRank,"-.2 0.45");
    MakeRankItem(%List,%List.count,$SpyRank,"0.2 0.45");
    MakeRankItem(%List,%List.count,$FlagRank,"0.5 0.45");

    return %List;
}      

function MakeRankItem(%ranklist,%index,%rank,%coord)
{
    %rankList.rank[%index]=%rank;
    %rankList.coord[%index]=%coord;
    %rankList.count++;
}
#7
04/21/2005 (9:27 am)
Thanks everyone for your solutions.

A couple more question then:

What is a ScriptObject? Is it basically just an empty class that we can add our dynamic fields onto?
Where could I find reference documentation on the various different objects available to me (e.g. ScriptObject)?

Cheers,
Martin
#8
04/21/2005 (9:39 am)
Martin: Yeah, just an empty class. Its a non-visual object. Its what I use to pass arrays from function to function. There's also something called SimSet. The reference doc that comes with the SDK has all of the other available objects. Not sure if that is exhaustive tho...
#9
04/21/2005 (9:51 am)
Cheers Kevin.

What reference doc are you referring to? The one I have that came with T2D (title: Torque 2D Reference Guide) only seems to cover the fx* cobjects and not more basic stuff like SimSet or ScriptObject.

Can you point me in the right direction?

Martin
#10
04/21/2005 (10:02 am)
Sorry, the Reference Guide is all we got. The next thing to do is search the forums, but then you have to know what you are searching for. ScriptObject, SimObject, SimSet, SimGroup. I don't know what else is out there.

Oh, and there's the new "TorqueScript Documentation Links" that comes with 1_0_2 release.
#11
04/21/2005 (10:28 am)
Magic mate, cheers for that. I'll give searching a shot. I've actually had my original question answered by everyone on this thread, but I'm always thirsty for more :->