Persistent Fields
by Justin Mosiman · in Torque Game Builder · 04/17/2009 (11:00 pm) · 4 replies
Hello,
I've created a new unit class derived from t2dAnimatedSprite, and I am trying to add some properties to the unit that I can set when it is created. I've looked through the parent classes and it looks like this needs to be done with persistent fields. I've tried adding the following:
But I am getting the compile error:
What am I doing wrong? I am following a similar format as what the parents are doing, and the variable mSpawnTime exists, as well as the method setSpawnTime.
Thanks,
Justin
I've created a new unit class derived from t2dAnimatedSprite, and I am trying to add some properties to the unit that I can set when it is created. I've looked through the parent classes and it looks like this needs to be done with persistent fields. I've tried adding the following:
void t2dGroundUnit::initPersistFields()
{
addProtectedField("SpawnTime", TypeF32, Offset(mSpawnTime, t2dGroundUnit), &setSpawnTime, &defaultProtectedGetFn, "");
Parent::initPersistFields();
}But I am getting the compile error:
Quote:
enginesourcet2dt2dgroundunit.cc(43) : error C2665: 'ConsoleObject::addProtectedField' : none of the 2 overloads could convert all the argument types
1> d:my documentsmy gamestgbwarenginesourceconsoleconsoleobject.h(523): could be 'void ConsoleObject::addProtectedField(const char *,const U32,const dsize_t,AbstractClassRep::SetDataNotify,AbstractClassRep::GetDataNotify,const U32,EnumTable *,const char *)'
1> d:my documentsmy gamestgbwarenginesourceconsoleconsoleobject.h(540): or 'void ConsoleObject::addProtectedField(const char *,const U32,const dsize_t,AbstractClassRep::SetDataNotify,AbstractClassRep::GetDataNotify,const char *)'
1> while trying to match the argument list '(const char [10], S32, dsize_t, void (__thiscall t2dGroundUnit::* )(bool), const char *(__cdecl *)(void *,const char *), const char [1])'
What am I doing wrong? I am following a similar format as what the parents are doing, and the variable mSpawnTime exists, as well as the method setSpawnTime.
Thanks,
Justin
#2
This question derives from a more broad question: What are the different types of persistent fields used for?
In consoleObject.h, I see four different types: addField, addFieldV, addProtectedField, and addDeprecatedField. While the documentation makes sense for addFieldV and addDeprecatedField, I'm not sure when I would want to use addField or addProtectedField. I used protected because that is what the parents were doing.
04/19/2009 (7:02 pm)
Thanks Phillip, that did the trick.This question derives from a more broad question: What are the different types of persistent fields used for?
In consoleObject.h, I see four different types: addField, addFieldV, addProtectedField, and addDeprecatedField. While the documentation makes sense for addFieldV and addDeprecatedField, I'm not sure when I would want to use addField or addProtectedField. I used protected because that is what the parents were doing.
#3
05/07/2009 (12:25 pm)
Thanks for the post this is very helpful.
#4
in the case above, setSpawnTime() is the custom setter.
the advantage of a custom setter is that if some action needs to happen as a result of the value changing then you can trigger that action in the setter.
05/07/2009 (12:48 pm)
addProtectedField() means when script accesses the variable, a custom setter or getter method in the C-side is executed, while addField() provides a more simple, raw access to the variable.in the case above, setSpawnTime() is the custom setter.
the advantage of a custom setter is that if some action needs to happen as a result of the value changing then you can trigger that action in the setter.
Associate Phillip O'Shea
Violent Tulip
static bool setSpawnTime( void *obj, const char *data ) { static_cast<t2dGroundUnit*>(obj)->setSpawnTime(dAtof(data)); return false; };Protected fields require a special method set up to handle the conversion from "const char *" to the required data type.
Put the above in your t2dGroundUnit.h file.