Game Development Community

Behavior Trigger callback from parents onCollision function.

by Peterjohn Griffiths · in Torque Game Builder · 04/15/2011 (11:51 am) · 3 replies

From script in a Behavior Trigger, is there a way to set a callback from the parents onCollision function.
Example:
I have an image(Parent), I mount a Trigger to it and add my behavior.
In most of the functions in the behavior, I can get a hook to the parent like so;
%myMountedParent = %this.owner.getMountedParent();

In the Behavior code, I would like to be able to do things when the parents onCollision function is called.

function %myMountedParent::onCollision(%ourObject, %theirObject, %ourRef, %theirRef, %time, %normal, %contacts, %points)
{
//MyCode Here.
}

I want to do this without having to precode the parent::onCollision function to call the behavior::onCollision function so I can just add this behavior and trigger to any image and have the parent::onCollision function auto call the behavior::onCollision.

#1
04/15/2011 (12:56 pm)
I'm not positive about all of this, but it's worth a shot...

function t2dSceneObject::onCollision( %this, %other, %ourRef, ...etc... )
{
  %children = %this.getAllMountedChildren();
  %cChildren = getWordCount( %children );
  for( %i = 0; %i < %cChildren; %i++ )
  {
    %child = getWord( %children, %i );
    %b = %child.getBehavior( "yourBehavior" );
    if( isObject( %b ) )
    {
      %b.onCollision( %this, %other, %ourRef, ... );
    }
  }
}

When you add the behavior to any image, add collision detection to the parent. This generic function will(???) trigger. This function will look for children with your specific behavior and will transfer the collision call to it.

(Boy, there's a lot of guessing here! *HEHE!*)
#2
04/15/2011 (2:43 pm)
Don't think that will work as it is, but what an idea. :D
The stepping through the children from the t2dSceneObject. Fantastic idea.
Will have a crack at it, now I have some direction and let you know the outcome.
#3
04/16/2011 (6:29 am)
This didn't work. :(
But was a good idea.
On the plus side, while looking through the code for t2dSceneObject, I realised, all I had to do is copy the collision details from the parent to the trigger and the trigger would automatically trigger its own onCollision function at the same time as the parent. I assume the onCollision function will still happen, even though the trigger is a child of another object?.
Not perfect, but a good workaround.
I will give it a go and see if it works.