Get initial data

Accessing data at runtime can be done in several ways.

Please note that you should only modify runtime-DataObjects at runtime. Those runtime-DataObjects are being serialized when saving to a file. See -> Add data objects at runtime

Get initial data by referencing the data object

The simplest way would be to reference your data object in your script so let’s assume we have an EnemyData object which looks like this:

public class EnemyData : DataObject
{
    public int health;
}

Now, in our Monobehaviour script, we can add the following reference:

public class Enemy : Monobehaviour
{
    // Our data object 
    public EnemyData enemyData;
}

You can now simply assign the EnemyData object by dragging the object from the Databrain editor to the Enemy script inspector field.

Using the dropdown attribute

pageDataObject Property Drawer

By using the dropdown attribute, you can use additional features such as quickly unassigning an object or quickly creating a new DataObject of type EnemyData directly from the field. To do this, simply add the DropdownObjectAttribute to your EnemyData field like this:

public class Enemy : Monobehaviour
{
    public DataLibrary data;

    // Our data object 
    [DataObjectDropdown("data")]
    public EnemyData enemyData;
}

Please note that we have to add an additional reference to our Data Library object and also have to pass in the name of the data library as parameter to the DataObjectDropdown attribute.

We can now access the data directly from the data object like this:

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.
        // Therefore we're registering a method to the initialization callback.
        data.RegisterInitializationCallback(Ready);     
    }
    
    // Data is ready
    void Ready()
    {
        // Get the initial health value
        var enemyHealth = enemyData.health;
    }
}

Please note that before we can access the data at runtime, we have to make sure the data is initialized. This can be done by registering a method to the initialization callback via RegisterInitializationCallback.

Get initial data directly from the DataLibrary object

It is also possible to get the data object directly from the DataLibrary object. In this case you simply have to reference the DataLibrary object as follow:

public class Enemy : MonoBehaviour
{
    public DataLibrary data;
}

You can now access the data object like this:

public class Enemy : MonoBehaviour
{
    public DataLibrary data;
    
    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()
    {
        // Multiple ways for accessing data object at runtime
        
        // 1.
        // Get the enemy data object by Guid.
        // You can optionally pass the data type.
        var _enemyData1 = data.GetInitialDataObjectByGuid("", typeof(EnemyData))  as EnemyData;
        // Get health value;
        var _health = _enemyData1.health;
        
        // 2.
        // Get the enemy data object by its title.
        // In this case you have to make sure that the title is unique.
        // You can optionally pass the data type.
        var _enemyData2 = data.GetInitialDataObjectByTitle("Enemy", typeof(EnemyData))  as EnemyData;
          // Get health value;
        var _health = _enemyData1.health;
        
        // 3.
        // Get all data objects of the type
        var _allEnemies = GetAllInitialDataObjectsByType(typeof(EnemyData));
        // Iterate through all enemies
        for (int i = 0; i < _allEnemies.Count; i ++)
        {
            var _enemy = _allEnenmies[i] as EnemyData;
        }
    }
}

API

Please head over to the API section of the DataLibrary to see more methods for accessing data from the DataLibrary object.

pageCustom GUI

Last updated