Add data objects at runtime

Workflow

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.

[EDITOR] Add DataObjects to the runtime DataLibrary

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:

[DataObjectAddToRuntimeLibrary]
public class EnemyData : DataObject
{
    public int health;
    public float strength;
}

Databrain comes with multiple useful attributes for your DataObject class. See the attributes section for more information.

pageDataObject attributes

[RUNTIME] Add DataObjects to the Runtime DataLibrary dynamically

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.

public class AddToRuntimeLibrary : MonoBehaviour
{
    // The data library reference
    public DataLibrary data;
    
    // The data object we want to clone and add to the runtime library
    public EnemyData enemyData;
    
    public void Start()
    {
        // Always make sure data is initialized before accessing it
        data.OnDataInitialized += Ready;
    }
    
    void Ready()
    {
        // Clone the data objects enemyData and add it to the runtime library
        var _clonedData = data.CloneDataObjectToRuntime(enemyData, this.gameObject);
        
        // We can now modify the runtime data
        (_clonedData as EnemyData).health = 100;
        
        // Get runtime clone associated to this game object
        // var _clonedData = enemyData.GetRuntimeDataObject(this.gameObject);
        
    }
}

Serialize runtime data

In order to make sure your data is being serialized, you need to add support for serialization to your DataObject class. Please read:

pageSerialize DataObjects

Last updated