Game Development Community

Calling Methods in Behaviors

by Scott Knowles · in Torque Game Builder · 01/04/2009 (8:42 am) · 1 replies

In the Fish Game Behavior tutorial there is a situation where one behavior's onCollision method calls a function defined in another behavior.

To be clearer :
Object A (class Fish) has a Behavior called fishHealthManagementBehavior.
fishHealthManagementBehavior has the method :

function fishHealthManagementBehavior::updateHealth(%this, %modifier)
{
// modify our health based on the modifier
%this.health += %modifier;

// lets clamp the values between the min health and the max health
if(%this.health > %this.maxHealth)
{
%this.health = %this.maxHealth;
}
else if(%this.health < %this.minHealth)
{
%this.health = %this.minHealth;
}

// call the update size method
%this.updateSize();
}


Object B (class Bubble) has a Behavior called fishModifyHealthBehavior

function fishModifyHealthBehavior::onCollision(%this, %dstObj, %srcRef, %dstRef, %time, %normal, %contacts, %points)
{
// lets update the health of the object we are colliding with
if(%dstObj.hasHealth)
{
%dstObj.updateHealth(%this.healthModifier);
%this.respawn();
}
}

So the %dstObj is that object A and auto-magically the fishManagementBehavior.updateHealth method is called?

Can you call a method defined in a behavior by calling that method on the owner object?

Can anyone give any more illumination to the rules around calling methods defined in Behaviors?

Thanks,

Scott Knowles

#1
01/04/2009 (10:30 am)
@Scott - It's pretty much as simple as you stated. If you create a behavior function, and add the behavior to an object, your object can call that behavior method.

The only time you are going to run into a problem is when your behavior returns a value, which is not going to work properly.