Get/Set runtime data

Due to the way scriptable objects work, you can only modify runtime-DataObjects at runtime. Runtime changes to an initial scriptable object (non-runtime object) won't persist.

Get/Set runtime DataObject from initial DataObject

If your DataObject has been added to the runtime DataLibrary as described in (Add data objects at runtime) then you can access the runtime DataObject from the initial DataObject (which was created in the editor) using GetRuntimeDataObject. See the following code:

public class Enemy : MonoBehaviour
{
    public DataLibrary data;
    
    // Our data object
    [DataObjectDropdown("data")]   
    public EnemyData enemyData;
    
    public void Start()
    {
        // Before accessing the data we have to make sure the data library is ready
        data.RegisterInitializationCallback(Ready);     
    }
    
    // Data is ready
    void Ready()
    {
        // Get the runtime data object from the initial enemyData DataObject.      
        var _runtimeEnemyData = (enemyData.GetRuntimeDataObject() as EnemyData)
         // Modify the health value of the runtime data object.
        _runtimeEnemyData.health -= 10;
    }
}

Get/Set dynamic runtime DataObjects

The method CloneDataObjectToRuntime returns the newly created runtime data object. It is advised that you keep a reference of this dynamically added runtime data object to be able to modify its values.

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
    [DataObjectDropdown("data")]
    public EnemyData enemyData;   
    // Reference of the enemy class
    public Enemy enemy;
    
    public void Start()
    {
        // Clone the data object enemyData and add it to the runtime library.
        // Pass this game object as the owner. This is important if you have
        // multiple clones of enemyData. This allows you and Databrain to later
        // find the enemyData runtime clone associated to this game object.
        // It also helps the DataObjectDropdown attribute to find the correct 
        // values from the runtime clone
        enemy.runtimeData = data.CloneDataObjectToRuntime(enemyData, this.gameObject) as EnemyData;
        
        /* Get the runtime clone by providing this game object.
        *
        * enemy.runtimeData = enemyData.GetRuntimeDataObject(this.gameObject);
        *
        */
    }
}


public class Enemy : MonoBehaviour
{
    public EnemyData runtimeData;
    
    public void Hit()
    {
        runtimeData.health -= 10;
        
        if (runtimeData.health <= 0)
        {
             // Dead   
        }
    }
}

Demo

Please also have a look at the demo scene in the Databrain package which demonstrates how to dynamically create DataObjects, change their values and restore the objects at runtime.

Last updated