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…
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
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.