Tween won't allow me to reset the property it was animating
Asked Answered
S

7

0

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.

Soph answered 8/9, 2023 at 18:44 Comment(0)
C
0

Soph Please format your code properly.

Corticosterone answered 8/9, 2023 at 18:55 Comment(0)
H
0

Soph 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?

Hoxsie answered 8/9, 2023 at 20:3 Comment(0)
S
0

Hoxsie 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.

Soph answered 8/9, 2023 at 20:8 Comment(0)
H
0

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.

Hoxsie answered 8/9, 2023 at 20:14 Comment(0)
C
0

Soph
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?

Corticosterone answered 8/9, 2023 at 20:18 Comment(0)
S
0

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: Corticosterone How can I format my code? I tried placing ~ before every line but the server didn't process it. Thanks.

Soph answered 8/9, 2023 at 21:53 Comment(0)
C
0

Soph 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

~~~

Corticosterone answered 8/9, 2023 at 22:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.