Game Development Community

How to call the function for all instances of the class?

by Dmitriy Stukalov · in Torque Game Builder · 09/05/2007 (3:12 am) · 2 replies

I've tryed the code below
// list of all scene objects
%scene_objects = SceneGraph2d.getSceneObjectList();
// loop through found objects
for (%i = 0; %i < getWordCount(%scene_objects); %i++)
{
   %current_object = getWord(%scene_objects, %i);
   if (%current_object.class $= "MyClass") %current_object.MyFunction();
}

I don't like the loop through all scene objects. Is there any possibility to make the code more efficive?

#1
09/05/2007 (6:47 am)
I personally do it this way :
function myclass::onlevelloaded(%this)
{
	if(!isobject(mysimset))
	  new simset(mysimset) {};
	
	mysimset.add(%this);
}


function do_function_for_all_myclass()
{
	%mysimsetcount = mysimset.getcount();
	for ( %i = 0; %i < %mysimsetcount; %i++ )
	{
		%currentobject = mysimset.getobject(%i);
		%currentobject.MyFunction();
	}
}
As I think about it, this is a really basic need and yet undocumented in the base tutorials or documentation. Something should be done.
#2
09/05/2007 (9:52 pm)
Great idea Benjamin.

This is a good example of the "thinking in TGB" approach :)

Thank you.