Change Scene in Quit Request

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By weinzierl

I wanted to play a very short animation when the player quits the game. I put the animation in a separate scene called Outro.tscn and tested it to make sure it works. Now I tried to play it on an Quit Request like this:

func _notification(what): 	
    if what == MainLoop.NOTIFICATION_WM_QUIT_REQUEST: 			
        print("Quitting requested...") 
        print("Playing Outro and Quit!") 			 
        get_tree().change_scene("res://Outro.tscn")

The functions gets called properly and both print lines are shown but the the Outro Scene is not played and there is no error shown either.

I would like to understand why that doesn’t work and I’m looking for a way to make the animation play whenever the game is quit. Glad if anyone could help!

:bust_in_silhouette: Reply From: jgodfrey

I assume that your game is exiting when the NOTIFICATION_WM_QUIT_REQUEST is processed, right? If so, that’s why the animation isn’t playing.

You can effectively “ignore” the quit request using this code:

get_tree().set_auto_accept_quit(false)

With that done, the game won’t exit and your animation should play. However, you’ll then need to manually exit the game when the animation finishes playing.

To do that, you could wire up the animation_finished signal in the outro scene. In that callback, you can exit the game via:

get_tree().quit()

Worked like a charm. Thank you!

weinzierl | 2022-08-18 08:10