Call a method on all instances of a class?
by John Vanderbeck · in iTorque 2D · 05/06/2012 (12:32 pm) · 6 replies
Let's say I have a screen full of boxes, and all the boxes have the class "boxClass". The user taps on one, and that instances onTouchDown() gets fired. Now I want to hide all the other boxes except the one touched.
How can I go about hiding every instance of boxClass without actually having variables for each instance?
I can't seem to figure out how to go about it without actually knowing the instances ahead of time.
How can I go about hiding every instance of boxClass without actually having variables for each instance?
I can't seem to figure out how to go about it without actually knowing the instances ahead of time.
#2
When you check the scripts that come with TGE, TGEA, T3D, T2D or iT2D you will see it being used pretty regularly (even level datablocks base on it :))
TorqueScript has no true OOP concept which results in various limitation on these aspects.
The pseudo OOP that TS has are a consequence of the datablocks and it was primarily designed for that and when working with datablocks you would always need to use SimGroups or SimSets to handle 'per instance' things or even keep them accessable without a million variables or 'torquescript arrays' ($myArray0, $myArray1 etc which is the same as $myArray[0], ...) and not a single global named instance
05/06/2012 (1:52 pm)
Thats indeed how you would do it and is the best way to handle it beside going to the C/C++ layer.When you check the scripts that come with TGE, TGEA, T3D, T2D or iT2D you will see it being used pretty regularly (even level datablocks base on it :))
TorqueScript has no true OOP concept which results in various limitation on these aspects.
The pseudo OOP that TS has are a consequence of the datablocks and it was primarily designed for that and when working with datablocks you would always need to use SimGroups or SimSets to handle 'per instance' things or even keep them accessable without a million variables or 'torquescript arrays' ($myArray0, $myArray1 etc which is the same as $myArray[0], ...) and not a single global named instance
#3
05/06/2012 (1:55 pm)
Cool. Works for me then :)
#4
05/06/2012 (10:50 pm)
@John - Here's another way to do it, although your way is perfectly fine.%sceneGraph = sceneWindow2d.getSceneGraph();
%totalObjectsInScene = %sceneGraph.getCount();
%i = 0;
while (%i < %totalObjectsInScene)
{
%object = %sceneGraph.getObject(%i);
%objectClassName = %object.getClassNameSpace();
if (%objectClassName $= "boxClass")
{
%object.setVisible(false);
}
%i++;
}
#5
05/11/2012 (12:07 pm)
Quote:What i'm doing right now is leveraging the built in ::onAdd method to add each object of that class to a SimSet and then traversing that SimSet when I need to call a method on every instance.Even though the thread is resolved, I thought I would provide my opinion as one of the engineers. This is my preferred and recommended approach. If there are going to be a lot of objects, I have gone as far as modifying the source code to handle this solution.
#6
Right now i'm avoiding engine changes as much as possible, so that I can just slip in 1.5.1 without too much hassle.
When I get closer to the end of June and ship date, i'll re-assess performance and move things into engine code as neccesary.
05/11/2012 (3:51 pm)
Glad to know i'm on the right track!Right now i'm avoiding engine changes as much as possible, so that I can just slip in 1.5.1 without too much hassle.
When I get closer to the end of June and ship date, i'll re-assess performance and move things into engine code as neccesary.
Torque Owner John Vanderbeck
VanderGames
What i'm doing right now is leveraging the built in ::onAdd method to add each object of that class to a SimSet and then traversing that SimSet when I need to call a method on every instance.