# How to make code be able to read through loop and go past for other code

**URL:** <https://forums.unrealengine.com/t/how-to-make-code-be-able-to-read-through-loop-and-go-past-for-other-code/1414044>\
**Category:** Verse\
**Tags:** unreal-editor-for-fortnite, fortnite-creative, question, fortnite\
**Created:** [November 23, 2023, 7:12am UTC](https://forums.unrealengine.com/t/how-to-make-code-be-able-to-read-through-loop-and-go-past-for-other-code/1414044 "2023-11-23T07:12:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pinkskyee](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@pinkskyee](https://forums.unrealengine.com/u/pinkskyee)\
**Post date:** [November 23, 2023, 7:12am UTC](https://forums.unrealengine.com/t/how-to-make-code-be-able-to-read-through-loop-and-go-past-for-other-code/1414044/1 "2023-11-23T07:12:23Z")

</div>

here is my current code mostly look at the loop function in the begin class, I want it so the script can go through the loop and look at the code below it and run that too at the same time

```auto
using { /Fortnite.com/Devices }
using { /Fortnite.com/Characters }
using { /Fortnite.com/Game }
using { /Verse.org/Simulation }
using { /Verse.org/Simulation/Tags }
using { /Verse.org/Random }
using { /UnrealEngine.com/Temporary/Diagnostics }
using { /Fortnite.com/UI }
using { /UnrealEngine.com/Temporary/UI }
using { /UnrealEngine.com/Temporary/SpatialMath }

elimination_game_device := class(creative_device):

    @editable
    EndGameDevice : end_game_device = end_game_device{}

    # Array of item granters used to grant the weapons
    @editable
    var WeaponItemGranters : []item_granter_device = array{}
    
    # This is set later, this will be equal to the the number of weapon item_granters in the island
    var NumberOfEliminationsToWin : int = 0

    # Map container to track players progress. This is how to determine which weapon to award to the player
    var AgentMap : [agent]int = map{}

    #Number of elims
    S2M<localizes>(S:string):message="{S}"
    #var UIButton : button_loud = button_loud{}

    var LastEliminatedBy: [player]player = map{}

    #Checking for new players

    OnPlayerAdded(InPlayer : player) : void =
        if (set AgentMap[InPlayer] = 1) {} 
        AllPlayers := GetPlayspace().GetPlayers()
        for (p:AllPlayers):
            var playerElims : int = 0
            var UIButton : button_loud = button_loud{}
            if (PlayerUI := GetPlayerUI[p]):
                UIButton.SetText(S2M("Kills: {playerElims}"))
                MyCanvas :canvas = canvas:
                    Slots := array:
                        canvas_slot:
                            Anchors := anchors{Minimum := vector2{X := 0.5, Y := 0.0}, Maximum := vector2{X := 0.5, Y := 0.0}}
                            Offsets := margin{Top := 100.0, Left := 50.0, Right := 50.0, Bottom := 50.0}
                            Alignment := vector2{X := 0.5, Y := 0.5}
                            SizeToContent := true
                            Widget := UIButton
                PlayerUI.AddWidget(MyCanvas)
            if (p = LastEliminatedBy):
                set playerElims += 1
                UIButton.SetText(S2M("Kills: {playerElims}"))
                
                
                
            
                

    # Check if the victory condition has been met and return the the result
    IsVictoryConditionMet(EliminationNumber:int)<transacts>:logic=
        if:
            EliminationNumber = NumberOfEliminationsToWin
        then:
            return true
        else:
            return false

    #Ending game once player reaches goal
    EndGame(Agent:agent):void=
        EndGameDevice.Activate(Agent)

    # Check if there is a winner for the game, if not then grant the next weapon
    GrantNextWeapon(Agent:agent):void=
        if(Player := player[Agent]):
            if (var CurrentItemNumber:int = AgentMap[Agent]):
                if (IsVictoryConditionMet(CurrentItemNumber) = true):
                    EndGame(Agent) # Game has been won
                else: # Game is not over yet
                    set CurrentItemNumber = CurrentItemNumber + 1
                    if (ItemGranter := WeaponItemGranters[CurrentItemNumber - 1]):
                        Print("Number of Weapon Item Granters: {CurrentItemNumber}")
                        ItemGranter.GrantItem(Agent)
                    
                    if (set AgentMap[Agent] = CurrentItemNumber) {}

    # Event that handles when a player is eliminated
    OnPlayerEliminated(Result:elimination_result):void=
        Eliminator := Result.EliminatingCharacter
        Eliminated := Result.EliminatedCharacter
        if (FortCharacter := Eliminator?, EliminatingAgent := FortCharacter.GetAgent[]):
            if(Eliminator <> Eliminated):
                GrantNextWeapon(EliminatingAgent)
                Print("Granting Weapon")

                

    OnPlayerEliminatedIteration(Result:elimination_result):void=
        Eliminator := Result.EliminatingCharacter
        Eliminated := Result.EliminatedCharacter
        if (FortCharacter := Eliminator?, EliminatingAgent := FortCharacter.GetAgent[]):
            if (Player := player[FortCharacter.GetAgent[]]):
                    Print("Trying To Set Last Eliminated")
                    if:
                        set LastEliminatedBy[Player]
                        
                    
                

    

    #Creating ui kill counter on screen
    #CreateUI(Player : player) : void = 
        #if (PlayerUI := GetPlayerUI[Player]):
            #MyCanvas :canvas = canvas:
                #Slots := array:
                    #canvas_slot:
                        #Anchors := anchors{Minimum := vector2{X := 0.5, Y := 0.0}, Maximum := vector2{X := 0.5, Y := 0.0}}
                        #Offsets := margin{Top := 100.0, Left := 50.0, Right := 50.0, Bottom := 50.0}
                        #Alignment := vector2{X := 0.5, Y := 0.5}
                        #SizeToContent := true
                        #Widget := UIButton
            #PlayerUI.AddWidget(MyCanvas)
            

    OnBegin<override>()<suspends>:void=
        Sleep(0.0)
        # Can use this variable to scale the number of eliminations needed based on how many item granters there are in the experience
        set NumberOfEliminationsToWin = WeaponItemGranters.Length
        Print("Number of Weapon Item Granters: {WeaponItemGranters.Length}")
        
        # Randomize the order in which the weapons are granted
        # set WeaponItemGranters = Shuffle(WeaponItemGranters)

        Self.GetPlayspace().PlayerAddedEvent().Subscribe(OnPlayerAdded)

        # Get all the players in the experience

        AllPlayers := GetPlayspace().GetPlayers()

        for (p:AllPlayers):
            var playerElims : int = 0
            if (fortCharacter := p.GetFortCharacter[]):
                fortCharacter.EliminatedEvent().Subscribe(OnPlayerEliminatedIteration)
            if (PlayerUI := GetPlayerUI[p]):
                var UIButton : button_loud = button_loud{}
                UIButton.SetText(S2M("Kills: {playerElims}"))
                MyCanvas :canvas = canvas:
                    Slots := array:
                        canvas_slot:
                            Anchors := anchors{Minimum := vector2{X := 0.5, Y := 0.0}, Maximum := vector2{X := 0.5, Y := 0.0}}
                            Offsets := margin{Top := 100.0, Left := 50.0, Right := 50.0, Bottom := 50.0}
                            Alignment := vector2{X := 0.5, Y := 0.5}
                            SizeToContent := true
                            Widget := UIButton
                PlayerUI.AddWidget(MyCanvas)
                loop:
                    Sleep(0.1)
                    Print("looping")
                if (p = LastEliminatedBy):
                    set playerElims += 1
                    Print("WorkingLoop In If")
                    Print("{playerElims} Please Work, text not updating")
                    UIButton.SetText(S2M("Kills: {playerElims}"))
                else:
                    Print("Broke Loop")
                

        
        for (EliminationGamePlayer : AllPlayers):
            if (FortCharacter := EliminationGamePlayer.GetFortCharacter[]):
                FortCharacter.EliminatedEvent().Subscribe(OnPlayerEliminated) # subscribe to eliminated event
            
            # Add Players to a Map to track progress
            if (set AgentMap[EliminationGamePlayer] = 1) {} 
            
            # Grant the first weapon to each player
            if (FirstItemGranter:item_granter_device = WeaponItemGranters[0]):
                FirstItemGranter.GrantItem(EliminationGamePlayer)
                 

```

---

<div class="post-metadata">

**Author:** ![ManualPotato](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/manualpotato/32/1144902_2.png) [@ManualPotato](https://forums.unrealengine.com/u/ManualPotato)\
**Post date:** [November 23, 2023, 8:54am UTC](https://forums.unrealengine.com/t/how-to-make-code-be-able-to-read-through-loop-and-go-past-for-other-code/1414044/2 "2023-11-23T08:54:16Z")

</div>

Add `break` where you want to break the loop.

---

<div class="post-metadata">

**Author:** ![pinkskyee](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@pinkskyee](https://forums.unrealengine.com/u/pinkskyee)\
**Post date:** [November 23, 2023, 10:44pm UTC](https://forums.unrealengine.com/t/how-to-make-code-be-able-to-read-through-loop-and-go-past-for-other-code/1414044/3 "2023-11-23T22:44:10Z")

</div>

That is a problem because I am trying to create a kill counter and it needs to be constantly checking, and be able to go past the loop and check the other code

---

<div class="post-metadata">

**Author:** ![bananna\_manuk](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@bananna\_manuk](https://forums.unrealengine.com/u/bananna_manuk)\
**Post date:** [November 26, 2023, 8:57pm UTC](https://forums.unrealengine.com/t/how-to-make-code-be-able-to-read-through-loop-and-go-past-for-other-code/1414044/4 "2023-11-26T20:57:58Z")

</div>

create a void to contain your loop and add when you declare it

```auto
MyLoop()<suspends>:void=
    loop:
           loop stuff here

   Sleep(0.5)

```

to start the loop you use spawn

```auto
    OnBegin<override>()<suspends>:void=
        Sleep(0.0)
        Spawn:
                Myloop()

```

my loop will run along side the main program routine

someone correct me if im wrong pls

---

<div class="post-metadata">

**Author:** ![ManualPotato](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/manualpotato/32/1144902_2.png) [@ManualPotato](https://forums.unrealengine.com/u/ManualPotato)\
**Post date:** [November 27, 2023, 11:48am UTC](https://forums.unrealengine.com/t/how-to-make-code-be-able-to-read-through-loop-and-go-past-for-other-code/1414044/5 "2023-11-27T11:48:28Z")

</div>

[bananna\_manuk](https://dev.epicgames.com/community/api/user_profiles/tab-redirect/about?discourse_username=bananna_manuk) is correct, I misunderstood the original question. The only thing to add is it’s preferable to use structured concurrency (in this case [Branch](https://dev.epicgames.com/documentation/en-us/uefn/branch-in-verse)) rather than `spawn` where possible.
