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.
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.
#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
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.
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
Then you can just pass the %array and access the values on the object.
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.
Torque 3D Owner Rubes
Does this make sense?