Skip to content
Bit Quirky Round Trip
Visual Scripting

Search documentation

All topics and APIs

Close returns focus to Search.

What are you looking for?

Try a common topic or a C# feature.

Browse the reference instead

Esc closes · Tab reaches results · Enter opens the focused link

v1.0.0
Menu

Switch on a state

Build a switch node on the canvas with one case per enemy state, then wire each state's calls to its own case.

When one state decides what an object does, a switch gives each state its own output and its own row of calls. On the canvas it is a yellow node with SWITCH in its header, a Selection pin that takes the state, and one white output per case. In the Platformer sample, an enemy can be patrolling, stunned, or defeated, and each case answers one question: what should this enemy do now?

This page builds that shape from an empty canvas. It uses the sample’s EnemyStateMachine class so every name on the nodes is real. In your own project, build the same shape in any class that holds a state, once its folder is indexed.

Changing an enemy’s state touches several of its parts at once: patrol movement, the body collider, and the defeated look. Each state turns those parts on or off in its own way, and the switch keeps each state’s calls together where you can see them.

  • An enemy that patrols, chases, or is stunned needs different behavior in each state.
  • A pickup does something different for each item type: coin, heart, or key.
  • A menu, a door, or a boss fight moves through named phases, and each phase sets things up its own way.

Switch on the enemy state

  1. Create ShowState

    Open Tools > Round Trip > Graph view, press Cmd/Ctrl+O and choose EnemyStateMachine. Press the + on the Methods section of the left rail, type ShowState in the name box and press Enter. The canvas shows a magenta entry node titled ShowState with an Exec pin.

    Clip: Ctrl+O opens the class picker, EnemyStateMachine is chosen, + on Methods adds a method, ShowState is typed and Enter creates it. While the + is pressed, it is highlighted with an arrow pointing at it. A magenta ShowState entry node lands alone on the canvas with an Exec pin.
  2. Place the switch

    Right-click blank canvas and pick switch under flow control. A yellow node with SWITCH in its header appears, with Exec and Selection on its left and Add pin at its foot. Wire the entry node’s Exec into the switch’s Exec.

    Clip: the pointer right-clicks blank canvas, types switch and clicks the switch row, and a yellow SWITCH node lands. While the pointer rests on the switch row, that row is highlighted with an arrow pointing at it. A white wire is dragged from the entry node Exec pin to the switch Exec pin. The entry Exec pin is highlighted while the pointer presses it and the drag starts, and the switch Exec pin is highlighted while the wire lands on it.
  3. Feed it the state

    Give it the state. Right-click, type CurrentState, and press get on its row. Drag the CurrentState pill onto the switch’s Selection pin.

    Clip: the pointer right-clicks blank canvas and the menu lists the CurrentState variable first. While the pointer rests on the get button of its row, that button is highlighted with an arrow pointing at it, and a click on it places the purple CurrentState pill. The pill is dragged onto the switch selection pin and a wire joins them. The pill is highlighted while its output is pressed, and the selection pin is highlighted while the wire lands on it.
  4. Add the cases

    Press Add pin on the switch node three times.

    The switch node after Add pin was pressed three times, with the Add pin control at the foot of the node highlighted and an arrow pointing at it: three unnamed white outputs sit above Completed, with the CurrentState pill wired into selection.
  5. Name the cases

    Name the cases. Right-click the switch node and choose Edit switch cases. The Switch cases panel lists the cases by number, each with a Matches box and a When guard box. Type EnemyState.Patrolling into the first Matches box, EnemyState.Stunned into the second and EnemyState.Defeated into the third. Click outside the panel to close it. The node’s outputs now carry the state names.

    Clip: the Switch cases panel is open on the canvas, over the switch node. EnemyState.Patrolling, EnemyState.Stunned and EnemyState.Defeated are typed into the three Matches boxes. Each Matches box is highlighted with an arrow pointing at it from when the pointer reaches it until its text is typed. Then a click outside closes the panel and the node outputs carry the three state names.
  6. Patrolling

    Patrolling. Right-click blank canvas, type SetPatrol and click the SetPatrolEnabled row. A bright teal node appears, its Target showing self, with an isEnabled checkbox. Tick isEnabled and wire the first case output into the node’s Exec. Place a SetBodyColliderEnabled node the same way and tick its isEnabled. Then wire the first node’s Exec output into it.

    Clip: SetPatrolEnabled is placed from the canvas menu with its isEnabled box ticked, and the Patrolling output is wired into it. While the pointer rests on the menu row, that row is highlighted with an arrow pointing at it. The isEnabled box is highlighted while it is clicked, and the Patrolling output is highlighted while it is pressed. SetBodyColliderEnabled is placed the same way and chained after it.
  7. Stunned

    Stunned. Place another SetPatrolEnabled node, leave isEnabled unticked, and wire the second case output into it.

    Clip: another SetPatrolEnabled node is placed with isEnabled unticked and the Stunned output is wired into it. While the pointer rests on the menu row, that row is highlighted with an arrow pointing at it. The isEnabled box is highlighted while it is ticked and unticked, and the Stunned output is highlighted while it is pressed.
  8. Defeated

    Defeated. Place SetPatrolEnabled and SetBodyColliderEnabled nodes with isEnabled unticked, then ApplyDefeatedVisual, and chain all three from the third case output with white wires.

    Clip: SetPatrolEnabled and SetBodyColliderEnabled are placed with isEnabled unticked, then ApplyDefeatedVisual. While the pointer rests on each menu row, that row is highlighted with an arrow pointing at it. While the Defeated output is pressed, it is highlighted with an arrow pointing at it. The Defeated output and two white wires chain all three.
  9. Compile

    Press Compile in the toolbar.

    The reloaded ShowState canvas after Compile: the SWITCH node with Patrolling, Stunned and Defeated outputs, each running its own row of teal call nodes, with the CurrentState pill feeding selection.
    The whole Graph view of EnemyStateMachine with the finished ShowState canvas, the toolbar Compile button highlighted and an arrow pointing at it. Compile is amber because the canvas holds changes that are not in the C# file yet.

Each new case ends on its own. When a case’s last node runs, the switch is done, and the next case’s nodes never run after it.

If you also work in C#, this is the sample’s ApplyState, drawn as the diagram beside the code. Your ShowState writes the same three cases.

A new case starts with its own break, so a case placed on the canvas never falls through into the next one. A case whose matches box is still empty is refused by name at Compile until you say what it matches.

When it goes wrong

SymptomCheckFix
Compile refuses with a case that has no name.Open Edit switch cases and look for an empty matches box.Type the state into it, or press Remove on that case.
A new state has no visible behavior.Compare the state names with the switch node's outputs.Press add pin, name the new case in Edit switch cases, and wire its calls.
Defeated enemies still collide.Follow the Defeated output to its SetBodyColliderEnabled node.Untick that node's isEnabled box, then Compile.
Full-size image