This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+36 votes

Still a newbie here.

Is there a way to close a game using GDscript?
I'm trying to use the texture button as the exit button and I wanted to close the game without using the close button like the me.close() in Visual Basic. Or am I doing it wrong?

in Engine by (24 points)

1 Answer

+66 votes
Best answer

More info in handling quit request tutorial.

by (675 points)
edited by

How about restarting the scene? I know it's not practical for game, but for debugging it makes some things faster.

@Jukepoks1 Easiest way is to change scene to current scene:


The current scene will be freed and then loaded again.

Thanks for fast answer!

I figured this would work little better, so the filename is not actually hard-coded:

var currentScene = get_tree().get_current_scene().get_filename()
print(currentScene) # for Debug

The link in the answer is broken.
The correctly working one seems to be:

Thanks. I edited the link in the answer.

Im using get_tree().quit() too to close the game in-game but it is giving errors on my console(not the outputbox, literal non-gui console). heres the error

OpenGL ES 3.0 Renderer: GeForce MX130/PCIe/SSE2
ERROR: ~List: Condition ' _first != __null ' is true.
   At: ./core/self_list.h:111
ERROR: ~List

I want to exit my game error free so what do I do?

Regarding the last question:

I'm not quite sure I fully grasp the relationship of classes to nodes yet.
But as I understand, this error comes up when there are class instances initiated with the new() method that don't get cleaned up at the end.

E.g. in a script, that is attached to a node:

const Potion = preload("res://")

var my_potion

func _ready():
    my_potion =

It looks like instances that aren't attached to a node, like my_potion, won't get cleaned up automatically.
You need to do it manually like so:

func _exit_tree():

_exit_tree will automatically be called on quit, if the script that contains it is attached to a node, like in this example.
It will, however, not be called, if you put it into itself, because that script has never been in the tree in the first place, as it wasn't attached to a node.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.