Execute graph

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);
}

Last updated