Game Development Community

Odd behavior with Behaviors?

by Caleb · in Torque 2D Beginner · 08/29/2013 (9:23 pm) · 5 replies

I'm having an issue with returning values from within a behavior from another behavior instance. Maybe I'm mistaken, but I am expecting the following to work:

if (!isObject(test1Behavior)){
	%template = new BehaviorTemplate(test1Behavior);
	
	%template.friendlyName = "Test1 Behavior";
	%template.behaviorType = "Test1 Behavior";
	%template.description  = "Test1 Behavior";
}
  
if (!isObject(test2Behavior)){
	
	%template = new BehaviorTemplate(test2Behavior);
	
	%template.friendlyName = "Test2 Behavior";
	%template.behaviorType = "Test2 Behavior";
	%template.description  = "Test2 Behavior";
}
  
function test1Behavior::ReceiveThenSend( %this, %string ){
	echo("Behavior 2 Sent: "@%string);
	return %string;
}

function test2Behavior::onBehaviorAdd( %this ){
	%this.schedule(1000, SendThenReceive);
}

function test2Behavior::SendThenReceive( %this ){
	%string = %this.owner.ReceiveThenSend("Success");
	echo("Behavior 1 Returned: "@%string);
	%this.schedule(1000, SendThenReceive);
}

My output looks like this every second:

Behavior 2 Sent: Success
Behavior 1 Returned:


#1
08/29/2013 (9:44 pm)
Not sure why the code above doesn't work.

However, to 'properly' send data from one behavior from another (on the same object), I'd suggest checking out the Behavior connections

github.com/GarageGames/Torque2D/wiki/Behavior-Connections
#2
08/29/2013 (10:22 pm)
You can try callOnBehaviors():

%string = %this.owner.callOnBehaviors("ReceiveThenSend", "Success");
#3
08/29/2013 (10:53 pm)
Thanks guys.

Richard your suggestion worked.

%string = %this.owner.callOnBehaviors("ReceiveThenSend", "Success");

I was aware of behavior connections, but this is still useful and expected, at least to me. I'll use the callOnBehaviors method from now on when needed.

It might also be worth noting that the call in the behavior tutorial uses the same method as I used above to pass data between instances, only I don't believe any data was return. Still might be useful to change it to callonBehaviors.

Thanks again for the replies!
#4
08/30/2013 (1:24 am)
Does anyone have a concrete example of where a behavior connection would be useful/simplify code?

I use behaviors almost exclusively in my game prototypes but have yet to find a practical use for connections.
#5
08/30/2013 (9:10 am)
Behavior connections were a feature we added during the development of 3 Step Studio to allow nifty "automatic" triggering of other behavior methods on the same object. The projectile behavior raised an impact signal when its onCollision handler was called that would trigger a dust-puff behavior, a sound effect behavior, an animation state change behavior and a damage dealing behavior.

The main use for all of that is to allow you to avoid violating the "DRY" principle (Don't Repeat Yourself, for those who don't know). Each behavior performs precisely one task but is set up to raise and receive signals to allow predictable interactions with other behaviors on the same object. Our projectile ended up carrying something like 17 behavior instances to accommodate all of its states for sound, animations, damage, launchability, trails in the air, self-deletion, etc, all connected as needed to make it work seamlessly.

What we never got finished was an actual editor for making these connections - we only got as far as duplicating template objects that carried the behaviors and connections already.

There is nothing here that can't be accomplished with callOnBehaviors(), but we were really going for a way to avoid brittle dependencies if I recall the conversations correctly.

Something else to note - if these behaviors are on the same object you can always pass data by placing it in a field on the object:
if (!isObject(test1Behavior)){  
    %template = new BehaviorTemplate(test1Behavior);  
      
    %template.friendlyName = "Test1 Behavior";  
    %template.behaviorType = "Test1 Behavior";  
    %template.description  = "Test1 Behavior";  
}  
    
if (!isObject(test2Behavior)){  
      
    %template = new BehaviorTemplate(test2Behavior);  
      
    %template.friendlyName = "Test2 Behavior";  
    %template.behaviorType = "Test2 Behavior";  
    %template.description  = "Test2 Behavior";  
}  
    
function test1Behavior::ReceiveThenSend( %this, %string ){  
    echo("Behavior 2 Sent: "@%string);  
    %this.owner.testData = %string;  // here...
}  
  
function test2Behavior::onBehaviorAdd( %this ){  
    %this.schedule(1000, SendThenReceive);  
}  
  
function test2Behavior::SendThenReceive( %this ){  
    %string = %this.owner.ReceiveThenSend("Success");  
    echo("Behavior 1 Returned: "@%this.owner.testData);  // and here...
    %this.schedule(1000, SendThenReceive);  
}