Game Development Community

Attach behavior by script

by Diogo Rios · in Torque Game Builder · 06/18/2008 (11:09 am) · 6 replies

Hi, how can i attach a behavior by script?

when u add it on the tgb it creates a line on the level file like:

_behavior0 = "MouseDraggableBehavior ";

but i need it to attach it dinamically like:

%animal = new t2dAnimatedSprite()
{
config = LionConfig;
scenegraph = %sg;
visible = %visible;

};

%animal.attachBehavior bla bla

#1
06/18/2008 (12:55 pm)
%animal.addBehavior( BehaviorTemplateName.createInstance() );

You can find these and other methods listed in the T2D reference section of the html docs. Listed under BehaviorTemplate, BehaviorInstance, BehaviorComponent (All sceneObjects are BehaviorComponents).
#2
06/18/2008 (1:19 pm)
I searched tdm for that and got not even one link of it. Plus, i need an example of one that has parameters assigned to it. can u help me?
#3
06/18/2008 (1:25 pm)
Create one in a level and look at it in the level file. I believe it is just a list with key value pairs separated by tabs or some other whitespace.
#4
06/18/2008 (1:53 pm)
You probably didnt read my post correctly, that was what i did, but i need it to attache by script dinamically. the solution James Ford provided seems correct but how do i setup the parameters for the behavior?.
#5
06/20/2008 (8:56 am)
%behavior = BehaviorTemplateName.createInstance();
%behavior.field1 = "value";
%animal.addBehavior(%behavior);

I was referring to the offline html docs not the TDN. For looking up all methods/callbacks/fields of a class, I would recommend loading up the offline docs, expand Reference, and expand TGB reference.
#6
06/20/2008 (9:28 am)
Thats great, thanks James.