- Edited
So the scenario is this: the player takes a hit and his health drops 10 units (out of 100). I then call a function with multiple awaits in order to regenerate the health every 3 seconds by 1 unit.
The problem is if the player takes another hit while regenerating, the first called functtion (with the initial awaits) keeps adding the original health points, and now there's a second one doing that on top.
I would like to stop the first function with the await timers from running at all. Something as simple as having a bool flag that turns on when the function is first called, and then each time it is called it checks the flag. The problem is I don't know what to do if I find the flag already on. I don't know how to discard the already running (or queued) await calls.
A few notes:
- i used one player as an example, but i do in fact have several thousands of objects that trigger their own await, so just using a get_time and checking in the _process loop is not an option
- i want just a single function regenerating at a time. I don't want to stagger them for various reasons (the simplest being i may not use just a constant single health point, but may regenerate 5 then 4 then 3 points and so on)
- I am open to other ideas on how to call a function that runs in parallel but can be cancelled once it is called again
Example code:
func _ready():
player.health_points = 100
func _process():
if (player.hit):
player.health_points -= 10
_start_regen(player)
func _start_regen():
await get_tree().create_timer(3).timeout
player.health_points += 1
await get_tree().create_timer(3).timeout
player.health_points += 1
await get_tree().create_timer(3).timeout
player.health_points += 1
Thanks