Issue with tweeners in _physics_process

Godot Version

v4.5.beta1.official [46c495ca2]

Question

I am trying to create a tween in the _physics_process, but too many issues.

var tween: Tween

func _ready() -> void:
	tween = get_tree().create_tween()

func _physics_process(delta: float) -> void:
	#handle interaction
	raycast.force_raycast_update() # force an raycast update to fix issues with it holding on
	if raycast.is_colliding() and raycast.get_collider().has_method("interact"):
		rich_text_label.modulate.a = 1
		raycast.get_collider().interact(self)
	elif not tween.is_running():
		tween.tween_property(rich_text_label,"modulate:a",0,1.0)

trying to get the rich text label to appear instantly when over an object, and fade out after the object is out of range. Text appears, but does not vanish

E 0:00:01:431 step: <Tween#-9223371999676463684>: started with no Tweeners.
<C++ Error> Method/function failed. Returning: false
<C++ Source> scene/animation/tween.cpp:342 @ step()

Tweens are supposed to be created and apply tweeners before the next frame starts, only hold onto a tween reference if you intend to kill it during it’s run.

i am intending to kill it, because a new object might appear before the tween ends

Good, still only use create_tween just before tween_property. There may be other logical issues, it seems like checking the raycast and setting modulate.a every physics tick would prevent the tween from running.

elif not tween.is_running():
	tween = create_tween()
	tween.tween_property(rich_text_label,"modulate:a",0,1.0)

that code never actually runs though. tween.is_running() returns true

solution

func _physics_process(delta: float) -> void:
	#handle interaction
	#print(tween.is_running())
	raycast.force_raycast_update() # force an raycast update to fix issues with it holding on
	if raycast.is_colliding() and raycast.get_collider().has_method("interact"):
		if tween.is_running():
			tween.stop()
			rich_text_label.modulate.a = 1
		if Input.is_action_just_pressed(input_interact):
			raycast.get_collider().interact(self)
			
	elif not tween.is_running():
		tween = create_tween()
		tween.tween_property(rich_text_label,"modulate:a",0,1.0)

i do not understand why this fixes it, but it does