Hi,

I'm using a tween to animate a progress bar's value, so it has an smooth and continuous decreasing movement. The point is that I want to refill the bar again (by setting its value to its maximum value, which is variable depending on the level's difficulty) once the player has finished the current level, and without reloading the current scene, but the line that should be doing that is ignored, without throwing any error or warning. I'm using this code:

func setup_timebar():
if smoother:
smoother.kill()
$TBContainer/TimeBar.value = $TBContainer/TimeBar.max_value
print("Reset to " + str($TBContainer/TimeBar.max_value))
smoother = get_tree().create_tween()
smoother.tween_property($TBContainer/TimeBar, "value", 0, $TBContainer/TimeBar.max_value)
smoother.tween_callback(_time_over)

That function is called whenever player completes the current level and passes to the next one. As you can see I'm using the kill() function and even checking that the reset is being done, it outputs "Reset to 15", but the bar keeps decreasing from where it stayed. I tried using stop() function as well (afaik, according to the documentation stop() function should reset the value it was animating by its own), alone or alongside with kill() but that line keeps being ignored. How can I solve that?

Thanks in advanced.

    YottaMxt Hmm, that's interesting. I am using stop() in my project for health and stamina bar and it works fine. Do you have some sample project where it doesn't work? Also are you using Godot 4.1.1?

      GlyphTheWolf I can either replace the kill() function with stop() or insert stop() before kill() (inside the conditional block), but it won't be working. Yes I'm using Godot 4.1.1.

      • xyz replied to this.

        In my project I am using code like:

        func on_health_change(_balance : float, current : float):
        	if(tmpTween != null && tmpTween.is_running()):
        		tmpTween.stop()
        	$HealthBarRealValue.value = current
        	if($HealthBarTmp.value > $HealthBarRealValue.value):
        		#Need tween
        		tmpTween = create_tween()
        		tmpTween.tween_property($HealthBarTmp, "value", $HealthBarRealValue.value, abs($HealthBarTmp.value - $HealthBarRealValue.value) * 0.1)
        	else:
        		#Just update value
        		$HealthBarTmp.value = $HealthBarRealValue.value

        And that's working for me as I expect. If stop() or kill() doesn't work for you then I guess value of your time bar have to be updated somehow before tween start, possibly from another place maybe? Tweens are processed after all nodes.

        YottaMxt
        What happens if you set from value explicitly by calling PropertyTweener::from() on the tweener object returned by tween_property()?
        What happens if you wait one frame immediately after killing the tween?

        I know I said it already but... format your code properly using ~~~ tags. How are we supposed to know where your if block ends?

        Thank you guys for your help, I finally solved it by replacing the tween_property with this and setting the setup_timebar() itself as the tween callback:

        smoother.tween_property($TBContainer/TimeBar, "value", $TBContainer/TimeBar.value - 1, 1)
        smoother.tween_callback(setup_timebar)

        And removing the conditional block and all the other garbage stuff. I only needed to call smoother.kill() once the player had completed the current level.

        Pd: xyz How can I format my code? I tried placing ~ before every line but the server didn't process it. Thanks.

        • xyz replied to this.

          YottaMxt How can I format my code?

          Select the code and press "Insert code" button.

          Or manually enclose the code block with ~~~ like this:

          ~~~

          code
          code
          code

          ~~~