Game Development Community

Basic question: datablocks versus behaviors

by Brooks Talley · in Torque Game Builder · 11/19/2009 (12:35 am) · 3 replies

So I'm trying to learn TGB. I don't blame GarageGames, but it's definitely a bit of a chore, with tons of sample code for different versions of the engine and limited reference material. Such is the downside of rapid development I suppose.

I'm mostly managing to make things go, but I'm a little unclear on what I *should* be doing with regards to behaviors and datablocks. Is there any clear documentation that says "use behaviors for _____, and datablocks for ______"? Or have behaviors superseded datablocks to where the later are no longer used for from-scratch development (it seems like you can put everything into OnBehaviorAdd and OnAddScene, right?)


#1
11/19/2009 (2:09 am)
The fundamental difference between datablocks and behaviors is that the former is basically a passive entity where the latter is an active entity(*).

Datablocks were originally devised for Torque networking as collections of static game data that could be sent over the wire once and then referenced and reused from an arbitrary number of game objects (think "max rounds of ammo for this gun" (static & all objects) vs. "current number of rounds for this gun" (dynamic & per-object)).

Behaviors, on the other hand, rather than attaching static game data to objects, are used to attach dynamic game *logic* to objects. They are a means to componentize logic and selectively mix it into existing objects.

So, in short, use datablocks to configure the static properties of objects and use behaviors to attach script logic to objects.


(*) Of course, like every clean picture, this is messed up somewhat by certain logic being explicitly triggered on datablocks.
#2
11/19/2009 (8:53 am)
Datablocks are "templates" for your game objects. When you attach a datablock to an object you just attached him a set of pre-defined static attributes. (size, rotation, layer, collision presets, physics that applies to an object...)

On the other hand, when you attach a behavior to an object, you attach him a set of dynamic functions that applies to that object, and that object can use. (WASDmovement, mouseEvents...) or any other functions.

The main advantage of those things is that it helps easier level building. That way, you can do some coding outside of standard TGB editor, and when it comes to put some interactive objects on the stage, to create game level for example you just:
1) import graphic from the "library"
2) attach datablock to it (to define attributes of an object)
3) attach behaviors(functions) to the object so it may become interactive.

Personally i neither use datablocks (coz they are buggy) nor behaviors, since i like to dynamically create level content. But thats just for advanced users.
#3
11/19/2009 (12:30 pm)
Cool, thank you both. Those explanations really help a lot.

Next question: is there a mechanism to package up a bunch of behaviors so I don't have to add six of 'em to every object? For instance, if I have:

- AutoTarget
- MouseTarget
- Patrol
- CarCollision

...and I want my PlayerCar to have MouseTarget, Patrol and CarColission, and my EnemyCar objects -- lots of 'em -- to have AutoTarget, Patrol, and CarCollision.

In that case, is there a way to just have PlayerCarBehavior and EnemyCarBehavior so I'm only adding two things to every object in the UI (a behavior and a datablock)? Or, better yet, can I assign the behavior(s) from the datablock or vice versa?

Thanks again -- I'm having a blast playing with TGB, but wow could it use better documentation of stuff like this. Someone really needs to write the 6-8 page "Intro to TGB / definitions / best practices" document that covers stuff like this so newbies aren't trying to clean best practices from the assembled knowledge of a bunch of tutorials (most of which are out of date in varying degrees) and forums.