Verse Library verse

13 Device

Drives NPC behavior through async event listening and state transitions for reliable logic.

verse-library/verse-concurrency-async-race-state-machines/13-device.verse

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<override>()<suspends> : void =
        # Wire the stand-in sensor: a press raises PlayerSpotted.
        SpottedButton.InteractedWithEvent.Subscribe(OnSpotted)
        # Run the brain forever.
        RunBrain()

Sign in free to read the full source 🌴

This Verse Library file is members-only. Create a free account to view the complete source and download the .verse file.

Sign in with Discord

Comments

    Sign in to vote, comment, or suggest an edit. Sign in