How could I quit out of a Unreal game with a specific exit code? In Unity you can just Application.Quit(exitCode); but I could not find anything regarding Unreal.

I know you can use the QuitGame-node or FGenericPlatformMisc::RequestExit(false); in C++ but without the custom exit code that I am looking for.

Any ideas?

  • 2
    \$\begingroup\$ What are you trying to accomplish by doing so? \$\endgroup\$
    – user1430
    Jan 28 '19 at 16:51
  • \$\begingroup\$ We use a kind of launcher tool at work that waits for processes to close and starts other applications dependent on the exit code of the given application. \$\endgroup\$
    – Nicolas
    Jan 28 '19 at 18:42
  • \$\begingroup\$ One alternative approach might be to have your launcher tool look for a "FailedExit" text file or something in a well-known spot, and have some code in your game that wrote out the "FailedExit" text file containing the code on what would have been a non-zero exit. It's a little clunky, but should work. \$\endgroup\$
    – user1430
    Jan 28 '19 at 18:53
  • \$\begingroup\$ Yeah I thought about this solution before, but it's not quite as elegant and, as you stated correctly, also a little clunky. If your answer below does not work as I need it to I might fall back to this plan or just scrap it and use Unity entirely. Fortunately I'm not forced to use Unreal for this project. Thanks for the input, tho! \$\endgroup\$
    – Nicolas
    Jan 29 '19 at 8:52

You can always call std::exit with the desired code.

Unreal doesn't seem to provide an API to set the game's exit code directly, perhaps because not all target platforms can usefully do anything with the exit code and/or not all target platforms should really permit user termination (outside of the OS).

You can confirm this by looking at, say, LaunchWindows.cpp (link requires GitHub access to the Unreal Engine, which is free with registration with Epic) as UE 4.21. You can see that the return value of WinMain is driven by GuardedMain which in turn is driven by FEngineLoop::Init(), which only ever returns the constants 0 or 1. You can reasonably conclude that since

  • there is no code during the regular shutdown path handling any sort of user-defined exit code on at least one platform and
  • Unreal itself does not call std::exit and
  • Where Unreal calls platform-specific termination calls (like PostQuitMessage) it always provides a constant to the function, never a user-supplied exit code

that the code doesn't contain an abstraction for handling user-supplied exit codes.

  • \$\begingroup\$ I only really used blueprints in Unreal so I'm not really sure if this will work as I need it to. I'm going to test this tomorrow. Thanks @Josh \$\endgroup\$
    – Nicolas
    Jan 28 '19 at 18:46
  • \$\begingroup\$ You could make a custom BP node that called std::exit if you'd rather do that. If you're building the engine from source you could also add your own pipeline to push a custom exit code out, but that might be more work than you want. \$\endgroup\$
    – user1430
    Jan 28 '19 at 18:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.