Stop Execution and Close Game on Error

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

I’m checking whether a custom file is in the right format or not corrupted.
If not, the game should give a blocking error message and close on OK.
But I can’t find in depth information about dealing with errors.

How I get the script and calling line of GDScript to add to the a print message?
How I pop up an error message dialog box? (I’m developing for desktop)
If the data is not right, I can see a lot of issues if I let it go but calling get_tree().quit(), execution continues until the game is actually closed. There is no way to force it?

:bust_in_silhouette: Reply From: eons

Is horrible when a game closes with an ugly window and frozen background (or just quit) because an expected error occurred (like files corrupted).

You can autoload a scene that has ready some dialog and a nice background, and maybe error information and bug report instructions, to put over the current scene when error happens.

Your error handling scene could listen for events, signals or just have a method to add the error window and remove the calling scene from the tree (and maybe write an error log).

So when an unexpected errors occurs (like a file that doesn’t exists and I forget to handle a possible error for this case) in release mode no information what so ever, no mechanism to close or log it and the game will just continue using unexpected data?

Christian | 2016-12-28 15:15

are you requesting a way to be able to handle unhandling error no matter what it comes?

volzhs | 2016-12-28 15:47

Most languages will exit or output some kind of error even in release mode when you try to reference something in a null variable (invalid memory reference). If you don’t handle them these errors are handled automatic. I’m asking whetter there is a feature like this in this language.
Anyway if there isn’t, its fine. I really like the suggestion above for expected ones and I hope I’ll never forget to check null values.

Christian | 2016-12-28 16:09

Normally, when you don’t expect an error (not even a try-catch) and something really bad happens, the application crashes.

And Godot may print something on the console, need to check.

eons | 2016-12-28 17:57

Ok, is really hard to make the stupid engine to crash xD

Just was able to get a segfault trying to use a method on a null variable without debug enabled.


There are stuff that should not pass a playtesting, like null variables, bad node names or method typos, exporting with debug enabled will throw a message on the console when errors occur.


For errors like corrupted or edited save files, you will have to do the consistency checks (not a common practice) when saving and loading to prevent problems.

eons | 2016-12-28 19:02

So… So far, no way of stopping the execution or throwing a severe exception?
This exists in every single language or platform.

jeancallisti | 2022-11-27 20:45

:bust_in_silhouette: Reply From: jeancallisti

It doesn’t fully crash the game but in debug it will forcefully interrupt it:

assert(false)

OR

assert(false, "you done messed up!")

There’s also

get_tree.quit()

but I don’t know exactly how to use that one. Read more here : Handling quit requests — Godot Engine (latest) documentation in English


On a different note:

It’s horrible when a game closes with an ugly window and
frozen background (or just quit) because an expected
error occurred

No. It can be good practice to forcefully interrupt a program, for example if some config files are missing at startup or if some essential backend could not be initialize. You might not be able to know exactly what the problem was but you know that if the game continues it will be in a inconsistent state, and that’s the worst. However it’s definitely bad practice to pretend that this scenario does not exist. Source: I’m a senior software developer in corporate environments.