Godot 3.1 : How to exit a full-screen game upon pressing the escape key?

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

Anyone know how to do this? I found some an old example for Godot 2.x but it throws an error in Godot 3.1 after pressing the esc. key, so I imagine it’s not compatible.

:bust_in_silhouette: Reply From: KijeviGombooc

Look at this question-answer: https://forum.godotengine.org/28425/how-to-toggle-fullscreen-from-code
It should answer your question.

Thanks for the reply,

GamesFromScratch has the answer to this for Godot 2.x (issue for 3.x)

#Input handler, listen for ESC to exit app
func _input(event):
      if(event.scancode == KEY_ESCAPE):

I can press esc to exit game just fine.

Unfortunate side effect of this code is, if you click the screen with the mouse, the game crashes.

Any ideas on that?

GodotUser | 2019-07-23 00:58

I’m sorry I misunderstood your question, I thought you want to exit fulscreen into windowed mode. I am currently at work but I’ll try not to forget looking into that when I get home

KijeviGombooc | 2019-07-23 06:24

And I don’t think your player would enjoy quitting when pressing escape, or if it’s only in a specific game state, then you should be able to use if Input.is_action_just_pressed("quit"): get_tree().quit()
Where “quit” is set to escape in input options
This code can then be in _process(delta) function too, (or any thats called from it) so you can put it in the proper part of your code

KijeviGombooc | 2019-07-23 06:33

I came to same conclusion when testing this. I was applying a tutorial to my game and realized how inadvertent key pressed OR mouse clicks would not be a good thing to end your game. So I added a camera to the game and so putting a quit button is not practical unless it can stay with the camera. Alt-F4 already works and is what people would expect.
Although that also is not always desired.

if Input.is_action_just_pressed(“quit”): get_tree().quit()

That should work, if I used this method, then I should have a confirmation prompt regardless. Even better, esc can be pause menu with game options.

Thanks for the feedback, I will experiment with the configuration.

GodotUser | 2019-07-24 00:44