Game Development Community

Getting behaviors to be aware of each other

by Fredric Echols · in Torque Game Builder · 11/16/2008 (7:53 pm) · 8 replies

Hello,

What is the mechanism with which complementary behaviors can be aware of each other?

As an example, let's say there is a Baseball behavior and a BaseballBat behavior, both of which provide tweakable parameters in the editor.

During the game, the Baseball behavior needs to read the attributes from the BaseballBat behavior on collision, to determine the amount of energy to transfer from the bat.

#1
11/16/2008 (9:56 pm)
As a side note, is there a reliable method for allowing behaviors to be aware of other objects in the world?

Here's the current (non-working) test code in the behavior initialization:

%template.addBehaviorField(TestSprite, "Attached sprite.", Object, "", t2dStaticSprite);

Unfortunately, the "Attached sprite" field always shows a value of "None" in the drop-down in the TGB editor, no matter how many static sprites are in the level.

Thanks!
#2
11/20/2008 (7:29 am)
The problem with the "TestSprite" behavior field is that it requires the objects to be named, i.e. have a name under Edit->Scripting->Name in the editor.

So that problem is solved. :)

It would still be quite useful to allow behaviors to become aware of each other. Does anyone have thoughts on that?

What type of method can be used to get a given behavior from an object in the scene?
#3
11/20/2008 (8:35 am)
Quote:What type of method can be used to get a given behavior from an object in the scene?

You can use the getBehavior method to get an objects behavior like this:

someObject.getBehavior("SomeBehaviorName");

Maybe this helps.
#4
11/21/2008 (6:47 am)
Stefan,

That's perfect. Thanks!

The behaviors are now happily sharing data with each other, which makes it easier to create sophisticated interactions.

For example, a callback in one behavior for ObjectA can store a variable, and a behavior on ObjectB can use the data by finding out whether ObjectA has the particular behavior and data it needs.

Allowing behaviors to "sense" each other in this way allows for some really cool editor functionality, because gameplay scenarios "just work" when they are put together by the designers. :)
#5
11/25/2008 (4:20 pm)
Sorry to ask but how I can use getBehavior to share variables? For instance, I have a variable called $player.moveRight in behaivor "A". Now I want to to use it in behavoir "B" in this way if ($player.moveRight ) {}

Thanks in advance

Learner
#6
11/25/2008 (5:07 pm)
Guys, this is what I am trying to do but it is not working

1. I named my object "player" in the scripting tap
2. In the behavior (MovementControlsBehavior) I attached to "player", I added a function in this way:

function MovementControlsBehavior::getVar(%this){
return 100;
}

3. I attached a behavior (testGetValue) to a sceneObject. In that behavior I added these lines


%behavoirTest = %this.player.getBehavior("MovementControlsBehavior");
echo (%behaviorTest.getVar());

Could you tell me what I am doing wrong (I know...everything)

Thanks in advance
Learner
#7
11/25/2008 (5:44 pm)
When you name an object, you reference the object using:

Player.getBehavior("MovementControlsBehavior");

When you try "%this.player" it is expecting that the player object is referenced in the dynamic field of "%this".
#8
11/25/2008 (5:58 pm)
Thanks Phillip. It is working!! I was close :)

Learner