Game Development Community

Namespaces and methods

by WesTT · in Torque Game Builder · 08/17/2008 (6:17 am) · 4 replies

Ok, first I have to say that I have not given myself a great deal of exposure to inheritance and method overriding in TorqueScript. I didn't bother as I am led to believe that generally these features do not exist as part of the language - only as a rough approximation using namespaces, superclasses and the like.

However, I was wondering how this factors into behaviors? It would appear that if I have an object with a behavior attached:

Object -> SomeSpecialBehavior

and place a method in the behavior called "setEnabled" it appears to get called, and the %this parameter points to the correct owner object. So I tested it like so:

function TrainLevelSpawnBehavior::setEnabled(%this, %enabled)
{   
   %this.owner.enabled = %enabled;   
   
   if (!isEventPending(%this.spawnSchedule) && %enabled)
      %this.spawnSchedule = %this.schedule(%this.startWait * 1000, "doSpawn");   
}

And indeed, it appears to call the function, pass in the correct owner and schedule the function which also runs. Problem is, the object never appears to become enabled. This appears to be the case anyway as I check if the object is enabled in the scheduled function. The object calling "setEnabled" is a zoning object that enables all objects within it's bounds when that area is entered.

One of the tutorials leads me to believe that calls such as this are simply making their way up the namespace "hierarchy" such as: trainSpawnPointStage3 -> TrainSpawnClass -> t2dSceneObject -> BehaviorComponent -> DynamicConsoleMethodComponent -> SimComponent -> SimObject. Which makes sense.

I'm obviously not sure if this is actually possible, or should be done this way. It would however be a very convenient way of enabling the object and it's scheduler. Can anyone clarify?

Cheers
Wes

#1
08/17/2008 (6:23 am)
I should also say: placing a breakpoint after setting enabled in the supplied functions shows the parameter value of %enabled to be copied over to the owner object. It just appears not to be the case in the behaviors scheduled "doSpawn" function, where it appears to be 0/false.
#2
08/17/2008 (7:45 am)
You aren't passing %enabled as an argument to "doSpawn" in your schedule, is that the problem?
#3
08/17/2008 (8:09 am)
Hi James. I thought of doing that, and that would probably work, but I am more interested in learning more about exactly how Torque Script handles such cases on objects with behaviors. At the moment, I've simply changed the behaviors "setEnabled" to enable or disable the schedule based on the %enabled parameter, and this works well.
#4
01/01/2009 (10:27 am)
I am also interested in this situation.

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 calliung methods defined in Behaviors?

Thanks,

Scott Knowles