Mini Behavior Tutorial
by Phillip O'Shea · in Torque Game Builder · 10/12/2007 (3:53 am) · 24 replies
Creation:
Behaviors are created in the same manner as regular script entities are. Below is some example code of how to create a behavior, and some of its various features:
It is pretty simple stuff when you look at it like that, however, there are some really awesome properties that you should take advantage of when managing behaviors. One of which are the different field types. I will list all of the field types and their descriptions below:
1. Default - Plain ol' input text box, nothing really fancy here,
2. Int - Input text box, which requires an integer to be entered,
3. Float - Input text box, requiring a float with 3 decimal points,
4. Point2F - Two input boxes, registering as a vector,
5. Bool - Nice lil checkbox,
6. Enum - Drop down list, data is controlled by the userData field,
7. Object - Drop down list, containing any object with the class defined in the userData field,
8. Keybind - Drop down list, containing all of the keyboard and joystick keys available,
9. Color - Color picker popup,
10. polygon - Creates a polygon using the parent object,
11. localpointlist - Local point using the parent object.
There are 9 different fields, many of them are self explanatory, however, the "Enum" and "Object" field types may be confusing for some.
Enum
When you specify the "Enum" field type, you must specify what values go into that list. It is done in the last parameter of the "addBehaviorField" function, named "userData". To create the list, you just specify it like above. Each item should be separated by a new line (NL) operator, or, using the "\n" escape sequence. Be careful of spaces! To specify a default value you must type out the full name of the list item. I could be wrong, but I do not think you can just specify which index value the item is.
This wraps up the Enum field type.
Object
In the above example, I have specified that the list object must contain all of the objects within the "t2dAnimationDatablock" class. I have been scripting an animation behavior for a series of scene objects and found it rather handy to have a list of all of the animations available, rather than having to type them all out.
You can specify any class type in your userData field and it will list each of the objects in that subset, handy huh?
Custom Field Types
You must first register your new object, then you must specify a function name that is called when an object using your behavior is selected. In this example, I have just created a regular text box which inputs ordinary text. However, it acts just like regular GUI objects do, in that you create them, specify names and other properties through script.
I hope this has helped out with some of the unknown things about behaviors!
Update: Added the two new field types added in 1.7.X.
Behaviors are created in the same manner as regular script entities are. Below is some example code of how to create a behavior, and some of its various features:
if (!isObject(ExampleBehavior))
{
%template = new BehaviorTemplate(ExampleBehavior);
%template.friendlyName = "My Example Behavior";
%template.behaviorType = "Example";
%template.description = "Just showing you a few properties";
%template.addBehaviorField(eName, "Example Field", default, "Default Value");
}It is pretty simple stuff when you look at it like that, however, there are some really awesome properties that you should take advantage of when managing behaviors. One of which are the different field types. I will list all of the field types and their descriptions below:
1. Default - Plain ol' input text box, nothing really fancy here,
2. Int - Input text box, which requires an integer to be entered,
3. Float - Input text box, requiring a float with 3 decimal points,
4. Point2F - Two input boxes, registering as a vector,
5. Bool - Nice lil checkbox,
6. Enum - Drop down list, data is controlled by the userData field,
7. Object - Drop down list, containing any object with the class defined in the userData field,
8. Keybind - Drop down list, containing all of the keyboard and joystick keys available,
9. Color - Color picker popup,
10. polygon - Creates a polygon using the parent object,
11. localpointlist - Local point using the parent object.
There are 9 different fields, many of them are self explanatory, however, the "Enum" and "Object" field types may be confusing for some.
Enum
%template.addBehaviorField(eList, "Example List", Enum, "Second Item", "First Item" NL "Second Item\nThird Item");
When you specify the "Enum" field type, you must specify what values go into that list. It is done in the last parameter of the "addBehaviorField" function, named "userData". To create the list, you just specify it like above. Each item should be separated by a new line (NL) operator, or, using the "\n" escape sequence. Be careful of spaces! To specify a default value you must type out the full name of the list item. I could be wrong, but I do not think you can just specify which index value the item is.
This wraps up the Enum field type.
Object
%template.addBehaviorField(eAnimation, "Example Animation", Object, "exampleAnimation", t2dAnimationDatablock);
In the above example, I have specified that the list object must contain all of the objects within the "t2dAnimationDatablock" class. I have been scripting an animation behavior for a series of scene objects and found it rather handy to have a list of all of the animations available, rather than having to type them all out.
You can specify any class type in your userData field and it will list each of the objects in that subset, handy huh?
Custom Field Types
//Specify the Name of the object type, and the function that is called to create the field
BehaviorEditor::registerFieldType("NewObject", "createNewObjectGui");
function BehaviorFieldStack::createNewObjectGui(%this, %behavior, %fieldIndex)
{
%fieldInfo = %behavior.template.getBehaviorField(%fieldIndex);
%name = getField(%fieldInfo, 0);
%description = %behavior.template.getBehaviorFieldDescription(%fieldIndex);
%control = %this.createTextEditProperty(%name, "TEXT", %name, %description);
%editField = %control.findObjectByInternalName(%name @ "TextEdit");
%editField.object = %behavior;
}You must first register your new object, then you must specify a function name that is called when an object using your behavior is selected. In this example, I have just created a regular text box which inputs ordinary text. However, it acts just like regular GUI objects do, in that you create them, specify names and other properties through script.
I hope this has helped out with some of the unknown things about behaviors!
Update: Added the two new field types added in 1.7.X.
About the author
Head of Violent Tulip, a small independent software development company working in Wollongong, Australia. Go to http://www.violent-tulip.com/ to see our latest offerings.
#22
Sorry, I mis-typed the post. I'm actually just using t2dAnimationDatablock and t2dAnimatedSprite, like it is listed in your examples. I managed to populate the list with some objects by setting it to t2dSceneObject. Unfortunately, all that seemed to appear in the list were plain Scene Objects, a tile layer, and maybe a few others. There were no animated sprites in the list (though there are some placed into the level).
I mostly just want to know if anyone else is having this problem with 1.6. If not, then I'll know it's local, and I'll try to figure out what could be happening.
12/29/2007 (2:32 am)
@ Phillip:Sorry, I mis-typed the post. I'm actually just using t2dAnimationDatablock and t2dAnimatedSprite, like it is listed in your examples. I managed to populate the list with some objects by setting it to t2dSceneObject. Unfortunately, all that seemed to appear in the list were plain Scene Objects, a tile layer, and maybe a few others. There were no animated sprites in the list (though there are some placed into the level).
I mostly just want to know if anyone else is having this problem with 1.6. If not, then I'll know it's local, and I'll try to figure out what could be happening.
#23
08/16/2008 (5:33 pm)
Updated the list of fields to include the two new types added recently.
#24
I tried all the behavior fields at once: tdn.garagegames.com/wiki/TGB/Behaviors/Behaviors_Demo. The "default" one kept giving me an error, but all the rest of them worked fine.
08/19/2008 (7:34 pm)
Very nice. Thanks.I tried all the behavior fields at once: tdn.garagegames.com/wiki/TGB/Behaviors/Behaviors_Demo. The "default" one kept giving me an error, but all the rest of them worked fine.
Associate Phillip O'Shea
Violent Tulip