# 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

<div align="left"><figure><img src="https://2348672745-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIoqF5QAgQsqxrjmTIKpE%2Fuploads%2F08R2JDglIyrJggDWMSW0%2FsimpleGraph.png?alt=media&#x26;token=eaa6c882-7d48-4459-8ff7-95bce7b6747c" alt=""><figcaption></figcaption></figure></div>

The Following code executes the graph above directly:

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

{% hint style="info" %}
Direct execution also allows you to execute graphs in your custom DataObject classes.
{% endhint %}

## 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.

<div align="left"><figure><img src="https://2348672745-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIoqF5QAgQsqxrjmTIKpE%2Fuploads%2FVtnuSucOPOlOSBNmTGNO%2FlogicController.png?alt=media&#x26;token=1b31c1eb-77a6-4a96-bfec-a60bc40b5ca9" alt=""><figcaption></figcaption></figure></div>

### Manually Execute

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

<pre class="language-csharp"><code class="lang-csharp">public class ExecuteGraphExample : MonoBehaviour
{
<strong>    // Reference to our LogicController component
</strong>    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);       
        });
    }
}
</code></pre>

## &#x20;Return value

<div align="left"><figure><img src="https://2348672745-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIoqF5QAgQsqxrjmTIKpE%2Fuploads%2FRlYfCjfezOrhw9IN5y2w%2FreturnNode.png?alt=media&#x26;token=bb01438a-bb67-4cbd-bc2d-6b16fd858451" alt=""><figcaption></figcaption></figure></div>

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

<div align="left"><figure><img src="https://2348672745-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIoqF5QAgQsqxrjmTIKpE%2Fuploads%2FOwnAbD9FJah4DOq4BJYc%2FhasItemGraph.png?alt=media&#x26;token=3db6d360-fcdb-47bd-a9a6-0978216711ab" alt=""><figcaption></figcaption></figure></div>

Execute the graph and get the returning result:

```csharp
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

<div align="left"><figure><img src="https://2348672745-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIoqF5QAgQsqxrjmTIKpE%2Fuploads%2FFwrGqpnUv8CtzUbD50I5%2FlogicController_unityEvents.png?alt=media&#x26;token=23d6084a-20a5-4051-95a6-ace54669d200" alt=""><figcaption></figcaption></figure></div>

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.&#x20;

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