Godot Version
v4.3.stable.mono.official [77dcf97d8]
Question
I’m trying to create a health display for a turn based game. Basically when a character’s health changes I want to spawn a little number that floats upwards from the character icon. I did this easily and it works, but the problem is when a character takes damage in quick succession, the numbes spawn on top of each other, which I don’t want. So I wanted to implement a delay between the spawns.
This is how I’m trying to do it:
I have an array in which I hold all of the health changes. When the health changes, I call an asynchronous function that scrolls through the array and spawns a display, then waits for 300 ms, and goes on to the next element. The function also has a bool which prevents it from being called while it’s already active, so I don’t have multiple instances of it. The idea is that, while the function is waiting, new elements would be added to the array, so once it has finished waiting, a new element would be inside to spawn a display for, and if there aren’t any, then I just clear the array and unlock the function.
Problem: it does not work. At all.
It only ever spaws the first display.
Here is the script:
Godot.Collections.Array<float> hpchange = new Godot.Collections.Array<float>();
bool displayingHealthChange = false;
void OnHealthChanged(float oldVal, float newVal)
{
healthBar.SetValue(sheet.statBlock.CurrHealth.ModValue);
hpchange.Add(newVal - oldVal);
GD.Print("Health Changed");
if (!displayingHealthChange) DisplayHealthChange();
}
async void DisplayHealthChange()
{
displayingHealthChange = true;
foreach(float change in hpchange)
{
GD.Print(hpchange.Count);
var display = hpChangedDisplay.Instantiate<HealthChangeDisplay>();
AddChild(display);
display.Position = hpDisplayPos.Position;
display.Start(change, ai);
await Task.Delay(500);
}
hpchange.Clear();
displayingHealthChange = false;
}