Game Development Community

Passing arrays to functions

by Gerald Fishel · in Torque Game Builder · 07/08/2008 (3:37 am) · 3 replies

Hi all,

I need to be able to pass an array to a function for some recursive calculations that I'm doing. As long as I've used Torque I never tried this before in script, and now obviously I discover that it doesn't work. I came across a resource that was supposed to explain how it can be done, but that resource is apparently broken.

I am trying hard to avoid having to write any C++ code for the prototyping of this project because I'm collaborating with somebody who does not have a TGB Pro license. It will pretty soon reach the point where it would have been more cost effective for me to just buy him a pro license and do it in C++, but I think it's still something that would be nice to know how to handle in script.

I guess theoretically I could convert the array to a space-delimited string and then parse it back to an array inside the function, but I have a feeling that would end up making it too slow for my purposes.

So does anybody know an elegant way around this?


Thanks,
Gerald

#1
07/08/2008 (4:01 am)
Space delimited string is one option, you can also use your own delimiter with nextToken.

Another alternative is to store the array in a dynamic field within a ScriptObject, then pass the ScriptObject to the function instead.

%someObj = new ScriptObject();
  %someObj.array[0] = "hi";
  %someObj.array[1] = "lo";

  someFunction( %someObj );

Then in someFunction you'd just access the array as %someObj.array[i]
#2
07/08/2008 (4:25 am)
Aha, thanks Gary. The ScriptObject method is perfect.
#3
07/15/2008 (5:50 pm)
I just posted a TScriptArray resource here:
www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=15077

It wraps this same idea with some helper functions you might find useful.