Serialization

Classes that derive from Serializable can perform automatic serialization to binary or XML format by defining attributes. Attributes are stored to the Context per class. Scene load/save and network replication are both implemented by having the Node and Component classes derive from Serializable.

The supported attribute types are all those supported by Variant, excluding pointers. Attributes can either refer to a variable at a memory offset in the object, or define setter & getter functions. Zero-based enumerations are also supported, so that the enum values can be stored as text into XML files instead of just numbers. For editing, the attributes also have human-readable names.

To implement side effects to attributes, for example that a Node needs to dirty its world transform whenever the local transform changes, the default attribute access functions in Serializable can be overridden. See OnSetAttribute() and OnGetAttribute().

Each attribute can have a combination of the following flags:

  • AM_FILE: Is used for file serialization (load/save.)
  • AM_NET: Is used for network replication.
  • AM_LATESTDATA: Frequently changing data for network replication, where only the latest values matter. Used for motion and animation.
  • AM_NOEDIT: Is an internal attribute and is not to be shown for editing.
  • AM_NODEID: Is a node ID and may need rewriting when instantiating scene content.
  • AM_COMPONENTID: Is a component ID and may need rewriting when instantiating scene content.

The default flags are AM_FILE and AM_NET. Note that it is legal to define neither AM_FILE or AM_NET, meaning the attribute has only run-time significance (perhaps for editing.)

See the existing engine classes e.g. in the Scene or Graphics subdirectories for examples on registering attributes using the URHO3D_ATTRIBUTE family of helper macros.