Is there a function to make the engine raise an error and stop working?

Godot Version

4.4

Question

I am using a JSON file for some data in my game, but there are a few times I made a mistake in JSON such as forgetting a comma, etc. But then in the terminal, I just see

ERROR: Parse JSON failed. Error at line xx: Expected '}' or ','

but the game just keeps on running and the dictionary that supposed to get data from the json is empty, and I have been hunting down what went wrong for 2 times now. I do not want it to happen again, so right now, I have this:

var result : Variant = JSON.parse_string(content)
if not typeof(result) == TYPE_DICTIONARY:
   push_error("Invalid JSON format from file!")

For the above, I got the error in my console but this error got pushed way up by various other prints.

What another function must I use in order to make the engine stop working and blatantly yell right at me that the JSON in the file is invalid?

Help > Programming

1 Like

I don’t think there’s something built-in for it, but you could make an “error scene” and switch to it, or bring up an OS popup dialog window…

You’re probably looking for something like this:

var json:JSON = JSON.new()
var err = json.parse(content_string)

if err == OK:
	var dict = json.data
else:
	if err == ERR_PARSE_ERROR:
		OS.alert("PARSE ERROR AT LINE: " + str(json.get_error_line()) + ", WITH MESSAGE: " + json.get_error_message())
	else:
		OS.alert(error_string(err))

And if you want it to kill the process you can use OS.crash(“CRASH MESSAGE”)

Edit: To clarify OS.crash() doesnt print the crash message correctly in the editor in my experience which is why I use OS.alert()

1 Like