Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I setup the esc key to exit Godot Application?

Tags:

godot

gdscript

I have a question about something I'm getting stuck on with a new 2D project. I added a sprite background and it shows no problem when I run the test. I setup the esc key as "key_exit" in the Input Map. Then I created a node2D as the root and added the following script to it:

extends Node2D

func _ready():
   if Input.is_action_pressed("key_exit"):
      get_tree().quit()

It doesn't work. I'm trying to create a simple loop that listens for esc key presses and quits when I press the escape key. If I add get_tree().quit() without the if condition, it quits as soon as it starts. How do I get it to "listen" for my if condition? What am I doing wrong here?

like image 267
Matthew Avatar asked Dec 23 '22 18:12

Matthew


1 Answers

I figured it out. I was able to get it working with the following:

extends Node2D

func _ready():
    set_process(true)

func _process(delta):
   if Input.is_action_pressed("key_exit"):
      get_tree().quit()
like image 186
Matthew Avatar answered Dec 26 '22 06:12

Matthew