Is there a way to close a game using GDscript?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Chemo
:warning: Old Version Published before Godot 3 was released.

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?

:bust_in_silhouette: Reply From: lukas
get_tree().quit()

More info in handling quit request tutorial.

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

Jukepoks1 | 2016-12-30 15:40

@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.

lukas | 2016-12-30 15:58

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)

Jukepoks1 | 2016-12-30 16:18

The link in the answer is broken.
The correctly working one seems to be:
Handling quit requests — Godot Engine (latest) documentation in English

SasQ | 2017-12-13 13:42

Thanks. I edited the link in the answer.

lukas | 2017-12-14 10:41

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?

Xian | 2019-07-21 03:17

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.

James Sauer | 2020-04-12 00:57