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
  • Direct execution
  • Logic controller
  • Manually Execute
  • Return value
  • Unity OnComplete Events
  1. Add-ons
  2. Logic

Execute graph

PreviousCreate GraphNextControl flow execution in groups

Last updated 9 months ago

A Logic graph can be executed directly from the DataObject (not scene dependent) or from the LogicController component (scene dependent).

If your graph has component references, you'll have to execute the graph from the LogicController component. Otherwise, nodes that need those references won't find them.

Direct execution

The Following code executes the graph above directly:

public class ExecuteGraph : MonoBehaviour
{
    public DataLibrary data;
    
    [DataObjectDropdown("data")]
    public Logic graph;
    public void Start()
    {
        data.OnDataInitialized += Ready;
    }
    
    void Ready()
    {
        graph.ExecuteGraph().OnComplete(result => 
        {
            // Get the result value from the return node on completion.
            Debug.Log("Graph complete. Result is: " + result.boolValue);
        });
    }
}

Direct execution also allows you to execute graphs in your custom DataObject classes.

Logic controller

The LogicController lets you assign a graph to a game object. A graph executed from the LogicController component runs as a unique instance in the scene. Therefore you can have multiple graph instances in a scene each running a copy of the same graph.

When using component references in a graph you will have to run the graph by a LogicController component to make sure you can assign the references.

Simply assign the LogicController component from the Unity inspector to a GameObject. Assign the DataLibrary and select the graph asset.

Manually Execute

You can manually start an execution from the LogicController. Simply uncheck the option: Execute on start.

public class ExecuteGraphExample : MonoBehaviour
{
    // Reference to our LogicController component
    public LogicController logicController;
    
    // Call this method manually
    void ExecuteManually()
    {
        logicController.ExecuteGraph();
    }
    
    void ExecuteManuallyWithOnComplete()
    {
        logicController.ExecuteGraph().OnComplete(result =>
        {
            // Get the result value from the return node on completion.
            Debug.Log("Graph complete. Result is: " + result.boolValue);       
        });
    }
}

Return value

The return value node lets you return a value to the OnComplete event.

Execute the graph and get the returning result:

void ExecuteManuallyWithOnComplete()
{
    logicController.ExecuteGraph().OnComplete(result =>
    {
        // Get the result value from the return node on completion.
        Debug.Log("Graph complete. Result is: " + result.boolValue);       
    });
}

Unity OnComplete Events

You can also call custom methods on the graph OnComplete event by using Unity Events. Create a method which takes the GraphData.GraphProcessingResult parameter and assign it to the Unity OnComplete Events list on the LogicController component.

public void UnityEventOnComplete(GraphData.GraphProcessingResult _result)
{
    // Graph is finished
    Debug.Log(_result.boolValue);
}