Introduction
A Datablock is a portion of TorqueScript that defines some standard
settings for an Object. Once a Datablock has been written, it can be
applied to as many objects as you want to give them all the properties
defined in that Datablock. In this Tutorial, we will learn how to place a
Datablock on some objects, then how to modify that Datablock so it
affects all of the objects it has been placed on.
System Overview
A Datablock is nothing more than a piece of script that defines a set
of properties for an object. Think of it like a configuration template.
All of your datablocks will be found in the file /projectFiles/scripts/datablocks.cs. The Datablock that we will be working with today looks like the following:
datablock t2dSceneObjectDatablock(FishDatablock) {
Class = "FishClass";
Layer = "0";
WorldLimitMode = "NULL";
WorldLimitMin = "-321.426 -185.000";
WorldLimitMax = "320.000 173.116";
UsesPhysics = "1";
WorldLimitCallback = "1";
minSpeed = "15";
maxSpeed = "55";
};
This datablock allows us to setup some basic data for the Fish in our
scene. By applying this datablock to every Fish object, we can be
assured that they all have the exact same world limits and movement
speed, and all use the FishClass object class.
Template
A datablock only requires a few pieces of data, allowing the user to
add any other properties they require. The following is a basic, raw
datablock, ready to be carved into whatever the developer requires.
datablock t2dSceneObjectDatablock(rawDatablock)
{
Layer = 1;
Size = "100 100";
};
This raw datablock will cause any object it is assigned to, to be placed
on Layer 1 and have a size in the Scene View of 100x100.
Example
Now that we understand the basics of what a datablock is, let's see
how it makes modifications of objects easy. We will do this by adding a
property to our fish. Open the Aquarium project, which ships with
iTorque2D. Let's make it so all of our fish have a blue tint.
Load up the datablocks.cs file for the Aquarium project. You will
see one Datablock within it, the FishDatablock. Modify this so it has
the following line of code at the bottom:
blendColor = "0.0 0.0 1.0 1.0";
Your datablock should now look like this
datablock t2dSceneObjectDatablock(FishDatablock) {
Class = "FishClass";
Layer = "0";
WorldLimitMode = "NULL";
WorldLimitMin = "-321.426 -185.000";
WorldLimitMax = "320.000 173.116";
UsesPhysics = "1";
WorldLimitCallback = "1";
minSpeed = "15";
maxSpeed = "55";
blendColor = "0.0 0.0 1.0 1.0";
};
Now save your datablock and go back into the iTorque2D editor. Note that
nothing has changed - yet. Datablock properties are only visible when
the game is running. Hit the play button, and your fish should now look
like this:
Conclusion
Modifying a Datablock is easy, and allows the developer to quickly
assign and adjust properties across all common objects in a game. Use
them to configure your AI properties for certain enemy types, rendering
properties, physics properties and more. |