Game Development Community

How to pass a list of objects to a script function?

by Eric Preston · in Torque Game Builder · 08/23/2005 (1:42 pm) · 4 replies

I want to pass an indexed array to a script function, so that I can do something like this example:

function sceneObject2D::total(%this, %indexedArray)
{
%sum=0.

for(%i=0; %i {

%sum= %sum+%indexedArray(%i);

}

return sum;
}

The array I really want to use is an array of sceneObjects...%obj[0], %obj[1],...etc, don't know if that matters.


Anybody know how to do this in Torque script?

Any help appreciated.

Eric

#1
08/23/2005 (1:59 pm)
You mean like this?

function total(%indexedArray)
{
   %objs = getWordCount(%indexedArray);
   for(%i = 0; %i<objs; %i++)
  {
     %objCur = getWord(%indexedArray, %i);
     %sum = %sum + %objCur;
  }
  return %sum;
}

%sum = total("1 2 3 4 5 6");

The fact that the "array" is composed of scene objects is unimportant.
They could be numbers or also objects, for instance:

function total(%indexedArray)
{
   %objs = getWordCount(%indexedArray);
   for(%i = 0; %i<objs; %i++)
  {
     %objCur = getWord(%indexedArray, %i);
     %sum = %sum + %objCur.getAnimationFrame();
  }
  return %sum;
}

%obj1 = new fxAnimatedSprite2D();
%obj2 = new fxAnimatedSprite2D();
%obj3 = new fxAnimatedSprite2D();
%sum = total(%obj1 SPC %obj2 SPC %obj3);
#2
08/23/2005 (2:13 pm)
The easiest way would be to add all of the sceneObjects to a SimGroup or SimSet and then pass the group to the function. Actually, depending on what you're using it for, you may want to forget the array and just use one of the grouping structures. They work similarly to an array but are much more robust.

In case you are unfamiliar with SimSets and SimGroups, creating, adding to, and traversing a works like this:
// Create the Group
%group = new SimSet(SimSetName) {
   // Dynamic fields here
}

// You can access the group by id (%group) or globally by name (SimSetName). You
// don't need to specify both in the definition, but you can.

// Add an object to the group. %obj is assumed to be a simObject (sceneObject works)
// of some kind.
%group.add(%obj1);
// or
SimSetName.add(%obj2);

// Remove an object
%group.remove(%obj1);

// Traverse the Set
%count = %group.getCount();

for (%i = 0; %i < %count; %i++)
{
   %obj = %group.getObject(%i);
   // Do stuff to %obj
}
The difference between a SimSet and a SimGroup is an object can only belong to one SimGroup, or multiple SimSets.
#3
08/23/2005 (3:29 pm)
Adam,

That is exactly what I was trying to do. It works great. Many thanks for the quick reply.

Eric

Mathew,

Many thanks for your replies. I guess that since I'm really working with scene objects in the list, Adam's approach is going to work better for me. I learned a lot from both replies. Many thanks,

Eric
#4
09/29/2005 (3:12 pm)
Thankyou guys, I was wondering how to delete all the enmies of my game :)

%size = SimSetEnemiesGroup.getCount ();

		for (%k = 0; %k < %size; %k++)
		{
		   %obj = SimSetEnemiesGroup.getObject (%k);
		   %obj.safeDelete ();
		}