+25 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 (25 points)

1 Answer

+43 votes
Best answer
get_tree().quit()

More info in handling quit request tutorial.

by (672 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:

get_tree().change_scene("res://current_scene.tscn")

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
get_tree().change_scene(currentScene)

The link in the answer is broken.
The correctly working one seems to be:
http://docs.godotengine.org/en/latest/learning/features/misc/handling_quit_requests.html

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 Chest.gd, that is attached to a node:

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

var my_potion

func _ready():
    my_potion = Potion.new()

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():
    my_potion.free()

_exit_tree will automatically be called on quit, if the script that contains it is attached to a node, like Chest.gd in this example.
It will, however, not be called, if you put it into Potion.gd 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 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 webmaster@godotengine.org with your username.