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:
Now, in our Monobehaviour script, we can add the following reference:
publicclassEnemy:Monobehaviour{ // Our data object publicEnemyData 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
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:
publicclassEnemy:Monobehaviour{publicDataLibrary data; // Our data object [DataObjectDropdown("data")]publicEnemyData 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:
publicclassEnemy:MonoBehaviour{publicDataLibrary data; // Our data object [DataObjectDropdown("data")] publicEnemyData enemyData;publicvoidStart() { // 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 readyvoidReady() { // Get the initial health valuevar 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:
publicclassEnemy:MonoBehaviour{publicDataLibrary data;publicvoidStart() { // Before accessing the data we have to make sure the data library is readydata.RegisterInitializationCallback(Ready); } // Data is readyvoidReady() { // 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)) asEnemyData; // 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)) asEnemyData; // Get health value;var _health =_enemyData1.health; // 3. // Get all data objects of the typevar _allEnemies =GetAllInitialDataObjectsByType(typeof(EnemyData)); // Iterate through all enemiesfor (int i =0; i <_allEnemies.Count; i ++) {var _enemy =_allEnenmies[i] asEnemyData; } }}
API
Please head over to the API section of the DataLibrary to see more methods for accessing data from the DataLibrary object.