Creating a new Datablock object
Datablock objects have static properties and are used as a common data store between game objects that derive from the GameBase
class. They are defined on the server and are passed to clients (by SimObject
ID only) during the initial transmission of a game level. In this recipe we'll see how to build a new Datablock
object.
How to do it...
Creating a Datablock
instance is straight forward. Here we will create a StaticShapeData
Datablock
, one of many possible Datablock
classes as follows:
datablock StaticShapeData(MyShapeData) { category = "Scenic"; shapeFile = "art/shapes/rocks/rock1.dts"; computeCRC = true; isInvincible = true; };
How it works...
We use the datablock
keyword when creating a new Datablock
class object and always give it a unique global name. This name is used by other objects to reference this Datablock
through the use of datablock
property of the GameBase
class.
If we happen to create two Datablock
instances with the same global name but of different classes, then a Cannot Re-declare data block with a different class error is output to the console and nothing is done with the second Datablock
instance. However, if the two global names and classes match, then all of the properties from the second Datablock
instance are copied into the first.
There's more...
There are a number of different things to keep in mind when it comes to creating a Datablock
instance. Let's take a look at them.
Creating a new Datablock object based on another Datablock object
We can base the properties of one
Datablock
instance on a previously created Datablock
instance through the use of a copy source during the creation of the Datablock
object. The process is the same as when using the new
keyword. See the Creating a new SimObject instance recipe for more information on using a copy source.
Limited total number of Datablocks
The SimObject
ID of a Datablock
instance comes from a special pool that is reserved for the Datablock
class. This ID pool only allows 1024 Datablock
instances to be defined per game level. This number may be increased by changing the source code of Torque 3D. It is this special SimObject
ID that is transferred between the server and client in a multiplayer game, and is used by the GameBase
derived classes to reference their Datablock
object on the client.
The datablock keyword should only be used on the server
Use of the datablock
keyword should be limited to the server script files. Only the server keeps a track of the special Datablock
ID pool, and all the Datablock
objects on the client are deleted just prior to a game level being loaded.
Datablock properties should be considered static
Once a Datablock
object has been created, its properties should be considered static. It is possible to modify the properties of a Datablock
object at any time, just as with any other SimObject
, but this should be avoided. The modified Datablock
properties are not retransmitted between the server and client and will result in strange errors during game play.
See also
Creating a new SimObject instance
Creating a new internal name only SimObject instance
Creating a new singleton recipes