Game Development Community

pass data from one behavior to another

by Chris Dubiel · in Torque 2D Beginner · 10/31/2013 (12:08 am) · 8 replies

i'm trying to make an a link to the past (rpg) clone.

i'm using a behavior to control a player's movement and direction. but now i want another behavior to control a player's attacks. is there a way to access data inside another behavior? for example, the movement behavior stores the player's direction, and i want the attack behavior to know which direction to create a hitbox.

also, is this a good method of creating functionality? i almost think that i should just add all of this functionality in one big player class, as eventually i would need to check the player's currently equipped weapon to see how much damage the attack should do.

any thoughts would be helpful!

About the author

Recent Threads


#1
10/31/2013 (5:33 am)
If you want to stay down the path of using multiple behaviors to create a complex object, I'd suggest reading about behavior connections. When you make the connection, you will have access to the other behavior and its output when the signal is raised.
#2
10/31/2013 (10:09 am)
can you give an example of how this works? i've already figured out how to connect my two behaviors to call a method in one behavior from the other, but how can the behavior itself be accessed?
#3
10/31/2013 (10:22 am)
Did you connect them using the method described in the doc I posted? Or did you use your own way?
#4
10/31/2013 (10:30 am)
so i have two behaviors, movementBehavior and attackBehavior. the player object attaches both behaviors to itself, and creates the connection from the "startAttack" output on attackBehavior to the "disableMovement" input on movementBehavior. then, from attackBehavior, i can raise the output on itself (using %this.owner.Raise(%this, startAttack) that connects to the input on movementBehavior to tempararily stop movement while the player attacks. i also have a second connection to then connect endAttack to enableMovement.

is this not how i should be using connections?
#5
10/31/2013 (3:03 pm)
I never really got the hang of connections so I keep doing things the old way. Basically a sanity check if the object in question has a specific behavior and then to call a specific function from that behavior.

function SomeBehavior::kill(%this)
{
    %respawn = %this.owner.getBehavior("RespawnBehavior");

    if (isObject(%respawn))
        %respawn.flagForRespawn();
}
#6
11/01/2013 (8:41 am)
function InBehavior::Pong( %this, %fromBehavior, %fromOutput )
{
    echo( "Pong was called!" );
}

Notice that the function that is raised passes the behavior and some data, which you can use to pass the current direction.
#7
11/01/2013 (7:14 pm)
Chris,

Also, you might want to you use callOnBehaviors method when looking for a return value from another behavior function. I ran into a little bug with getting return values between behaviors.

See my post:

http://www.garagegames.com/community/forums/viewthread/134947
#8
11/02/2013 (10:19 am)
okay cool! thanks for the help guys