TextureProgress problem

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Sha6er

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.

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.

timothybrentwood | 2021-06-24 15:02

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.

Sha6er | 2021-06-27 15:34

:bust_in_silhouette: Reply From: timothybrentwood

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.

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 :slight_smile:

Sha6er | 2021-07-02 07:26