I searched a lot, but didn't find anything about "checking for errors in a GDScript" and it would help me a lot if someone actually has the solution to this question!
In context, I need to check for errors in a GDScript so I can set it to be able to run or not if it has errors. In this case, if it has errors, I don't want that GDScript to run, but if there's no errors, then it can run well.
Check for Errors in a GDScript
- Edited
If an error is encountered, the project will stop running and display an error message.
Are you referring to warnings, rather than errors? You can make warnings be treated as errors:
Project Settings / General / Debug / GDScript
- Edited
DaveTheCoder You can't run errors. But you can run plenty of bugs
xyz actually i found out that godot is like javascript, even if your func errors out the other processes still run.
Good case is where you leave half written line in your gdscript, an dlaunch the game it will run but wont load that gdscript file.
If the script is not essential you wouldnt even know it failed to run.
brenioks as for the error, did you mean you want to explicitly throw error if the funtion branches the wrong way?
maybe push_error()
is what you want?
func evaluate(command, variable_names = [], variable_values = []) -> void:
var expression = Expression.new()
var error = expression.parse(command, variable_names)
if error != OK:
push_error(expression.get_error_text())
return
- Edited
kuligs2 Good case is where you leave half written line in your gdscript
You can't really run half written lines. If it runs, it's a complete expression that at least evaluates. For example having a line with only a single variable name is valid in most languages. It's not considered half written by the language itself.
kuligs2 actually i found out that godot is like javascript, even if your func errors out the other processes still run.
I love javascript. If it was a d&d character, I'd be true chaotic neutral
You need to make a distinction between two fundamental types of errors: compile time errors and runtime errors. They'll both stop execution but the former will do it immediately upon start or sooner (like syntax errors) while the latter will do it when the offending line is executed (like trying to access an object via a null reference). Compile time errors are a piece of cake from debugging standpoint. Runtime errors, however, can be sneaky an some offending lines may execute very rarely. Every interpreted language strives to minimize them in various ways, for example by introducing compile time type checks like GDScript or TypeScript.
xyz well i dont get it if i have faulty line in different script it fails on a line that is perfectly good to run.
The errored line where the code stopped executing had the correct data
yes the faulty line was somewhere in the player.gd script but it didnt throw at that line but it threw error in the world.gd script where i instantiate player.gd..
Sometimes convoluted spaghetto code can throw you off and you keep chasing your own shadow for a while trying to understand why it dont work even that the data is correct.
- Edited
kuligs2 "is mayo an instrument?" is a syntax error, meaning it's a compile time error. It's detected prior to running the code and editor informs you about it. You can still run the project as that particular script may never actually run in the project . Your second error is a runtime error. It can't be detected prior to running, but once it is encountered during runtime the script has not way of proceeding other than to throw an exception and stop the execution
- Edited
kuligs2 I meant the entire code actually.
If there's any errors in a code (GDScript), I want to print something to the console. I know Godot does that already, but I want to do it myself so I can display the error inside my App's own console.
That's because I'm trying to make a Code Runner/Executor and I need to display, inside the App's console, the errors that can happen when we run a code (GDScript).
PS (If this helps somehow): The code runs by creating a GDScript, setting the GDScript code to be the text from a CodeEdit, sets that GDScript to be the script from a Object, set its process to true
and it runs.
- Edited
You may need GDExtension or a custom C++ module to do that. I doubt if GDScript itself has that capability.
https://docs.godotengine.org/en/4.2/classes/class_gdextension.html
https://docs.godotengine.org/en/4.2/contributing/development/core_and_modules/custom_modules_in_cpp.html
The official documentation doesn't cover this well. There may be third-party documentation.
- Edited
- Best Answerset by brenioks
Ok. I actually got the answer and it was at my face the hole time!
DaveTheCoder For God's sake GDScript has that capability and it works very well. Well enough that we can get exactly what the error in that GDScript is.
The solution is really simple by the way! It's just to check if the script.reload()
isn't OK
Code:
# This code runs inside a CodeEdit, so there's where the "get_text()" comes from
func _on_play_button():
# Create and set the code for a GDScript
var script = GDScript.new()
script.set_source_code(get_text())
print("Script Sourced!")
# Reload the GDScript, so it can run the code and check if it has any errors
var error = script.reload()
if error != OK:
# If it has errors, we printerr to Godot and end this function right here
printerr("error")
return
# If there was no errors, we print a "succesful!" message or something like that
print("Script loaded with no errors!")
# the Object's script is set to this script and it runs!
obj.set_script(script)
By the way script_reload()
returns an Error
that can be OK
, FAILED
or ERR_...
(meaning that there's a lot of different errors that can be thrown by this method). It was very easy to solve, I just didn't know that script_reload()
returned an Error
to start with.
PS: If you try this by yourself, you'll need to know that it'll throw an error (In the Godot Engine itself) exactly when the code gets to the script_reload
if you're running DEBUG, but by continuing or running it in RELEASE, it works yay!