Game Development Community

Can't get datablock to work [solved]

by Max Kielland · in Torque Game Builder · 01/20/2013 (4:48 am) · 8 replies

Hi,

I have defined a datablock and are assigning it when I create a new class instance. But when I inspect the datablock variables they are all uninitialised.

datablock t2dSceneObjectDatablock(BaseTileDB) {

  class     = TBaseTile;
  Speed     = 1;
  Compass   = "E";
};

function TBaseTile::Create(%this,%tileLayerObject,%tile,%tileScript) {

  %baseTile = new ScriptObject()
  {
    dataBlock = "BaseTileDB";
  };

  echo(%baseTile.Speed);
  echo(%baseTile.Compass);

  return %obj;
}

The inspected "Speed" and "Compass" all show "" instead of 1 resp. "E".

I have read (www.garagegames.com/community/forums/viewthread/92321) high and low about datablocks and I can't figure this out. In some cases The above approach is used and in some other cases a datablock is created with new rather than datablock, as shown below.

new t2dSceneObjectDatablock(BaseTileDB) {

  class     = TBaseTile;
  Speed     = 1;
  Compass   = "E";
};

function TBaseTile::Create(%this,%tileLayerObject,%tile,%tileScript) {

  %baseTile = new ScriptObject()
  {
    dataBlock = "BaseTileDB";
  };

  echo(%baseTile.Speed);
  echo(%baseTile.Compass);

  return %obj;
}

Neither of these approaches works and show empty fields.

As a bonus question; Is there a simpler datablock to derive from than t2dSceneObjectDatablock? I really don't need all the scene data. An empty datablock to derive from would do fine..

#1
01/20/2013 (6:05 am)
You must access it using %baseTile.datablock.Speed and %baseTile.datablock.Compass.
#2
01/20/2013 (6:06 am)
Quote:Is there a simpler datablock to derive from than t2dSceneObjectDatablock?
That is the most basic datablock you should use in TorqueScript for scene based objects.
#3
01/20/2013 (6:20 am)
Okay, it seems like I have misunderstood the datablock concept a bit.

1.
So the variables declared in the datablock is not treated as class variables of the class using the datablock?

So if the datablock assignment in my class; datablock = MyDatablock is a class variable itself I could name it anything, right? Such as MyData = MyDatablock; because MyDatablock is of the type datablock, MyData would be handled correct?

So what is the benefit of using datablock over class variables, more than assigning default values to a bunch of variables?

2.
As you can see I use a ScriptObject because I create a class that is not displayed on the screen. Using a t2dSceneObjectDatablock seems a bit overkill for a class not used by a scene. What is the most simple and basic variant of a datablock for none scene based objects?

Thank you for casting some light over this heavily debated concept :)
#4
01/20/2013 (6:29 am)
Quote:So the variables declared in the datablock is not treated as class variables of the class using the datablock?
In this instance, it does not work that way. A ScriptObject is not a type of t2dSceneObject. If you created a t2dStaticSprite and assigned it a t2dSceneObjectDatablock in the config property, that sprite will then take on the variables in the datablock.

A ScriptObject is just a simple console object. That means you can declare it in TorqueScript and manipulate it. It does not automatically share set and get properties from objects of a different class.

Quote:Using a t2dSceneObjectDatablock seems a bit overkill for a class not used by a scene. What is the most simple and basic variant of a datablock for none scene based objects?
It is overkill. Just store the variables directly on the ScriptObject as fields.
#5
01/20/2013 (6:36 am)
I think I understand what you are saying.

The simple answer to my problem is that since MyClass isn't a member of a t2dSceneObject, I have to access them via the datablock variable.

%this.datablock.MyDatablockVariable;

If my class had been a derivate of a t2dSceneObject, I would be able to access the datablock variables as they where direct members of the class;

%this.MyDatablockVariable;

Is this correct interpreted?
#6
01/21/2013 (7:24 am)
Essentially, you have the right idea.
#7
01/21/2013 (7:33 am)
And he's also recommending an approach more like this:
function TBaseTile::Create(%this,%tileLayerObject,%tile,%tileScript) {   
  
  %baseTile = new ScriptObject()   
  {   
     class     = TBaseTile;   
     Speed     = 1;   
     Compass   = "E";   
  };   
  
  echo(%baseTile.Speed);   
  echo(%baseTile.Compass);   
  
  return %obj;   
}
Though something like this:
function TBaseTile::Create(%this,%tileLayerObject,%tile,%tileScript,%tileDatablock) {   
  
  %baseTile = new ScriptObject()   
  {   
    dataBlock = %tileDatablock;   
  };   
  
  echo(%baseTile.datablock.Speed);   
  echo(%baseTile.datablock.Compass);   
  
  return %obj;   
}
lets you pass in a datablock to set properties in your objects by predefined types by simply passing in a datablock with the desired properties. Overkill, but if you have large groups of properties it might be worth it for the convenience factor.
#8
01/21/2013 (12:37 pm)
Thanks, I solved it with a more C++ like approach.

function TClassA::Initialise(%this,%param1,%param2) {

  %this.BaseParam = "Base initialisation"
  %this.SomeParam = 1;
}

function TClassB::Create(%param1,%param2) {

  %obj = new ScriptObject() {
    class      = TClassB;
    superclass = TClassA;
  };
  
  %obj.Initialise(%param1,%param2);
  
  return %obj;
}

function TClassB::Initialise(%this,%param1,%param2) {

  // Base class initialisation
  TClassA::Initialise(%this,%param1,%param2);

  // derived initialisation
  %this.DerivedParam = "Derived initialisation"

  // Override base param
  %this.SomeParam = 3;
}

This will bubble the initialisation bottom up adding or overriding my variables as the initialisation bubbles up.