Databrain Documentation
databrain.ccDiscord
  • Welcome
  • Installation / Update
  • Interface
  • DataObject Property Drawer
    • No GUI Implemented - FIX
  • Getting started
  • Guides
    • Add data objects
    • Add data objects at runtime
    • Get initial data
    • Get/Set runtime data
    • Serialize DataObjects
    • Runtime Save & Load
    • Use custom serializer
    • Import
    • Search
    • Custom GUI
    • Custom GUI with Odin Inspector
    • Hierarchy Template
  • Separate DataObjects
  • Using Version Control
  • Attributes
    • DataObject attributes
    • Field attributes
  • API
    • DataLibrary
    • DataObject
  • FAQ
    • Databrain - FAQ
  • Add-ons
    • Events
    • Blackboard
    • Logic
      • Interface
      • Create Graph
      • Execute graph
      • Control flow execution in groups
      • Custom Nodes
        • Asynchronous execution
        • Node Attributes
      • Scene Components
      • Graph Events
      • Finite State Machine
        • Create custom Actions
        • Examples
        • State Machine Nodes
    • Stats
      • Achievements
      • Modifiers
      • Values
      • Progressions
      • Components
      • Nodes
    • Progress
      • Progress Settings
      • Progress Graph
      • Progress Resources
      • Runtime UI Setup
      • API
        • ProgressController
    • Techtree (LEGACY)
      • Techtree Manager
      • Techtree
      • TechtreeResource
      • Techtree UIBuilder
        • Custom Techtree Node Button
        • Tooltip
      • API
    • Localization
      • Localization Manager
      • Localization
      • Examples
      • Components
      • Import
      • API
    • Inventory
      • Getting started
      • Demo
      • Data setup
        • Inventories
        • Slot IDs
        • Items
          • Blueprints
          • Item rarities
        • Money
        • Loot tables
        • Events
      • Runtime UI
        • Setup
    • Dialogue
      • Getting Started
        • Demo
      • Dialogue runtime setup
      • Actors
        • Emotions
      • Localization
      • Audio
      • Animation
      • Variables
      • Custom theme
      • Import
      • Nodes
      • API
        • DialogueController
        • IDialogueUIControl
  • Changelog
    • Databrain - Changelog
      • 1.4.0
      • 1.3.2
      • 1.3.1
      • 1.3.0
      • 1.2.0
      • 1.1.0
      • 1.0.10
      • 1.0.9
      • 1.0.8
      • 1.0.7
      • <= 1.0.6
Powered by GitBook
On this page
  • Workflow
  • [EDITOR] Add DataObjects to the runtime DataLibrary
  • [RUNTIME] Add DataObjects to the Runtime DataLibrary dynamically
  • Serialize runtime data
  1. Guides

Add data objects at runtime

PreviousAdd data objectsNextGet initial data

Last updated 1 month ago

Workflow

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

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.

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

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
DataObject attributes
Serialize DataObjects