How do you setup a flexible datablock?
by Kyrah Abattoir · in Torque 3D Professional · 09/05/2011 (2:16 pm) · 7 replies
I would like to create a new datablock for an object, so the object has some flexible parameters, yet set them to a default value when the object is instanciated, how would i do this?
About the author
3D artist, programmer, game designer, jack of all trades, master of none.
#2
09/05/2011 (2:36 pm)
so how can i ensure that at no moment the parameter i left out of the datablock won't be in an unset state?
#3
Or, since if you want flexible parameters, you're probably thinking of parameters that are custom from player to player? That would simply be a matter of adding a variable to the player or object and then networking it (the object-based parameters that Robert is talking about, with full network support, which allows you to update them on the fly and have them be individual variables).
09/05/2011 (2:38 pm)
It's actually possible- though you need to take an approach similar to that which AFX uses to substitute values. That's non-trivial, to say the least.Or, since if you want flexible parameters, you're probably thinking of parameters that are custom from player to player? That would simply be a matter of adding a variable to the player or object and then networking it (the object-based parameters that Robert is talking about, with full network support, which allows you to update them on the fly and have them be individual variables).
#4
Initialize it with a value that you can check against (like -1 or 0 or something). Mainly, though, datablocks are static data, so you shouldn't be doing that...
09/05/2011 (2:39 pm)
Quote:so how can i ensure that at no moment the parameter i left out of the datablock won't be in an unset state?
Initialize it with a value that you can check against (like -1 or 0 or something). Mainly, though, datablocks are static data, so you shouldn't be doing that...
#5
In your case it sounds like you want to attach some dynamic data to every object that uses a specific datablock which is entirely possible by using the datablock's onAdd() method.
As an example, let's say you create a datablock for an object like so:
To add a dynamic variable to each object that uses MyDataBlock you'd simply do:
In the above function, the %this parameter references the individual instance of 'MyDataBlock' that each object uses. The %obj parameter actually holds the id of the object that was created.
09/05/2011 (5:49 pm)
As I said in your other thread, datablocks are designed to be static blocks of data that are referenced by several objects of the same type. Attaching any dynamic data to them is generally a bad idea because datablocks are (by default) only downloaded by a client once so any changes won't be transmitted properly. In your case it sounds like you want to attach some dynamic data to every object that uses a specific datablock which is entirely possible by using the datablock's onAdd() method.
As an example, let's say you create a datablock for an object like so:
datablock StaticShapeData(MyDataBlock)
{
shapeFile = "something/somethingElse/shape.dts";
...
};To add a dynamic variable to each object that uses MyDataBlock you'd simply do:
function MyDataBlock::onAdd(%this, %obj)
{
%this.AStaticVariable = 321; // this will be attached to the datablock
%obj.ADynamicVariable = 123; // this will be attached to the object using this datablock
}In the above function, the %this parameter references the individual instance of 'MyDataBlock' that each object uses. The %obj parameter actually holds the id of the object that was created.
#6
Datablocks are only transmitted to clients when they connect to the server, so if you think about that, modifying the parameters in a datablock will not affect any previous players in the game.
Object based parameters are the way to go, if you need to adjust all of them at once, perform a loop through the object list, comparing them with %object.getDatablock().getName() to test if it is the specific block you need, then go about the route previously mentioned (object modifier): %object.parameter = value;
Good luck!
09/05/2011 (6:00 pm)
@Chris: That's what I was looking to say.Datablocks are only transmitted to clients when they connect to the server, so if you think about that, modifying the parameters in a datablock will not affect any previous players in the game.
Object based parameters are the way to go, if you need to adjust all of them at once, perform a loop through the object list, comparing them with %object.getDatablock().getName() to test if it is the specific block you need, then go about the route previously mentioned (object modifier): %object.parameter = value;
Good luck!
#7
09/06/2011 (12:17 am)
@Chris: Thank you! that's axactly what i was looking for! i just wasn't sure where to initialise the dynamic variables so they would be initialized at the same time the object would be instanciated.
Torque Owner Robert Fritzen
Phantom Games Development
You might want to however do object based parameters instead:
%data.parameter = Not Possible (to my knowledge)
%object.parameter = Best Option