-
-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Exit MainLoop script with a custom error code #7557
Comments
(Examples below are for 4.x; I haven't tested it in 3.x, but should work too.) You can extend extends SceneTree
func _init() -> void:
if error:
printerr("Error message.")
quit(1)
return
print("Success message.")
quit(0) If you don't like 3 lines of error output, then you can use void return: extends SceneTree
func fail(message: String) -> void:
printerr(message)
quit(1)
func success(message: String) -> void:
print(message)
quit(0)
func _init() -> void:
if error:
return fail("Error message.")
return success("Success message.") |
I'm aware that you can extend SceneTree for this, but there seems to be a bug where Godot processes are repeatedly spawned forever and they never execute, which doesn't occur with MainLoop. I'm not sure how to describe that bug or what is happening there so I made this proposal, since it feels like exit code functionality should be a part of MainLoop anyway. |
Indeed: // os.h
int _exit_code = EXIT_FAILURE; // unexpected exit is marked as failure
// scene_tree.cpp
void SceneTree::quit(int p_exit_code) {
_THREAD_SAFE_METHOD_
OS::get_singleton()->set_exit_code(p_exit_code);
_quit = true;
}
In 3.x there is OS.exit_code property. In 4.x, the exit code can only be set via
This breaks backward compatibility. |
How would one do an expected exit from MainLoop without this feature? It seems weird that the docs would encourage you to cause an "EXIT_FAILURE", based on the example here in which exiting is expected.
Definitely. |
I agree that the current API looks weird since you can't set the exit code via We probably could restore the
Or we can add a new API exclusively to the |
Perhaps we could do the following:
I think that would solve all the problems at a small cost:
|
The crash handler is called regardless of the assigned exit code. Remember that exiting on an unclean signal such as SIGSEGV or SIGFPE will modify the process' exit code (128 + signal number). |
Method Specifying your exit code from a console program is such a basic feature! |
OK, so at least it looks like this iffy workaround works for func quit(code: int) -> void:
var s := SceneTree.new()
s.quit(code)
s.free() But I really don't like it. |
Readding the As for having
Crashing should exit with the signal This appears to already be the case… mostly. Here, I'm using
I'd expect it to be |
I agree that there's a missing feature now to implement a MainLoop with a custom exit code. Moving @dalexeev's proposal in #7557 (comment) also makes sense to me, and may be better than introducing a fairly high level godotengine/godot#89229 evaluates going back to
Slight off topic, but I believe the explanation here is:
|
I worked on 3 in godotengine/godot#99254. Technically that's about any detected errors and not crashes. It's guarded by a While doing that I came to the same conclusion as 2. I.e., |
Describe the project you are working on
I'm writing a C# game that requires a pre-build script to run to compile some assets, so I have added this to my
.csproj
file:I need to use a GDScript script for this, as I need access to PCKPacker as part of this build step. My script extends MainLoop instead of SceneTree, because SceneTree causes the script to repeatedly execute and not finish (I have no idea if this is a bug with msbuild or Godot, but it is a separate issue).
Describe the problem or limitation you are having in your project
Currently, returning
true
from MainLoop's_process()
and_physics_process()
functions exists the program with error code 1, causing my build to fail. It used to be possible to useOS.set_error_code()
before exiting to change the exit code in Godot 3.1, but that was removed in Godot 3.2 with the addition ofSceneTree.quit(0)
where you could specify the exit code as an argument.Describe the feature / enhancement and how it helps to overcome the problem or limitation
Instead of returning a
boolean
from MainLoop's_process()
and_physics_process()
functions, let them return anint
instead which determines the error code.Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams
Example MainLoop script:
If this enhancement will not be used often, can it be worked around with a few lines of script?
This can't be worked around by anything in Godot.
In my case, I can ignore the error code by adding
IgnoreExitCode="True"
to my.csproj
(but this isn't ideal):Is there a reason why this should be core and not an add-on in the asset library?
This is a very standard feature.
The text was updated successfully, but these errors were encountered: