Game Development Community

Passing arrays as functoin parameters

by Emil Diego · in Torque Game Engine · 03/21/2007 (8:35 am) · 7 replies

I'm getting some weird results when passing an array to a function.

I created a function to search all the elements of the array for a specific value and to return the index.

function GuiRTSTSCtrl::findInArray(%this, %xArray, %iArraySize, %xValue)
{

%iCount = 0;
while (%iCount < %iArraySize)
{
echo("xArray[" @ %iCount @ "] = " @ %xArray[%iCount]);
if (%xArray[%iCount] == %xValue)
{
// We found the value were looking for
echo("Found it at index : " @ %iCount);
return %iCount;
}
%iCount++;
}

echo("Didn't find it");
return -1;


}

I am populating the aray in a different function, but no matter what I try to do, %xArray does not contain any values.

Here is how i am calling the function
%iCurrentIndex = %this.findInArray( %iUnitIdArray, %iArraySize, %iValue )

I know the array implementation is different than C / C++ but is there something I am missing. Is it not possible to pass arrays as parameters?

Any help would be appreciated.

#1
03/21/2007 (9:36 am)
Nope...in Torquescript, arrays are not *really* arrays. This is from TDN:

Quote:Note: TorqueScript does not have support for arrays as such. Instead, the bracket notation is a shorthand for creating a variable name. $a[0] is synonymous with $a0, and $M[1,4] with $M1_4. You must not think of $a as the name of the array containing $a[5]. TorqueScript arrays provide the means for creating variable names dynamically: if $i is 5, then $a[$i] is synonymous with $a5.

Does this make sense?
#2
03/21/2007 (10:28 am)
Does that mean that the only way to access arrays from different functions is to make them global? Or is there a way to pass the contents of the entire array as a variable ?
#3
03/21/2007 (10:38 am)
Well, my understanding is that, at least in Torquescript, each individual element of an array is actually a separate variable itself. So if you wanted access to all of the contents of an array in a different function, you'd have to either pass all individual elements of the array to the function, or store it as a global. The latter is easier, but of course there are issues with accessing global variables in a multiplayer client-server architecture like Torque.

I am creating a single-player game only, so it's not really an issue for me to store arrays as global variables, and it works well in that respect.
#4
03/21/2007 (10:44 am)
Do a search for script array. There are a couple different resources about script arrays that are useful. There's also one specifically about passing arrays.
#5
03/21/2007 (10:50 am)
You can use a good ole ScriptObject.

%array = new ScriptObject();

%array.data[0] = "blah0";
%array.data[1] = "blah1";
%array.data[2] = "blah2";

Then you can just pass the %array and access the values on the object.
#6
03/21/2007 (10:54 am)
Ah, the ScriptObject. I hadn't really looked into that. Good to know.
#7
03/21/2007 (11:01 am)
Thanks for the feedback. I found a Array class that was created for the TGE engine. Here is the link. I'm testing it now and I'll get back to you on how it runs.