How to switch scene after an animation finishes?
Asked Answered
R

3

0

I am trying to switch the scene after the screen fades to black.

`extends TextureButton

onready var anim = $Fade/AnimationPlayer

func _process(delta):
if Input.is_action_just_pressed("mouseleft"):
anim.play("fadeblack")

anim.connect("animation_finished", self ,"_on_animation_finish")

func _on_animation_finish():
get_tree().change_scene("res://Node2D.tscn")`

Reaction answered 30/11, 2023 at 5:30 Comment(0)
E
0
  1. Make sure your animation doesn't loop.
  2. animation_finished passes a StringName parameter which needs to be handled. Change
    func _on_animation_finish(): to func _on_animation_finish(_anim_name):
    The _ in front of _anim_name tells Godot that you intend to throw away the variable, so it's important, but the rest of the variable can be named whatever you want.

If that still doesn't work, then instead of anim.connect try anim.animation_finished.connect.

Btw, you can also await signals, which sometimes is much cleaner.

anim.play("fadeblack")
await anim.animation_finished
get_tree().change_scene("res://Node2D.tscn")`

Just don't do await inside of _process

Excitor answered 30/11, 2023 at 7:20 Comment(0)
P
0

In this case I would probably just go with awaiting for the animation to finish.

Pardoes answered 30/11, 2023 at 12:50 Comment(0)
R
0

Excitor it worked thanks a bunch

Reaction answered 30/11, 2023 at 16:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.