I'm having health bar slowly decreasing over delta time.

TextureProgressBar

var current_bar_value = 100
const bar_speed = 5

func _process(delta):
	current_bar_value -= bar_speed * delta
	current_bar_value = clamp(current_bar_value,0, 100)
	value = current_bar_value

func _on_button_pressed():
	var add = value+20
	var tween = get_tree().create_tween()
	tween.tween_property(self, "value",add, 0.15).set_ease(Tween.EASE_IN_OUT)

After the button is pressed I want it to tween to new value +20 and continue on decreasing. The problem is it tweens there but quickly goes back to original decreasing value.

How to fix the code? Thank you.

  • Try to set progress bar's step value to something small. It's 1 by default in which case adding values less than 1 will not register at all. Either that or maintain an additional counter variable and copy it to value, as you did in the initial example.

    extends TextureProgressBar
    
    var speed = 8
    var tween = null
    
    func _ready():
    	self.step = .001
    
    func _process(delta):
    	if not tween:
    		value -= delta * speed
    
    func _on_button_pressed():
    	tween = get_tree().create_tween()
    	tween.tween_property(self, "value", value+20, 0.15)
    	tween.connect("finished", tween_done)
    	
    func tween_done():
    	tween = null

You have two variables storing the same state: value and current_bar_value. _process updates both. But the tween only updates value. So when the tween is done, _process will override value with the old current_bar_value.

IMO the easiest solution is just to nuke current_bar_value and use value instead.

    Zini Thanks I changed to use only value and It kinda works. But for some reason if I set bar_speed below 30 it won't decrease at all? and anything above 30 will be too fast.

    You need to pause decreasing in _process() during the time the tween is active. Otherwise increase and decrease will be both happening at the same time giving you unwanted behavior.

    Just this already causes problem

    func _process(delta):
    	value -= 30 * delta

    If I were to change without using delta the same problem happen with value below 0.5

    func _process(delta):
    	value -= 0.5

    I didn't change any TextureProgressBar settings except set its initial Value to 100

    Try to set progress bar's step value to something small. It's 1 by default in which case adding values less than 1 will not register at all. Either that or maintain an additional counter variable and copy it to value, as you did in the initial example.

    extends TextureProgressBar
    
    var speed = 8
    var tween = null
    
    func _ready():
    	self.step = .001
    
    func _process(delta):
    	if not tween:
    		value -= delta * speed
    
    func _on_button_pressed():
    	tween = get_tree().create_tween()
    	tween.tween_property(self, "value", value+20, 0.15)
    	tween.connect("finished", tween_done)
    	
    func tween_done():
    	tween = null

      xyz Thank you it works. A quick question on Tween:

      How can I do the same if I use Tween instead of -= value?

      Something like this:

      var tween = get_tree().create_tween()
      
      func _ready():
      	tween.tween_property(self, "value",0, 20)
      
      func _on_button_pressed():
      	tween.tween_property(self, "value",value+20, 0.15)

      First time using Tween and I don't know how to manipulate it.

      • xyz replied to this.

        Gowydot No, it wont work like that. If you call tween_property() multiple times in a row the engine will queue tweens one after another. You can force it to do it in parallel by calling set_parallel() but I'm not sure Godot 4 will actually run it. It'll probably just ignore any new calls if the property is in the middle of a tween.