Game Development Community

How can we return an Array?

by Tejaswin · in Torque Game Builder · 08/10/2010 (10:14 am) · 8 replies

Hi,

Can someone please tell me how to we return an Array from one function and get the array in other function and use the values?

Thanks in advance,
Tejaswin.

#1
08/10/2010 (11:47 am)
It depends, are you talking about C++ or torquescript?

In C++,
first declare a function like so:
int someclass::foobar(int []);
Then you pass the array as a parameter of the fuction
int array[10]; // array of 10 integers
someclass obj;
obj.foobar(array); // arrays are passed by reference automatically
//process array here

In torquescript,
all variables are actually strings, that means there are no real arrays.
So to return a collection of variables:
function foobar(%this)
{
  %val1 = 1;
  %val1 = 2;
  %val1 = 3;

  return   %val1 SPC %val2 SPC %val3;
}
To use the "array"
%array = foobar();
  %val1 = getWord(%array,0);
  %val2 = getWord(%array,1);
  %val3 = getWord(%array,2);


Hope this helps :-)
#2
08/10/2010 (12:11 pm)
Any time I need an array in script, I use a simset.
#3
08/10/2010 (12:54 pm)
Personally I try to do all my array processing in C++. :-)
#4
08/10/2010 (1:08 pm)
How much of a hassle is it for you when a new version of Torque comes out (assuming you upgrade)?
#5
08/10/2010 (1:11 pm)
There's also an ArrayObject that you might want to try.

Depending on what you need, a SimSet could work. Objects that belong to a SimSet can belong to other SimSets as well. See if this is desirable.

Objects within a SimGroup however can only belong to that one group at a time.
#6
08/10/2010 (1:27 pm)
I don't think the ArrayObject has made it into the stock TGB codebase yet. There is a resource out there, which the one that is in T3D is based on though.
#7
08/10/2010 (2:45 pm)
Kevin:
Ya it's a bit of a pain, but if you do any iPhone development you will quickly learn why heavy processing in script is not a good idea. :-)
#8
08/11/2010 (4:00 am)
Thank you guys for the replies, I will try to use Simset.