addProtectedField
Using addProtectedField
The addProtectedField
function is used to define a new field for an object class, which can be accessed and modified in scripts and in the engine's GUI editor. The main difference between addField
and addProtectedField
is the latter controls access of the class object's variable. The basic syntax for using addProtectedField
is as follows:
where:
name
is the name of the field as it will appear in the editor, or used in scripts.type
is the data type of the field, such asTypeS32
,TypeF32
,TypeString
, etc.fieldName
is the name of the class' attribute variable being exposed.className
is the name of the class the field is being exposed for.setFunction
is the name of a custom set function that is called whenever the value of the field changes.getFunction
is the name of a custom get function that is called whenever the value of the field is accessed.writeFunction(optional)
is the name of a custom write function that is called whenever the value of the field is written out to a file as part of serialization.description
is a string that provides a brief description of the field, which will be displayed in the editors.
Example
Let's say we want to add a protected field to an object class in Torque3D that represents the object's name. The following code shows how to do this using the addProtectedField
function:
In this example, we are adding a new protected field to the MyObjectClass
object class, with the data type TypeString
, defining the offset for the MyObjectClass
and mHeight
variable, custom set and get functions setName
and getName
, and a description of "The name of the object".
For simplicity, and because it is not commonly used, we ignored the writeFunction
in this example.
It's important to note that, similar to how initPersistFields()
itself is a static function because it is invoked for the class configuration, not a specific object itself, the set, get, and write functions called back are also defined as static in the class header.
This is why you see that there is a static_cast
applied to go from the void*
to the actual object being modified.
With this implementation, the protected field can be accessed and modified both in the engine's editor and in scripts, and the custom set and get functions ensure that the value of the field is properly set and retrieved.
Last updated