Game Development Community

Return Array

by Kevin James · in Torque Game Builder · 04/11/2005 (6:45 pm) · 5 replies

How do I return an array?

This doesn't seem to work:

%list = MakeArray();

function MakeArray()
{
     ...
    return %localarray;
}

I get nothing back.

About the author

Computer security, digital forensics, and platform jumper enthusiast. shells.myw3b.net/~syreal/


#1
04/11/2005 (7:40 pm)
Try %localarray[]
#2
04/11/2005 (8:32 pm)
Nope. It doesn't like that.
#3
04/11/2005 (8:53 pm)
Arrays don't pass friendly in TorqueScript... script objects do though

%array = new ScriptObject();

%array.contents[0] = "test";

in that you can pass %array
#4
04/12/2005 (1:44 pm)
Or you could pack & unpack the array:

function makearray(%numberofitems) {
   if(0 >= %numberofitems) return "";

   for(%i=0;%i<%numberofitems;%i++) {
       %array[%i] = %i;
   }

   %returnvalue = %array[0];
   for(%i=1;%i<%numberofitems;%i++) {
        %returnvalue = %returnvalue SPC %array[%i];
   }
   return %returnvalue;
}

function soicanuselocalvars() {
   %items = 10;
   %packedarray = makearray(%items);
   for(%i=0;%i<%items;%i++) {
      %unpackedarray[%i] = getWord(%packedarray, %i);
      echo(%unpackedarray[%i]);
   }
}

soicanuselocalvars();

It's a crappy solution, but it works. Or just do what the other guy does, and create an object then pass the handle around. Don't forget to delete it when you're done. The benefit of this way is that you don't have to worry about managing memory, as stuff gets automatically freed when it leaves scope. It's also not really significantly slower; remember that anything speed-significant probably needs either a different algorithm, or C++.

It sucks, get over it, I honestly recommend you find another way to do whatever it is you're trying to do without passing arrays around.

Gary (-;

PS It makes my fingers itch to even write this, but global variables will do the job, too.
#5
04/12/2005 (4:45 pm)
@Matt: How is the Scriptobject different/better than a SimSet?

@Gary: I have sold my soul to the global god more than once in this project, but I really have reasons for this to be local.