Watch the State Designer Subgraphs tutorial on YouTube.

Subgraphs store State Designer behavior in reusable ScriptableObject assets. Split behavior into a Subgraph when the same group of States, Actions, and transitions appears in several graphs, when a large graph needs a clearer high-level structure, or when the referenced behavior must be swapped.

Choose how to create the Subgraph

State Designer exposes two different editor workflows:

Workflow Result
Export in the operations toolbar Copies the entire current graph into a new Subgraph asset. The source graph remains unchanged. Use this when the whole graph should become reusable.
Right-click a State selection and choose Save as Subgraph Extracts a valid region into a new Subgraph asset and replaces that region with a Subgraph Reference. Entry and exit transitions are reconnected to the reference. Use this while refactoring part of an existing state machine.

Save selected States as a Subgraph

  1. Select every State in the region that should become reusable.
  2. Right-click the selection and choose Save as Subgraph.
  3. Choose the asset path and name.
  4. Inspect the new Subgraph Reference, its incoming and outgoing transitions, and its Shared Variable Overrides.

The command becomes available only when the selection can be represented by one reference:

  • External transitions and events enter exactly one distinct selected State. Multiple incoming connections to that same entry State are allowed.
  • Every selected State is reachable from that entry.
  • No more than one selected exit State transitions to an unselected State.
  • That exit State cannot also transition back into the selected region.

State Designer saves the selected region, replaces it with a Subgraph Reference, and preserves the valid external transition path. Shared Variables that are externally configurable can be exposed as overrides on the reference. A Graph variable marked Internal remains usable inside the Subgraph but is intentionally omitted from those override controls. See Variables for the Internal command.

If Save as Subgraph is disabled, simplify the selection to one entry and one optional exit, then check that every selected State is reachable from the entry.

Checkpoint: the parent graph enters one Subgraph Reference, the reference opens the expected selected States and transitions, and its outgoing transition returns to the same unselected State as before.

Create and reference a Subgraph

Author the reusable States and transitions in a graph, then use Export in the State Designer toolbar to create a Subgraph asset. In the parent graph, add a Subgraph Reference state and assign one or more Subgraphs to its Subgraphs list.

A Subgraph Reference state placed in a parent State Designer graph.

The Subgraph Reference state lets you assign one or more subgraphs that should be loaded when the graph initializes. By default, it uses every subgraph in the Subgraphs list.

Use Shared Variable Overrides on the reference when the parent graph should provide different values without changing the Subgraph asset.

The reusable example turns toward Target, then logs a message. Target and Turn Speed are Graph Shared Variables; the parent provides its own Target and turn speed through the reference.

Inside Turn And Log A, Start enters Face Target with RotateTowards, then Report with a Log Action.

Face Target runs RotateTowards in Sequence; Target uses the shared Target variable and Rotation Speed uses Turn Speed, with Up Vector (0,1,0), no axis lock and Arrived Angle 1.

Start enters Wait, then Reusable Sequence, then Done; the parent return transition runs below all three states.

Reusable Sequence references Turn And Log A, maps Target to Parent Target and Turn Speed to Parent Turn Speed, and uses TerminalNodes transition mode.

How it runs

When the graph initializes, State Designer replaces each Subgraph Reference with the nodes injected from its selected Subgraphs. The referenced States and Actions perform the behavior, and their transitions continue to control execution. When several Subgraphs are assigned to one reference, they are processed in list order.

If selection logic changes later, reevaluating the Subgraph References rebuilds the injected content and restarts the graph with the new selection.

Key reference and reuse choices

Runtime selection

If you want runtime selection logic, subclass SubgraphReference (or implement ISubgraphReferenceNode) and override EvaluateSubgraphs. That method runs before Subgraphs is read, so you can choose which subgraph(s) to return.

using Opsive.GraphDesigner.Runtime;
using Opsive.StateDesigner.Runtime;
using Opsive.StateDesigner.Runtime.States;
using UnityEngine;

public class RandomSubgraphReference : SubgraphReference
{
    private Subgraph[] m_Selection;

    public override Subgraph[] Subgraphs => m_Selection != null ? m_Selection : m_Subgraphs;

    /// <summary>
    /// Performs runtime selection before subgraphs are injected.
    /// </summary>
    public override void EvaluateSubgraphs(IGraphComponent graphComponent)
    {
        if (m_Subgraphs == null || m_Subgraphs.Length == 0) {
            return;
        }

        m_Selection = new[] { m_Subgraphs[Random.Range(0, m_Subgraphs.Length)] };
    }
}

In this example, EvaluateSubgraphs picks one random Subgraph and stores it in m_Selection, then the Subgraphs property returns that selection.

Variable overrides

Subgraph Reference supports SharedVariableOverride[], so a parent graph can provide values to variables defined inside the Subgraph. Each override maps:

  • Source: A variable defined in the Subgraph.
  • Override: The value or variable the parent should use instead.

This keeps one reusable Subgraph asset while allowing each parent to supply values such as different targets, speeds, timers, or flags. If an override entry is empty, the Subgraph keeps its own value for that variable.

Reevaluation

At runtime, Subgraph Reference nodes are replaced by injected subgraph nodes. If your selection logic changes later (for example, random/conditional selection), call:

m_StateMachine.ReevaluateSubgraphReferences();

This reevaluates all Subgraph References, calls EvaluateSubgraphs again, rebuilds the injected nodes, and restarts the graph with the new Subgraph selection. You can also trigger this through the built-in ReevaluateSubgraphReferences Action.

Pooling

If you frequently switch subgraphs at runtime, pooling can reduce overhead. The key pattern is:

  1. Instantiate subgraph instances ahead of time.
  2. Call Deserialize() on each instance.
  3. Set Pooled = true.
  4. Reuse those instances instead of creating new ones on demand.
using Opsive.StateDesigner.Runtime;
using UnityEngine;

public class SubgraphPooler : MonoBehaviour
{
    [Tooltip("A reference to the state machine that receives pooled subgraphs.")]
    public StateMachine m_StateMachine;
    [Tooltip("The subgraph asset to clone into the pool.")]
    public Subgraph m_Subgraph;
    [Tooltip("Pool size.")]
    public int m_PoolSize = 5;

    private Subgraph[] m_SubgraphPool;
    private int m_Index;

    private void Awake()
    {
        m_SubgraphPool = new Subgraph[m_PoolSize];
        for (int i = 0; i < m_PoolSize; ++i) {
            m_SubgraphPool[i] = Instantiate(m_Subgraph);
            m_SubgraphPool[i].Deserialize();
            m_SubgraphPool[i].Pooled = true;
        }
    }

    public void Assign()
    {
        m_StateMachine.Subgraph = m_SubgraphPool[m_Index];
        m_Index = (m_Index + 1) % m_SubgraphPool.Length;
    }
}

If you are swapping Subgraphs through Subgraph Reference selection logic, return pooled Subgraphs from EvaluateSubgraphs and call ReevaluateSubgraphReferences() when you want the switch to happen.

Verify the result

Enter Play mode and confirm that:

  • The parent graph runs the States from the assigned Subgraph.
  • Each Shared Variable Override supplies the expected value, while an empty override retains the Subgraph default.
  • Changing runtime selection and reevaluating the references runs the newly selected Subgraph content.

Related:

Settings reference

Component or task Setting Default Description
Subgraph settings Data new StateMachineData() The state machine data.