Godot Version
4.7
Question
Hello pretty people,
i’m trying to make a system in which abilities costs hearts.
imagine using an ability and it costs 2 out of your 5 hearts. those 2 hearts used would go on cooldown and for that cd time you would have only 3 hearts meaning that taking 3 dmg would kill you. after the cd time has expired the hearts are active again and you are back to the full 5.
ive managed to make one heart work making it a progress bar that shows the cooldown but im having issues making it work for other hearts too. mainly because i don’t know what would be the optional way to handle this situation. should i make a timer connected to each heart? or should i have an heart that i duplicate for each hp point? should it be a scene on its own?
ive watched some tutorials on hp bars and my process has stopped when ive had to start handling more hearts in cooldown.
i would put some script here but for now i really just need to understand what would be the best logic and from there go with scripting.
thanks!!
Without requirements there isnt sufficent info here to answer such an open ended design question.
I will refer you to this method of “How to Solve It” by George Polya.
Think about your data and constraints, (health, cool down UI). Seperate logic from state, make a plan to execute, think about your solution, how it works and/or how it can be improved.
Before getting to visuals, getting the logic working would be the best. First question is, will each heart have its own cooldown? Or after a set cooldown, will all of them get restored at the same time?
If each heart has its own cooldown, it could easily be just an integer in code like this and then you’d be free to handle visuals however you see fit:
var available_hearts: int = 5
var cooldown: float = 5 # seconds.
func use_heart(amount: int) -> void:
available_hearts -= amount
_heart_restoration(amount)
print("Hearts used: %s" % amount)
func _heart_restoration(amount: int) -> void:
await get_tree().create_timer(cooldown).timeout
available_hearts += amount
print("Hearts restored: %s" % amount)