Add data objects at runtime
Last updated
Last updated
When using data and modifying data at runtime, it is important to understand the following workflow.
When creating a new DataLibrary, Databrain also creates a secondary internal Runtime Data Library. You should only modify runtime-DataObjects at runtime. Every DataObject inside of the runtime DataLibrary is being serialized. (If the DataObject fields are marked as serializable (attribute: [DatabrainSerialize]) -> Serialize DataObjects)
Only data types which are in the Runtime DataLibrary are being serialized to Json when using the Save / Load API. This is to make sure that only modifiable DataObjects are being added to the Json save file which prevents the file from including unnecessary data so that the file doesn't get too large.
The Runtime DataLibrary can be accessed in the save settings of the Databrain Editor.
Only data objects of data types which are marked as “add to runtime serialization” are being added to the runtime data library. To automatically add your custom data object types to the runtime library, simply go to the save settings menu and check the "Add to runtime DataLibrary" option. You can now see the save icon next to your data type. Databrain will then automatically clone the DataObject to the runtime DataLibrary on start.
Alternatively, you can also add the [DataObjectAddToRuntimeLibrary] attribue to your DataObject class like this:
Databrain comes with multiple useful attributes for your DataObject class. See the attributes section for more information.
If you're instantiating objects at runtime and want to make sure, that each dynamically created object has its own runtime data object, then you can clone an initial data object to the runtime data library using the CloneDataObjectToRuntime method. See following code example:
Keep in mind, an initial DataObject can have multiple runtime clones. If you create multiple runtime clones at runtime, make sure to pass the owner game object to the CloneDataObjectAtRuntime method. This allows Databrain to later find the runtime clone associated to the owner game object.
In order to make sure your data is being serialized, you need to add support for serialization to your DataObject class. Please read: