Game Development Community

Help with Array's (learning)

by Katelan Moye · in Torque Game Engine · 01/14/2008 (1:03 pm) · 5 replies

Ok, so I am reading through this guide: http://tdn.garagegames.com/wiki/TorqueScript


I am kinda stuck on learning the syntax of arrays.... For the sake of ease, I am going to paste the portion of the guide here below so you guys don't have to click the link and look for it.

Quote:Arrays:

$MyArray[n] (Single-dimension)
$MyMultiArray[m,n] (Multi-dimension)
$MyMultiArray[m_n] (Multi-dimension)

TorqueScript is extremely flexible with arrays. Many scripting languages are more flexible with arrays than compiled languages like C++, but TorqueScript is even more flexible than most scripting languages. All this flexibility can get confusing, and deciphering how TorqueScript handles arrays sometimes trips people up.

For example, it is a common misconception that TorqueScript does not support multi-dimensional arrays. This is not true, as the code in the box above shows. The reason many people get confused about multi-dimensional arrays in TorqueScript is that there are multiple-ways to address the array. As you can see, you can separate the dimension indices (m and n, in the above) with commas, or even underscores

Below are two more interesting features of the way TorqueScript handles arrays:

1) $a and $a[0] are separate and distinct variables. It is advisable to use some kind of naming convention so it is clear when you are dealing with an array.

$a = 5;
$a[0] = 6;
echo("$a == ", $a);
echo("$a[0] == ", $a[0]);

Run this code, and you will see that $a and $a[0] are distinct in the output.

2) $MyArray0 and $MyArray[0] are the same. It may be surprising, but TorqueScript allows you to access array indices without using the common bracket [] syntax. Again, it is advisable to use a naming convention to identify arrays in your code.

$aryMyArray[0] = "slot 0";
echo ($aryMyArray0);

$aryMyArray[1] = "slot 1";
echo ($aryMyArray1);

$aryMyArray[0,0] = "slot 0-0";
echo ($aryMyArray0_0);

As the output from the above code illustrates, TorqueScript treats bracketed indices the same as appended indices.

So under example #1 how is this an example of an array? It looks to me as an example of defining a global variable.... Thus, $a = 5; How is this an array?!

Also, under example #1

Why not just type "echo($a);" I don't understand why you are supposed to type "echo("$a == ", $a);" esspecially when if you type both in the console, you will get the same output. Which in this case is "5"...


Now, under example #2

$aryMyArray[1] = "slot 1";

What makes this statement an array? Is it "$ary"? This looks A LOT to me like another example of a global variable...

What are the differences in syntax between global variables and array's? I see no difference.... If I understood this, this would help me a lot

Can someone help?

Thanks ahead of time!

-Pete

#1
01/14/2008 (1:37 pm)
Pete. I tend to use other containers in TorqueScript, but here are some answers to your questions:

1. Typing echo("$a == ", $a); is just for your benefit. In the console you will see "$a == 5". It's easier to scan a console log or spammed console and find a full sentence or notifier, instead of just a single number.

As for the rest, you are focusing a little too much on the '$' and less on the brackets. By using the brackets, that is what makes an object an array:

function arrayTest()
{
    $globalArray = "";  // <== This object will exist even when the function is complete
    %localArray = "";   // <== This object will no longer exist when the function is complete

    for(%i = 0; %i < 10; %i++)
    {
         $globalArray[%i] = %i;   // <== Fills the array with #s 1-10

         %localArray[%i] = %i;   // <== Fills the array with #s 1-10
     }
}

Alternatively:
function arrayTestTwo()
{
    $globalArray[0] = "hello";
    $globalArray[1] = "world";
     
     echo($globalArray[0] @ " " @ $globalArray[1]);

    %localArray[1] = 2;
    %localArray[0] = 1;
}

Like other scripting languages, TS treats nearly everything as a string object. Containing and parsing these objects is a matter of execution, and with slightly less emphasis on declaration. The one exception is found when dealing with default arrays.

I know it seems a little confusing at first. I tend to use an Array object (from a resource) or simSets. However, a TS array still comes in handy now and then.


Does that help a little?
#2
01/14/2008 (1:53 pm)
This helps a lot, thanks for posting. I think I understand it now, I imagine that once I have to actually use array's in my scripts that I will become more familiar with them.

I do believe I understand the syntax now...

$ThisCanSayAnything[1] = 2;

That's an array specifically because of the brackets....

$ThisCanSayAnything = 1;

This is a variable....

Am I right?
#3
01/14/2008 (1:54 pm)
Right enough to use it properly =)
#4
01/14/2008 (4:57 pm)
There's also the array resource.
#5
01/14/2008 (7:30 pm)
@ Michael, thanks!

@ Orion, yeah... I looked at that resource.... Way above my head to even come close to understand what it is, what it does and how I can benefit from using it... I'll get there =) Thanks for linking it though.

-Pete