How to break a loop with a subscribe event?

Hi, I have a bit of code that runs a loop with sleep timers after a player enters a zone, and I want to break the loop each time the player is eliminated. How can I do that? My EliminatedEvent.Subscribe is throwing an error…

using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /Fortnite.com/Characters }

powers := class(creative_device):

    @editable
    mutator1: mutator_zone_device := mutator_zone_device{}
    @editable
    pow: visual_effect_powerup_device := visual_effect_powerup_device{}

    OnBegin<override>()<suspends>:void=
        mutator1.AgentEntersEvent.Subscribe(OnZoneEntered) 

    OnZoneEntered(InPlayer:agent) : void =
        spawn{_OnZoneEntered(InPlayer)}
    
    _OnZoneEntered(InPlayer:agent)<suspends> : void =
        loop:

            InPlayer.GetFortCharacter[].EliminatedEvent.Subscribe(break)

            pow.Pickup(InPlayer)
            Sleep(5.0)

Thanks!

You should use a function to handle the eliminated event and switch the state of a logic variable there. Then in your loop you can evaluate the variable and break then, otherwise loop. Maybe throw a frame skip Sleep(0.0) in the loop too.


#Handle Players
loop:
    loop:
        #wait for player elimination
        if(BreakState = true):
            break

        Sleep(0.0)
    #Do game logic, Power up, etc.
    Sleep(5.0)
    Set BreakState = false

On entered zone, change a maped variable to the agent to true: ex: set PlayerInZone[InPlayer] = true

On exiting zone, set the mapped variable to false if desired, otherwise set state elsewhere.

On elimination, check if InZone := PlayerInZone[InPlayer] is true, then set BreakState = true

1 Like

You can use a logic variable to break the loop, or you could use an event type. Here is an example:

BreakLoopEvent : event() = event(){}

LoopingFunction()<suspends>:void=
    race:
        block:
            loop:
                # Run loop code here
                Sleep(0.0)
        block:
            BreakLoopEvent.Await()

The BreakLoopEvent.Await() will suspend that line of code until you call BreakLoopEvent.Signal() from anywhere in code. When the BreakLoopEvent.Signal() is called, that block will finish which means it wins the race against the looping block, terminating the running of the loop without even having to use break in the loop! If you are unfamiliar with the type race I recommend looking at the documentation as it is a good expression to know.

4 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.