0 votes

am using this code to calculate a stamina system but i get problem

extends TextureProgress

func _ready():
    max_value = Global.max_stamina
    value = Global.stamina
    $Label.text = "ST  " + str(value) + "  /  " + str(max_value)

func _process(delta):
    max_value = Global.max_stamina
    if Input.is_action_just_pressed("attack"):
        if value >= Global.damage * delta:
            value -= Global.damage
            $Label.text = "ST  " + str(value) + "  /  " + str(max_value)

func _on_Timer_timeout():
    if value < max_value:
        value += 1
        $Label.text = "ST  " + str(value) + "  /  " + str(max_value)

the problem happens when the global variable is bigger than the textureProgress value
i can keep hitting attack and the value just keep reaching 0. i want it to stop the attack when the value is less than damage it should increases the value without the interruption of hitting attack action but what happen it keeps decreases the value even without the attack condition met.

Godot version 3.2
in Engine by (51 points)

Do you mean to have if value >= Global.damage * delta:? It seems strange to check that conditional then immediately do: value -= Global.damage.

if value >= Global.damage: makes more logical sense to me.

am checking the damage value if it is less than the TextureProgress value it will substract the damage value from the TextureProgress value. forget about if value >= Global.damage * delta: it is not the main problem even if i change it it still won't work properly as expected
what am having now when i attack it works good untill it reaches conditional bigger than TextureProgress value, then the TextureProgress value will increases but still i can't execute attack animation although the conditional met.

anyway if anyone have another ideas to work it just share it i need the stamina system to work as described earlier.

1 Answer

0 votes
Best answer

The solution is just to clamp value to max_value:

func _on_Timer_timeout():
    if value < max_value:
        value = clamp(value + 1, 0, max_value)
        $Label.text = "ST  " + str(value) + "  /  " + str(max_value)

I do think you need to reconsider your if conditional that I mentioned in the comment above - I don't think it's doing what you think it's doing. Global.damage * delta will be a small number since delta is usually very small (think > 0.2) so value -= Global.damage could end up negative.

by (3,707 points)
selected by

thanks for for your reply.
i figured out what was the problem in my previous code up there, the progress bar or texture progress was doing the job perfectly but the attack state included inside the player node is not, so i added a bool true or false in the progress code to check if the condition met or not so the player will have own condition depending on that bool to execute the attack or not. so it solved.
thanks timothybrentwood for brainstorming with me :)

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.