API ReferenceGE Extensionsflowgraph
Flowgraph States Manager
- **File:** `extensions/flowgraph/states.lua`
Overview
- File:
extensions/flowgraph/states.lua
The States manager handles the flowgraph state machine system. It builds, starts, stops, and transitions between states defined in a state graph. States are subgraphs that run independently with their own extension proxies and hooks.
Exports
Returns a constructor function function(mgr) that creates a new States instance bound to a flowgraph manager.
Key Methods
| Method | Signature | Description |
|---|---|---|
init | (mgr) | Initialises the states manager with empty state tables. |
destroy | () | Calls clear(). |
clear | () | Destroys all extension proxies, clears all states, and refreshes dependencies. |
buildStates | () | Main entry point: gathers nodes/links, resolves inter-hops, builds ports, creates states. |
addStateFromPortedNode | (pnode) | Creates a state from a processed node with its own extension proxy. |
startAutoStartStates | () | Starts all states flagged as auto-start. |
startState | (id, transData) | Activates a state, submits its hooks, fires onStateStarted events. |
stopState | (id) | Deactivates a state, clears its hooks, fires onStateStopped events. |
transition | (sourceStateId, transitionName, transitionData, sourceNode) | Queues a transition for end-of-frame resolution. |
resolveTransitions | () | Processes all queued transitions - stops source states, starts target states. |
isRunning | (id) → bool | Checks if a state is currently active. |
getTransitionStack | (id) → table | Returns the accumulated transition data for a state. |
broadcastCall | (functionName, ...) | Calls a hook function on all active states. |
broadcastCallReturn | (functionName, results, ...) | Calls a hook function on all states, collecting results. |
hasNodeForHook | (functionName) → bool | Checks if any state has a node that handles a given hook. |
Internals
State Structure
Each state contains:
{
id = nodeId, -- unique state ID (from the state node)
depth = {...}, -- graph depth path for group-state nesting
graph = targetGraph, -- the subgraph this state executes
name = "StateName",
autoStart = bool, -- whether to start automatically
extProxy = proxy, -- extension proxy for hook isolation
active = bool, -- current running status
ports = {}, -- transition ports (name → {targets, stopDepth})
transitionStack = {}, -- accumulated data passed through transitions
}Build Pipeline
gatherNodesAndLinks()- Recursively collects all nodes and links from the state graph hierarchy into flat lists.resolveInterHops()- Flattens connections through group-state connectors (entry/exit nodes) into direct "virtual links".buildPortsAndStarts()- Assigns transition ports to each state node and identifies auto-start states.addStateFromPortedNode()- Creates runtime state objects with extension proxies.
Transition Resolution
Transitions are queued during execution and resolved at frame end via a dedicated extension proxy:
- All queued transitions are processed to determine which states turn on/off.
- States exiting group-states trigger all nested states to stop.
- Off-states are stopped first (sorted by ID), then on-states are started.
- If a final exit node is reached with no new states starting, the project stops.
- Transition data is accumulated in each state's
transitionStack.
How It Works
- The flowgraph manager calls
buildStates()when a state graph project starts. - States are built from the state graph's node/link structure, flattening group-state hierarchies.
- Each state gets its own
extensionProxyfor isolated hook management. - A resolver extension proxy runs every frame to process queued transitions.
- State transitions are triggered by nodes calling
states:transition(sourceId, name, data). - The resolver stops source states and starts target states atomically at frame end.
Usage Example
-- States are managed internally by the flowgraph system.
-- Nodes trigger transitions like:
self.mgr.states:transition(stateId, "success", {score = 100})
-- Checking if a state is running:
if self.mgr.states:isRunning(someStateId) then
-- state is active
end
-- Getting accumulated transition data:
local stack = self.mgr.states:getTransitionStack(stateId)
local score = stack.scoreKey Dependencies
core_flowgraphManager- proxy registration and dependency managementnewExtensionProxy()- creates isolated extension hook proxiesextensions.refresh()- refreshes hook routing after proxy changes
See Also
- Flowgraph Base Module - Related reference
- Flowgraph Base State Node - Related reference
- Flowgraph Base Node - Related reference
- FlowGraph Guide - Guide