Game Development Community

C++ object specific variables

by Chris Norpchen · in Torque Game Engine · 07/02/2004 (1:46 pm) · 3 replies

I need to set a variable in C++ for a particular object. If I have a variable X and I use mDataBlock->X I change the datablocks variable X and all of the objects using that datablock change. I want a variable in C++ for each particular object. I know in script you can just say %obj.X and it gives me X for that particular object. But, I neeeeed to have a variable set on an individual object in C++.
I have tried to set it in the public section of the ShapeBase class, but I can't seem to access it??? How do you add an object specific variable and how do you access it in the engine code???
Please help.

#1
07/03/2004 (10:48 am)
OK, here is the problem I am having...
I have a variable (call it myvar) on a ShapeBase that I add in the onAdd part of ShapeBase.cc. Later I change myvar in ShapeBase::advanceTime and print it to the console and it shows the correct number changed on the individual object. Now each individual shapebase now has its own individual myvar variable.
Everything fine so far...
Now I try to change myvar in a ConsoleMethod that the script can send an amount to to change the myvar. But, it doesn't change the individual ShapeBase variable myvar??? It prints out it's own myvar??? How do I access a variable that is on an individual ShapeBase (not the Datablock that affects all ShapeBases) in a ConsoleMethod??
#2
07/03/2004 (11:21 am)
...
#3
07/09/2004 (7:14 am)
Actually, I figured it out. tsShapeInstance is usually for rendering, so I don't know if you want to put certain variables in there. But, you can add variables in the onAdd function in C++ to your object, like WheeledVehicle::onAdd in the WheeledVehicle.cc file. Kinda like if you want a variable in script to be object based for a particular object you add the variable in the onAdd file in script.
Like:
WheeledVehicle::onAdd(blah blah blah...
%obj.myvar=10;

now the variable myvar will be 10 for only that vehicle. But, you still cannot individually change DataBlock variables on a per object basis, but you can create new object specific variables.

Also, in C++ code, you can't just add the variable and expect it to work. It will work in the C++ code but will not allow you to change it in script unless you use a consoleMethod and setup a flag or variable that uses a Maskbit. You tell it to change the variable and then tell it to update the Maskbit you have the variable a part of and then the engine will update the internal variable on the individual object in C++. Quite confusing, but I hope this helps anyone out there that is having the same problem I was.