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
  • Register Listener
  • UnregisterListener
  • Raise Event
  • Custom event object
  • Global DatabrainEvent
  1. Add-ons

Events

The event add-on is included by default in the Databrain package

The Event add-on is an object-based generic event system. It supports class types as parameters.

Register Listener

Register a listener to the event.

databrainEvent.RegisterListener(Action<T> _data);

UnregisterListener

remove listener from event

databrainEvent.UnregisterListener(Action<T> _data);

Raise Event

Raise an event.

databrainEvent.Raise(T);

Custom event object

Here's an example of a custom event object, including a parameter class

public class MyEvent : DatabrainGenericEvent<MyEventData>{}

// Event parameter class
public class MyEventData
{
    public float param1;
    public string param2;
    
    // Constructor
    public MyEventData(){}
}

You can now use this event like this:

EventCaller.cs

// Event caller example
public class EventCaller : MonoBehaviour
{
    public DataLibrary data;
    
    [DataObjectDropdown("data")]
    public MyEvent myEvent;
    
    // Using IMGUI for testing purposes only
    public void OnGUI()
    {
        if (GUILayout.Button("Call Event"))
        {
            // Create parameters
            var _params = new MyEventData();
            _params.param1 = 99.9f;
            _params.param2 = "My Event";
            // Raise event
            myEvent.Raise(_params);
        }
    }
}

EventListener.cs

public class EventListener : MonoBehaviour
{
    public DataLibrary data;
    
    [DataObjectDropdown("data")]
    public MyEvent myEvent;
    
    public void Start()
    {
        // As with all DataObjects we have to make sure the DataLibrary is ready
        // before using it.
        data.RegisterInitializationCallback(Ready);
    }
    
    void Ready()
    {
        // DataLibrary is ready. We can now register to the event
        myEvent.RegisterListener(EventCalled);
    }
    
    void EventCalled(MyEventData _data)
    {
        // Event has been called
        Debug.Log("EVENT CALLED " + _data.param1 + " _ " + _data.param2);
    }
}

Global DatabrainEvent

You can also use the base type - DatabrainEvent for global events which do not require any parameters.

public DatabrainEvent myEvent;

myEvent.Raise();
PreviousDatabrain - FAQNextBlackboard

Last updated 1 year ago