How can I cancel a function?

I have a looping function that heals the player when they enter a zone but it’s healing when they leave the zone and it stacks on itself if they reenter which I don’t want, how can I make the loop stop once they trigger the AgentExitsEvent? I think I use the race expression for this but I’m not really sure, thanks!

HealPlayer(Agent : agent)<suspends> : void =
        race:
            loop:
                Sleep(1.0)
                if (Player := player[Agent]):
                    if (FortniteCharacter : fort_character = Player.GetFortCharacter[]):
                        FortniteCharacter.Heal(5.0)

            HealZone.AgentExitsEvent

HandleAgentEnter(Agent : agent) : void=
        spawn:
            HealPlayer(Agent)

OnBegin<override>()<suspends>:void=
        HealZone.AgentEntersEvent.Subscribe(HandleAgentEnter)

The race keyword is used when starting multiple asynchronous functions at the same time. I think an easier solution might be to create a variable that stores data on the player being inside the area. Basically, create a boolean (logic) variable that says whether or not the player is inside, and change that variable anytime the player either enters or exits. Then you can do the healing as long as the variable is true (which will change anytime the event is fired off) For example:

IsPlayerInsideHealZone : logic = false
HealPlayer(Agent : agent)<suspends> : void =
        loop:
            if (IsPlayerInsideHealZone = false):
                 break
            Sleep(1.0)
            if (Player := player[Agent]):
                if (FortniteCharacter : fort_character = Player.GetFortCharacter[]):
                    FortniteCharacter.Heal(5.0)

            


HandleAgentEnter( Agent : agent ) : void =
    set IsPlayerInsideHealZone = true
    spawn:
        HealPlayer

# Then create the function for agent exiting field
HandleAgentExit( Agent : agent ) : void =
    set IsPlayerInsideZone = false

OnBegin<override>()<suspends>:void=
        HealZone.AgentEntersEvent.Subscribe(HandleAgentEnter)
        HealZone.AgentExitsEvent.Subscribe(HandleAgentExit)



1 Like

that is so smart!! I completely forgot about the logic type thank you so much!! :smiley:

This ain’t gonna work if you’re planning on having multiple players on the map though, you’ll have to use a map{} ( [player]logic ) instead

1 Like