Puzzled by Dynamic Variables
by Matthew Genge · in Torque 3D Professional · 02/09/2012 (4:21 am) · 7 replies
Dear All,
I am new to torque script and I am struggling with dynamic variables. I want to define dynamic variables within an object whose values can be used and changes in script. However, I don't seem to be able to figure out the syntax, the values are constantly blank.
I've tried definiting them in the data block:
datablock StaticShapeData (RBoat){
ShapeFile="art/shapes/RubberBoat/rubberboat.dae";
//Dynamic variables
maxVelocity = 10;
};
Then once I've spawned the staticshape I've tried calling them in two ways from server-size script, but neither return a value:
%value = %obj.maxVelocity;
%value = %obj.getFieldValue(maxVelocity);
I'd assumed to make the values dynamic and changeable at runtime I add canSaveDynamicFields to the DataBlock like this:
datablock StaticShapeData (RBoat){
ShapeFile="art/shapes/RubberBoat/rubberboat.dae";
//Dynamic variables
canSaveDynamicFields = "1";
maxVelocity = 10;
};
So I am puzzled. If anyone can illuminate I'd be grateful.
Thanks, Matt
I am new to torque script and I am struggling with dynamic variables. I want to define dynamic variables within an object whose values can be used and changes in script. However, I don't seem to be able to figure out the syntax, the values are constantly blank.
I've tried definiting them in the data block:
datablock StaticShapeData (RBoat){
ShapeFile="art/shapes/RubberBoat/rubberboat.dae";
//Dynamic variables
maxVelocity = 10;
};
Then once I've spawned the staticshape I've tried calling them in two ways from server-size script, but neither return a value:
%value = %obj.maxVelocity;
%value = %obj.getFieldValue(maxVelocity);
I'd assumed to make the values dynamic and changeable at runtime I add canSaveDynamicFields to the DataBlock like this:
datablock StaticShapeData (RBoat){
ShapeFile="art/shapes/RubberBoat/rubberboat.dae";
//Dynamic variables
canSaveDynamicFields = "1";
maxVelocity = 10;
};
So I am puzzled. If anyone can illuminate I'd be grateful.
Thanks, Matt
About the author
I am an Earth Scientist who likes writing games for fun.
#2
Many thanks for the answer. Just what I was looking for.
Thanks,
Matt
02/09/2012 (8:07 am)
Dear Steve,Many thanks for the answer. Just what I was looking for.
Thanks,
Matt
#3
This is how you would access an object's datablock's dynamic field(s):
02/09/2012 (9:40 am)
The problem was you were adding dynamic fields to a datablock instance and tried accessing said fields from within the object instead which is incorrect.This is how you would access an object's datablock's dynamic field(s):
// get object's data block %data = %obj.getDatablock(); // now you have access to the datablock's fields %value = %data.maxVelocity; echo(%value); // you can even do it this way: echo(%obj.getDatablock().maxVelocity);
#4
I'll have to maintain some arrays to hold data...which is a shame it is not as elegant as I hoped.
Matt
02/09/2012 (12:52 pm)
StaticShape really doesn't like having dynamic fields. Sorry but neither of these suggestions seem to work. The variables seem to only be accessible in the same function that they are created but not in others. It is bizarre. I'll have to maintain some arrays to hold data...which is a shame it is not as elegant as I hoped.
Matt
#5
Created as:
Accessed as:
Returns "10";
02/09/2012 (1:26 pm)
datablock StaticShapeData(rBoatData)
{
ShapeFile="art/shapes/RubberBoat/rubberboat.dae";
maxVelocity = 10;
};Created as:
%shape = "rBoatData";
%newObj = new StaticShape()
{
datablock = %shape;
};
MissionCleanup.add(%newObj);Accessed as:
%newObj.getDatablock().maxVelocity;
Returns "10";
#6
Perhaps it is something to do with where I am instancing the staticshape in a serverCmd function called from a client script. I don't seem to be able to access the variables in a seperate serverCmd function.
02/09/2012 (2:32 pm)
Hmmm....I believe you.Perhaps it is something to do with where I am instancing the staticshape in a serverCmd function called from a client script. I don't seem to be able to access the variables in a seperate serverCmd function.
#7
I can define dynamic variables in the datablock, whose values can be changed at runtime. However, I haven't tested whether these new values will apply to all shapes created from the dataBlock.
These values can be assigned and accessed with:
Dynamic variables can also be defined when the static shape is instanced. As shown by Steve:
But there is an issue....there seems to be a limit on the memory available for variable names. For example the following will throw an error during compiling:
Dynamic variables defined on instancing are accessed and assigned by:
Finally...my main mistake was getting confused between the two following statements to return the current control object. I was using the wrong object ID to try and return the values.
Matt
02/11/2012 (6:00 am)
After much experimenting I have an explanation and solution:I can define dynamic variables in the datablock, whose values can be changed at runtime. However, I haven't tested whether these new values will apply to all shapes created from the dataBlock.
datablock StaticShapeData (RBoatData){
ShapeFile="art/shapes/RubberBoat/rubberboat.dae";
maxVelocity = 10;
boatmaxwaveAngle = 0.02745;
boatmaxAccel = 0.001;
boatwaterDrag = 0.0001;
};These values can be assigned and accessed with:
%obj.getDatablock().maxVelocity;
Dynamic variables can also be defined when the static shape is instanced. As shown by Steve:
%obj = new StaticShape() {
datablock=%shape;
bsp = 0.0;
bc = 0;
ba = 0.0;
bts = 0.0;
bta = 0.0;
bwp = 0.0;
};But there is an issue....there seems to be a limit on the memory available for variable names. For example the following will throw an error during compiling:
%obj = new StaticShape() {
datablock=%shape;
boatSpeed = 0.0;
boatAccel = 0;
boatClient = 0.0;
boatTurnSpeed = 0.0;
boatTurnAccel = 0.0;
boatWavePhase = 0.0;
};Dynamic variables defined on instancing are accessed and assigned by:
%val = %obj.ba;
Finally...my main mistake was getting confused between the two following statements to return the current control object. I was using the wrong object ID to try and return the values.
%obj = %client.getControlObject(); %obj = %client.player.getControlObject();
Matt
Associate Steve Acaster
[YorkshireRifles.com]
datablock StaticShapeData(rBoatData) { category = "StaticShape"; shapeFile = "art/shapes/RubberBoat/rubberboat.dae"; }; function rBoatData::onAdd(%this, %obj) { %obj.maxVelocity = 10; }Alternatively you can add variables to the object when you spawn it with a script - this way you can spawn the same type of objects but alter the variable for each one:
%shape = "rBoatData"; %newObj = new StaticShape() { datablock = %shape; maxVelocity = 10; }; MissionCleanup.add(%newObj);Note that I use "rBoatData" as a naming convention to show that it's datablock info rather than an object, it's an old Torque convention that I've stuck with.
-------------
Incidentally, if you want a vehicle you'd be better off using a vehicle class rather than a staticShape.