Skip to content

NPC state machines ​

States can also run behavior trees to choose NPC work within a mode using the same memory and task cancellation rules.

Add AI Brain to an NPC in the Inspector. Choose Blank state machine or Guard patrol, then New behavior. Save creates a reusable .opbrain asset and assigns it to that NPC. Other NPCs can select the same Behavior asset. Edit behavior, or double-click the asset in the Library, opens Flow → Behavior. You can also create an unassigned asset from that workspace's empty state. Switching to Arrange or another workspace keeps the graph and its draft open. The normal Save command includes Behavior drafts; Undo and Redo act on the Behavior document while its workspace is active. Close behavior closes the document, with Save and Discard choices for unsaved edits.

Use Add state, right-click, or Shift+A to search for states and actions. Drag from a state's Next port to another state's Enter port to create a transition. Drag from Any state for an interrupt that applies throughout the machine. New connections ask for an event; click the wire to choose its trigger, conditions, or delay. A missing event must be filled in before saving.

Click a wire to inspect it. Drag either selected endpoint to reconnect it, or press Delete to disconnect. Reconnecting preserves the trigger, conditions, and priority. States support loops and multiple transitions between the same pair. Keyboard users can focus a port, press Enter, then focus the target port and press Enter again; Escape cancels.

Select a state to name it, choose its parent and initial child, and configure entry actions, its running task, and exit actions. Searchable action pickers list OpalScript methods alongside built-in actions. Numbers, booleans, vectors, entities, and assets use typed controls; choose Memory beside an argument to bind a compatible NPC variable. Edit script opens the method's source. See custom OpalScript nodes for the creation workflow and a complete example.

The Connection inspector defines conditions, events, delays, or task outcomes. Use Earlier and Later to change priority; wiring never rearranges existing transition priorities. An eligible interrupt is evaluated before task completion. Among eligible interrupts, the first transition in the list wins, including ancestor and Any State transitions. At most one transition runs per decision frame. Place death and stun interrupts before ordinary decisions. A transition from a parent exits its active descendants; state tasks and asynchronous entry actions are canceled before the next state enters. Exit actions should finish synchronously. Task failure and cancellation stay distinguishable in the live trace; cancellation does not automatically take a failure transition.

Memory variables have number, boolean, text, vector, or object-reference types. Every NPC receives an independent copy. Initial values can be overridden in its Inspector; Reset initial values restores the current behavior's defaults and removes obsolete overrides. Flow can call Send Behavior Event, Set Behavior Number, Set Behavior Boolean, Set Behavior Text, Set Behavior Vector, or Set Behavior Object. State Entered and State Exited expose stateId and name; Is in State checks the active state and its ancestors.

In Play, open a behavior from the NPC's Inspector and choose Live to inspect its active state, memory, task outcomes, and recent decisions. Saved definition revisions restart the affected machine at a decision boundary. Invalid asset syntax or schema leaves the existing machine active and displays an error. Renaming variables or changing their types can invalidate an NPC's initial overrides and prevent restart until those overrides are corrected or reset. Stop and scene teardown cancel all owned work. Pausing an NPC's time group pauses decisions and waits.

Guard patrol ​

Attach an AI Mover and a Character Controller set to Controlled By: AI. Create a Guard patrol behavior. Set patrolLeftX, patrolRightX, patrolY, targetX, and targetY in the NPC's initial values. These are movement coordinates for AI Mover. Send alert to leave patrol and approach the target point, stun to interrupt for one second, or die to enter Dead permanently.

Attack windup lasts 0.4 seconds, then the guard enters strike. Connect AI Brain's State Entered event, with stateId equal to strike, to your game's damage or projectile action. Interrupting windup prevents this strike state from being entered. Acquiring a target, updating its coordinates, and deciding when to send alert belong to the game's perception logic. The template approaches a point; it does not automatically track a moving target or inflict damage.

While AI Brain controls an NPC, it suppresses AI Mover's autonomous patrol/follow/ flee fallback. Owned move tasks continue normally. Disabling Brain stops its work and retains this suppression until the component retires, preventing an old configured chase from unexpectedly resuming.

Behavior source assets, including typed asset references in action parameters, round-trip through server, IndexedDB, desktop folders, and exported game packages. Saving a shared behavior changes every NPC referencing that asset. A concurrent save to a newer revision is refused; reopen the asset before retrying.