using { /Fortnite.com/Devices } using { /Verse.org/Simulation } using { /Verse.org/Verse } # The named states. An enum keeps the machine readable — no magic numbers. npc_state := enum: Idle Chase Attack # A guard NPC driven by a loop around a race of awaited events. guard_brain := class(creative_device): # Stand-in "sensors": pressing this button means "a player was spotted". @editable SpottedButton : button_device = button_device{} # Events the brain awaits. Real perception code would Signal these. PlayerSpotted : event() = event(){} PlayerLost : event() = event(){} # The current state of the machine. var State : npc_state = npc_state.Idle OnBegin() : void = # Wire the stand-in sensor: a press raises PlayerSpotted. SpottedButton.InteractedWithEvent.Subscribe(OnSpotted) # Run the brain forever. RunBrain() # Bridge a device event (Subscribe) into our async event (Await). OnSpotted(Agent : agent) : void = PlayerSpotted.Signal() # The heartbeat: forever, run the current state, which returns the next one. RunBrain() : void = loop: case (State): npc_state.Idle => set State = Idle() npc_state.Chase => set State = Chase() npc_state.Attack => set State = Attack() # IDLE: stand around until a player is spotted. Idle() : npc_state = Print("[Guard] Idle — watching.") PlayerSpotted.Await() # wait until the sensor fires Print("[Guard] Player spotted!") return npc_state.Chase # CHASE: run them down, but give up if we lose them or it drags on. Chase() : npc_state = Print("[Guard] Chasing.") Winner := race: block: Sleep(3.0) # caught up after 3s of chasing 1 block: PlayerLost.Await() # lost line of sight 2 block: Sleep(10.0) # chase timed out 3 case (Winner): 1 => return npc_state.Attack 2 => return npc_state.Idle _ => return npc_state.Idle # ATTACK: strike on a cooldown until the player gets away. Attack() : npc_state = Print("[Guard] Attacking!") Winner := race: block: Sleep(5.0) # player escaped after 5s 1 block: loop: Sleep(1.0) Print("[Guard] Strike!") # never completes → loses 2 case (Winner): 1 => return npc_state.Chase _ => return npc_state.Chase