Datablocks... WTF? TGEA 1.8
by Duey Oxburger · in General Discussion · 02/13/2009 (7:44 pm) · 2 replies
Hey Guys,
Noob... What is a Datablock, and how is one made? When would you use it?
Anyone got a link to docs?...
Thx,
Duey
ps - Artist here, not programmer...
Noob... What is a Datablock, and how is one made? When would you use it?
Anyone got a link to docs?...
Thx,
Duey
ps - Artist here, not programmer...
About the author
#2
First off, datablocks are a great lesson in object-oriented in programming, you'll need to understand how that works a bit in order to get datablocks.
In order to save network bandwidth (and a tiny bit of a users memory, as well, but the main purpose is 99% for networking) datablocks are loaded, checked, etc... at the beginning of a game. They are assumed to remain mostly constant throughout the game. Datablocks contain info that will be shared among many classes, without the need for each object of that class to store/update the data themselves. This is where the OO part comes in.
Let's say you have 5 players in a game, and they all use the datablock PlayerBody. The way this really works is there is 1 PlayerData object, the datablock, and 5 different Player objects, the 5 players. Now each Player has many fields that change over time, such as their position, speed, direction, what animation they are doing, etc... These all need to be stored on the individual Player Object, because 1 object may (and almost certainly will) be different than another in that field. And when that player updates that field over the network (let's say...position), only that player will have their position updated to the new one on the server, and not every other Player warped to that spot.
Players (and anything inherited from GameBase and usually SimBase, really) have what's called a datablock, this is really just a pointer back to their datablock. A good way to remember what the class name of an object's datablock is the name of an object is <ClassName> (e.g. ShapeBase) and the datablock name is <ClassName>Data (ShapeBaseData). So Players are Player and their datablock is a PlayerData.
The PlayerData stores the data that is assumed will not be changed throughout the game, and only needs to be networked on connect. So, a player's mass or runForce will be shared among all Player objects with the same datablock. Changing a field in a datablock will change it for ALL objects pointing back to it, so if for one of your 5 players you call %player.datablock.runForce = 5000; to try and give them a speed bonus, all of the other 5 players will have their runForce modified as well, because you aren't affecting the object at all, only the object these things lookup. That example is a very common beginner mistake.
To set up a datablock, you do so in script with the following format:
May be hard to understand from that, so let's say you want to create a player datablock by the name of myPlayerData:
I hope this helps you.
02/14/2009 (1:18 pm)
No problem, datablocks are probably the most confusing thing to newcomers to Torque, so I'll take some time to explain them.First off, datablocks are a great lesson in object-oriented in programming, you'll need to understand how that works a bit in order to get datablocks.
In order to save network bandwidth (and a tiny bit of a users memory, as well, but the main purpose is 99% for networking) datablocks are loaded, checked, etc... at the beginning of a game. They are assumed to remain mostly constant throughout the game. Datablocks contain info that will be shared among many classes, without the need for each object of that class to store/update the data themselves. This is where the OO part comes in.
Let's say you have 5 players in a game, and they all use the datablock PlayerBody. The way this really works is there is 1 PlayerData object, the datablock, and 5 different Player objects, the 5 players. Now each Player has many fields that change over time, such as their position, speed, direction, what animation they are doing, etc... These all need to be stored on the individual Player Object, because 1 object may (and almost certainly will) be different than another in that field. And when that player updates that field over the network (let's say...position), only that player will have their position updated to the new one on the server, and not every other Player warped to that spot.
Players (and anything inherited from GameBase and usually SimBase, really) have what's called a datablock, this is really just a pointer back to their datablock. A good way to remember what the class name of an object's datablock is the name of an object is <ClassName> (e.g. ShapeBase) and the datablock name is <ClassName>Data (ShapeBaseData). So Players are Player and their datablock is a PlayerData.
The PlayerData stores the data that is assumed will not be changed throughout the game, and only needs to be networked on connect. So, a player's mass or runForce will be shared among all Player objects with the same datablock. Changing a field in a datablock will change it for ALL objects pointing back to it, so if for one of your 5 players you call %player.datablock.runForce = 5000; to try and give them a speed bonus, all of the other 5 players will have their runForce modified as well, because you aren't affecting the object at all, only the object these things lookup. That example is a very common beginner mistake.
To set up a datablock, you do so in script with the following format:
datablock <class name>( <What you want to name the datablock> )
{
<field1> = <field1 value>;
<field2> = <field2 value>;
<field3> = <field3 value>;
<field4> = <field4 value>;
...
}; //Needs a semi-colonMay be hard to understand from that, so let's say you want to create a player datablock by the name of myPlayerData:
datablock PlayerData(myPlayerData)
{
shapeFile = "~/data/shapes/player/player.dts";
cameraDefaultFov = 90.0;
cameraMinFov = 5.0;
cameraMaxFov = 120.0;
mass = 90;
drag = 0.3;
maxdrag = 0.4;
density = 10;
maxDamage = 100;
maxEnergy = 60;
repairRate = 0.33;
//Do the rest of the PlayerData fields, or do not enter them and they will receieve the Default value
}; //Always end with a semi-colonI hope this helps you.
Torque Owner Brandon Baker
World Core Studios