Throw exception or error?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By rico345100

I’m making some kind of utility script that can use with any godot project. In the script, it requires some parameters, so I want to stop the game and warn the developer if the value is empty.

Nothing related found in documenation. In Unity, when script throw exception, game stopped and display the error detail(only in Unity editor play mode). How to throw exception or error from GDScript? Using Godot 3.1.1

:bust_in_silhouette: Reply From: Magso

If the value must be above 0 an error might happen regardless but you can use push_error

:bust_in_silhouette: Reply From: tx350z

I know this question is rather old but thought I’d provide an answer just in case someone is searching.

The best way to raise an error made by the programmer (i.e. not a runtime error) is to use assert(). It can be counter-intuitive though because an error is raised when the condition is FALSE. Therefore you have to invert the check you are intending. So, if a variable must have a value, then you check for the variable NOT BEING EMPTY.

Example:

assert( required_var != "", "ERROR: You must give required_var a value.");

Doc Link: @GDScript — Godot Engine (stable) documentation in English