change_scene_to_file()

Godot Version

extends CanvasLayer

@onready var fade_rect: ColorRect = $FadeRect
var transition_time := 1.0

func _ready():
var viewport_size = get_viewport().size
# Imposta la dimensione del ColorRect in base alla viewport
fade_rect.size = viewport_size
fade_rect.visible = false

func change_scene_with_transition(scene_path: String) → void:
# Assicurati che il ColorRect sia visibile e inizialmente trasparente
transition_time = 1.0
fade_rect.modulate.a = 0
fade_rect.visible = true

# Avvia il Tween per eseguire il fade-out (da trasparente a nero opaco)
var tween = get_tree().create_tween()
tween.tween_property(fade_rect, "modulate:a", 1, transition_time).set_trans(Tween.TRANS_LINEAR).set_ease(Tween.EASE_IN)
await tween.finished

# Cambia la scena
get_tree().call_deferred("change_scene_to_file", scene_path)
tween = get_tree().create_tween()
tween.tween_property(fade_rect, "modulate:a", 0, transition_time).set_trans(Tween.TRANS_LINEAR).set_ease(Tween.EASE_IN)
await tween.finished
fade_rect.visible = false

Question

Hi guys i have this global script to use whenehever the player chenges area on the game and it mostly works, the problem is when i change the scene back immediately after i entered in the new scene.
It seems like the tweens doesnt work in that case and the fade in and out are shown just for a moment leting me see everything behind the “scenes”

it looks like the issue might be caused by two tweens happening at the same time. you could either prevent the player from moving back until the transition is completely over, or you could kill the tween from the previous transition so it doesn’t interfere with the new transition

You are totaly right, in fact i solved by waiting a signal in the beging of the function emitted in the end when the second tween finishes

1 Like