Thursday, April 12, 2007

Torque Script - datablocks - very confusing

It took me quite a while to really understand datablocks in Torque. All the books say exactly the same thing.


Of all the features in TorqueScript, Datablocks are probably the most confusing. To make things worse, they are central to the creation of most objects, which means you need to understand them relatively early.

I think all the books plagiarized this from the official documentation on datablocks.

Datablocks are just shared objects that contain static data that doesn't change from one object to the next. So the engine can send it once over the network and reference it by id from then on. I get this and I think every explanation made this perfectly clear. The problem is that all the callbacks from the engine into the script code comes through these datablocks. For instance:

datablock StaticShapeData(SomethingData)
{
// Basic Item properties
shapeFile = "./mymodel.dts";
};
function SomethingData::onCollision(%this, %obj, %col)
{
// your code here
}
This really confused me and I was trying to create my own object types to no avail. The code above creates a type SomethingData which will show the 3d model contained in the file mymodel.dts. This is fine, but the call to onCollision is done on the datablock object (SomethingData) versus the object itself which in this case ends up being a StaticShape. Since the datablock is static and shared across all instances I couldn't figure out why or how you kept data in individual objects.

I finally had an epiphany when I noted that %obj is always passed into the callbacks and is the actual object instance. Individual changes and values can be made in this object. Seems simple enough now, but this killed some brain cells for me.

So I placed this comment in one of my pieces of sample code for class.

function SomethingData::onCollision(%this, %obj, %col)
{
/* * MANTRA - REPEAT OVER AND OVER UNTIL DATABLOCK UNDERSTANDING SETS IN
* 1. %this is the datablock * 2. %obj is the object instance
*
* of much less importance
* 3. %col is the object colliding

Here is the lecture where I tried to explaing this. I think the notes are still a little jumpy, but the in person version seemed to clear things up for students. Your fuel economy may vary.

No comments: