The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

+1 vote

I would like to kill the program or terminate it from within the program similar to the get_tree().quit() function but without waiting for the iteration to finish or return anything.

Godot version 3.4.2
in Engine by (663 points)

there is OS.kill() method, I never tried using it though

2 Answers

+2 votes
Best answer

OS.kill(OS.get_process_id()) should do the trick. Beware, there is no way to catch this from your code (which means you can't ensure files are safely written before killing the process)!

Also, if you call OS.kill() after an infinite loop was started, the OS.kill() line will never be reached, which means your process won't be killed.

by (12,878 points)
selected by

Do you know? Will multi-threading still allow the OS.kill command to run? I'm asking this because the program is less of a game and more of a massive algorithm that could be put in a game but is quite slow even on my Ryzen 5500U and multi-threading would speed it up a lot since I have 12 cores. Or would that not allow the OS.kill() call to happen? Because having a thread that just kills the program after it has been running for more than a certain amount of time without the main algorithm finishing would be really nice I think.

Using multiple threads within your project should not impact OS.kill()'s ability to work. I'm not sure if OS.kill() will work if it's called within a non-main thread – it logically should, but please test this just in case.

–1 vote

Simple answer: D O N T

Killing your game without finishing anything leaves possibly hundreds of megabytes of ram allocated, possibly even corrupting your save file if you happen to have auto-save running while killing the game.

The reason it isn't quitting is because you're performing some expesive operation, or running a for-loop with many iterations. The only viable solution is to make the loop check for a quit signal.

An alternative solution

For example, if you have a for-loop with a lot of iterations (e.g. for generating a procedural world), you can do either of two things: Multithreading (the hard way), or use coroutines, this example shows yield being used to wait a frame for every iteration so Godot can check for inputs or the quit signal, but I recommend to look into yield and come up with a better solution, as each iteration will take a minimum of one frame this way (1/60th of a second):

extends Node2D

func long_loop() -> void:
    for i in range(999):
        print(i) # Example
        yield(get_tree(), "idle_frame") # Allow Godot to update

func _ready() -> void:
    get_node("Button").connect("pressed", self, "button_pressed")

func button_pressed() -> void:
by (104 points)

Killing your game without finishing anything leaves possibly hundreds of megabytes of ram allocated

Processes don't work like that on modern operating systems. The OS will be able to reclaim memory after the process is abruptly killed.

The part about potential savefile corruption is true though.

Like Calinou said a modern OS can just reclaim this I've seen this happen on my 21.04 Ubuntu after running a program with a massive memory leak (that an unknown person who is not me, made). I recently got my first Windows copy and even it has a system to take care of that. As for Mac, I have never used a Mac.

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.