Game Development Community

Behavior methods have problems returning value

by Dmitry Yanushkevich · in Technical Issues · 06/02/2011 (2:55 am) · 1 replies

Gentlemen,

I've bumped into a strange case here. Let me describe the environment.

I'm working with behaviors to implement custom objects logic, and here are, say, the following behaviors:

* TakeDamageBehavior -- almost 1:1 copied from TGB tutorials/examples
* CloudBehavior -- AI behavior

The code is as follows:
// Called from somewhere else in the code.
function TakeDamageBehavior::takeDamage( %this, %color, %type, %damage )
{
   echo( "TakeDamageBehavior::takeDamage(): hit by" SPC %color @ %type );
   
   if( %this.owner.onTakeDamage( %color, %type, %damage ) )
   {
      // Someone else processed this for us, do nothing else.
      return;
   }
   // The rest is cut.
}

// Dummy to avoid undefined behavior and errors.
function t2dSceneObject::onTakeDamage( %this, %color, %type, %damage )
{
   return false;
}

The general idea was to have a method that will do custom processing before TakeDamageBehavior::takeDamage() does, if needed. Indeed, in cloud.cs I have:
// Custom processing of this event by cloud AI
function CloudBehavior::onTakeDamage( %this, %color, %type, %damage )
{
   echo( "CloudBehavior::onTakeDamage(), state " @ %this.state );
   
   switch$( %this.state )
   {
      case "WaitForPlayer":
         %this.changeState( "RunAway" );
         return true;
      // The rest is cut.
   }         
}

What I see as a result, the code after if statement in TakeDamageBehavior::takeDamage() still runs pretty fine even though true is (always, in fact) returned from CloudBehavior::onTakeDamage(). What's more, when stepped through by Torsion, I see t2dSceneObject::onTakeDamage() getting executed returning false. What is more interesting, removing t2dSceneObject::onTakeDamage() definition does NOT solve the issue -- the correct value is still not passed through. Looking through the source code, I see that the return value is just discarded (behaviorComponent.cpp:158).

Is this intended and I am just misusing this mechanism? If so, what is the proper way of doing this kind of work (returning values via object->behavior calls)?

About the author

Worked here and there, did both game and business development. Finally decided to start own business, and here we are -- a cutie Cat Paw Studio has been born! :D


#1
01/21/2013 (12:38 pm)
I'm having this issue as well. If I call behavior's method and expect it to return a value I just get...nothing. I thought maybe this thread might help with the issue but that's an unrelated issue apparently. Is there any way to accomplish this?

Here's some example code. The behavior's function:
function ElevatorPlatformBehavior::getTriggerName(%this)
{
   return %this.TriggerName;
}

And then elsewhere I'm doing something like:
echo( %ourObject.ElevatorObject.getTriggerName() );

Where %ourObject.ElevatorObject is assigned the behavior. The getTriggerName function calls correctly so I know that code is being run and I can echo %this.TriggerName and see the code correctly but there's nothing being called from that function.

What do I need to change in the source to allow this?