Verse Library verse

09 Fragment

Runs multiple event awaits concurrently and handles whichever condition triggers first.

verse-library/verse-concurrency-async-race-state-machines/09-fragment.verse

# Three things could end this phase. Whichever happens first wins;
# the others are cancelled. The winning arm's number tells us what happened.
Winner := race:
    block:
        DamageTaken.Await()      # the NPC got hit
        1
    block:
        PlayerSpotted.Await()    # the NPC saw a player
        2
    block:
        Sleep(8.0)               # nothing happened for 8 seconds
        3

case (Winner):
    1 => Print("Hit! React.")
    2 => Print("Spotted a player — give chase.")
    3 => Print("All quiet — go back to idle.")
    _ => Print("unreachable")

Comments

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