Problems with cooldown

Godot Version

Godot 4.6.2

Question

I want to make a visual component for the spell’s cooldown. The cooldown is 1 second. The problem is that when the spell is ready to use, the visual shows that only 10-20% is ready.

func cd_vis(spell_n : int, cd_time : float):
	var cd_time_plus = cd_time / 100.0
	print(cd_time_plus)
	if spell_n == 1:
		for i in range(101):
			await get_tree().create_timer(cd_time_plus).timeout
			$HotBar/HotBarBG/Spell1/CD.text = str(i) + "%"
	if spell_n == 2:
		for i in range(101):
			await get_tree().create_timer(cd_time_plus).timeout
			$HotBar/HotBarBG/Spell2/CD.text = str(i) + "%"
	if spell_n == 3:
		for i in range(101):
			await get_tree().create_timer(cd_time_plus).timeout
			$HotBar/HotBarBG/Spell3/CD.text = str(i) + "%"
	if spell_n == 4:
		for i in range(101):
			await get_tree().create_timer(cd_time_plus).timeout
			$HotBar/HotBarBG/Spell4/CD.text = str(i) + "%"
if input_button == "Z" and !cd_spell1_flag:
	cd_spell1_flag = true
	if core.Spell1.mellee:
		mellee_attack(core.Spell1)
		input_button = null
	if !core.Spell1.mellee:
		long_attack(core.Spell1)
		input_button = null
	core.inv_way.cd_vis(1, cd_spell1)
	await get_tree().create_timer(cd_spell1).timeout
	cd_spell1_flag = false

User Timer nodes, await is going to get you intro trouble and likely already has. You could attach a process to the timer to set a label with the remaining time, or a percent of the time remaining.

extends Timer

@export var target_label: Label

func _process(_delta: float) -> void:
    var percent_remaining := 100 - roundi(time_left / wait_time * 100)
    target_label.text = str(percent_remaining) + "%"
1 Like

I know it’s not nice to write a thank-you letter a month later, but thank you for the advice. Although I didn’t use it at the time, it gave me an idea that is now used in my code (I can’t tell you about the idea because it was a month ago and I’ve forgotten).

1 Like