how can i use a wait function in my script

Godot Version

Godot v4.7.2.stable

Question

Hello, i'm a beginner and i need some way to make a cooldown for stamina regen, i need it to regenerate a certain amount at a time but i can't figure out how to do it in an efficient way whenever the stamina goes below a certain amount, i tried using a Timer but i'm really struggling with it, any help would be appreciated.

Could you show us what your current setup is? The code, the hierarchy, etc :slight_smile:

for cooldowns i usually use delta timers and main _process function, here i update every object and node in my game that exists, for instance let’s say you have player object

func _process(delta: float) -> void:
  player.update(delta);

and inside player:

var stamina: float = 100.0:
  set(new_value):
    stamina = clamp(new_value, 0.0, 100.0);

var sprinting: bool = false;

func update(delta: float) -> void:
  if (sprinting):
    stamina -= delta * 10.0;
  else:
    stamina += delta * 5.0;

this code works like this:

  • we create stamina with setter so we could clamp (limit value between two other numbers) any new value we give to it
  • in update function we check if we’re sprinting (i assume you want to remove stamina while sprinting) and then subtract stamina multiplied by delta to avoid frame rate bugs
  • if we aren’t sprinting we add stamina multiplied by delta again

var stamina: float = 100:
set(stamina_value):
stamina = clamp(stamina_value, 0, 100);

func _process(delta: float) → void:
if stamina < 100:
stamina += delta * 10;

i got it working so thanks alot, only issue now is that now the stamina becomes a float when it regens, i need it to be an int value cause this is for a dash system that consumes and then regenerates stamina

thinking about it now i could also try having 3 charges of sorts that get depleted and recharge over time rather than a dynamic stamina system

Why do you need it to be an int? Almost every single game I know, Stamina bars are a float since it’s easier to deal with and have a smoother curve you can represent on a progress bar. Having it be an int would require a lot of back and forth conversions.

Could you tell us why you need it to be an int exactly?

at the moment i’m trying to make a dash with a cool down and limited dashes so i was thinking to have an int based system cause its only big bursts of stamina that needs to be removed and regenerated, i also realize that in that case i may as well just use a normal cooldown rather than a stamina based system

But that doesn’t really answer the question of why it needs to be an integer vs. a float.

DANG i’m so sorry for wasting all of your guys time, i just realized thats exactly how dashing works in most games anyway, i can’t believe it took me so long to realize, thanks for taking your time to try and help though

thanks so much for helping me out, sorry for wasting your time

thank you so much to everyone that helped