Game Development Community

Help with global variables

by Johnny Vo · in Torque Game Builder · 06/23/2009 (7:15 pm) · 3 replies

Hi, everyone. I'm stuck and am asking the forum for help. I want to write a function that would take in different global arrays. I know you can't pass in arrays but is there anyway around this. Here is what i'm trying to do.

function doSomething($global_variable)
{
echo($global_variable[0]);
echo($global_variable[1]);
}

//so i can do something like this.
doSomething($bullets);
doSomething($big_bullets);

thanks!

#1
06/24/2009 (12:43 am)

You can wrap the array in an object like so:

function doSomething( %obj )
{
   echo( %obj.array[ 0 ] );
   echo( %obj.array[ 1 ] );
}

$bullets = new ScriptObject()
{
   array[ 0 ] = "foobar";
   array[ 1 ] = "barfoo";
};
$bullets.array[ 2 ] = "zing";

doSomething( $bullets );

BTW, the argument in doSomething still is a local variable.

Add a "size" or "numItems" property so functions know how big the array is.
#2
06/24/2009 (12:44 am)

Forgot: if your arrays are supposed to contain SimObjects, SimSet and SimGroup are more appropriate. Both contain a set of SimObjects. SimGroup owns its containing SimObjects (i.e. they go down if the group does), SimSet doesn't.
#3
06/24/2009 (11:28 am)
Hey this is perfect. Thank you!