You could use coroutines for screen fading here to make the code cleaner

i.e. by calling await in a function, you make it a coroutine that can itself be awaited on

in base_screen.gd

const FADE_TIME = 0.5

var tween: Tween

func _ready():
	visible = false

func fade(alpha: float):
	if tween:
		tween.kill()
	get_tree().call_group("buttons", "set_disabled", true)
	tween = get_tree().create_tween()
	tween.tween_property(self, "modulate:a", alpha, FADE_TIME)
	tween.play()
	await tween.finished
	get_tree().call_group("buttons", "set_disabled", false)

func appear():
	if not visible:
		modulate.a = 0.0
		visible = true
		await fade(1.0)
	
func disappear():
	if visible:
		modulate.a = 1.0
		await fade(0.0)
		visible = false

then in screens.gd

func change_screen(new_screen: BaseScreen):
	if current_screen:
		var old_screen = current_screen
		current_screen = null
		await old_screen.disappear()
	if new_screen:
		await new_screen.appear()
		current_screen = new_screen

Then, in _on_button_pressed, you use await change_screen(new_screen) if you want to do something after the screen has changed; that way, you don’t need separate timers.

I also set up a change screen to ensure that the screen is null during a transition for safety.

5 Likes

What is cleaner in this code?

Thank you this is a better solution!

It is cleaner because this code handles the screen properties in the base_screen.gd , so the change_screen() function won’t contain current_screen.visible = false

Privacy & Terms