# How to break a loop with a subscribe event?

**URL:** <https://forums.unrealengine.com/t/how-to-break-a-loop-with-a-subscribe-event/1227276>\
**Category:** Verse\
**Tags:** unreal-editor-for-fortnite, fortnite-creative, question, fortnite\
**Created:** [July 12, 2023, 5:55pm UTC](https://forums.unrealengine.com/t/how-to-break-a-loop-with-a-subscribe-event/1227276 "2023-07-12T17:55:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![TTVTROOFTH](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@TTVTROOFTH](https://forums.unrealengine.com/u/TTVTROOFTH)\
**Post date:** [July 12, 2023, 5:55pm UTC](https://forums.unrealengine.com/t/how-to-break-a-loop-with-a-subscribe-event/1227276/1 "2023-07-12T17:55:20Z")

</div>

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…

```auto
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!

---

<div class="post-metadata">

**Author:** ![Nman\_Pkr](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/nman_pkr/32/1194697_2.png) [@Nman\_Pkr](https://forums.unrealengine.com/u/Nman_Pkr)\
**Post date:** [July 14, 2023, 2:41am UTC](https://forums.unrealengine.com/t/how-to-break-a-loop-with-a-subscribe-event/1227276/2 "2023-07-14T02:41:10Z")

</div>

> [@TTVTROOFTH](#):
>
> `.E`

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.

```auto

#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

---

<div class="post-metadata">

**Author:** ![DragonRage508](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/dragonrage508/32/677485_2.png) [@DragonRage508](https://forums.unrealengine.com/u/DragonRage508)\
**Post date:** [August 4, 2023, 12:44am UTC](https://forums.unrealengine.com/t/how-to-break-a-loop-with-a-subscribe-event/1227276/3 "2023-08-04T00:44:07Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [September 3, 2023, 12:44am UTC](https://forums.unrealengine.com/t/how-to-break-a-loop-with-a-subscribe-event/1227276/4 "2023-09-03T00:44:33Z")

</div>

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