# Add data objects at runtime

## Workflow

When using data and modifying data at runtime, it is important to understand the following workflow.

<figure><img src="https://2348672745-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIoqF5QAgQsqxrjmTIKpE%2Fuploads%2FG2xiLKFrLe46vrNBPsEu%2Fdatabrain_runtime-data-workflow.png?alt=media&#x26;token=830554bc-4e76-41a4-b74b-c39dbb5ea0c2" alt=""><figcaption></figcaption></figure>

{% hint style="warning" %}
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](serialize-dataobjects))
{% endhint %}

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.

<div align="left"><figure><img src="https://2348672745-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIoqF5QAgQsqxrjmTIKpE%2Fuploads%2FtBtSbXBGz0qXeU3SAfFr%2FopenRuntimeLibrary.png?alt=media&#x26;token=53f5fab9-1dee-4056-af12-59771aa2f06a" alt=""><figcaption></figcaption></figure></div>

## \[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.

<div align="left"><figure><img src="https://2348672745-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIoqF5QAgQsqxrjmTIKpE%2Fuploads%2FdgFjI7GlKEqGDdoQNKhL%2FaddToRuntimeLibrary.png?alt=media&#x26;token=a50b364d-3683-4b58-ad86-bcc24a3716a4" alt=""><figcaption></figcaption></figure></div>

Alternatively, you can also add the **\[DataObjectAddToRuntimeLibrary]** attribue to your DataObject class like this:

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

{% hint style="info" %}
Databrain comes with multiple useful attributes for your DataObject class. See the attributes section for more information.
{% endhint %}

{% content-ref url="../attributes/dataobject-attributes" %}
[dataobject-attributes](../attributes/dataobject-attributes)
{% endcontent-ref %}

## \[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:

{% hint style="warning" %}
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.
{% endhint %}

```csharp
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:

{% content-ref url="serialize-dataobjects" %}
[serialize-dataobjects](serialize-dataobjects)
{% endcontent-ref %}
