I’m implementing saving in my game and I’m finding conflicting info online on how to save on exit in Unreal. Which of these is the correct way?
A. Listen to the event dispatchers
ApplicationShouldUnloadResourcesDelegate (alt+F4 on PC will trigger only
ApplicationWillTerminateDelegate, it seems).
B. Implement a custom GameEngine class just for this, as is described here: https://answers.unrealengine.com/questions/127897/event-on-close.html
BeginDestroy on my GameMode class. (This crashes my game in the call to
UGameplayStatics::CreateSaveGameObject so I guess that if this is the way to do it, then I’m doing it wrong?)
D. Something else?
Which is the most reliable way to save on exit? (Note that PC is currently my only targeted platform, but I would prefer to know the best way in general that covers all platforms.)
Also, is doing async save using
UGameplayStatics::AsyncSaveGameToSlot on exit a bad idea, or is it actually good since Unreal can continue other shutdown logic and waits to make sure the save is finished before final termination?
I would hook that up to the user interface. Your users click some button to exit the game, right? Have a dialog pop up asking “Are you sure you want to exit etc.?” When they click to confirm, you start the save logic.
You might have to go about it a little differently, depending on your specific setup. But you wouldn’t have to worry about the application exiting before the save is done and all. Also: Consider this line in the documentation of
This may be called when the application is getting terminated by the OS.
Seems like at least option A is not that reliable.
Thanks for replying! My goal here is to make saving completely automatic, so I don’t want to ask the user whether they want to save.
ApplicationWillTerminateDelegate won’t be called when the user kills the application through Task Manager, turns off the power of their PC, or when the game crashes. Not saving in those cases is as expected for the user, I think. Unfortunately the documentation doesn’t mention whether there are other situations than that where this delegate isn’t called.
I see. It would still work roughly the same, though. They perform an action that is equal to “wanting to quit the game”, like returning to the main menu, quitting to desktop or whatever; some button is pressed somewhere. When that happens, do the saving, then do the rest.
As for all the situations where
ApplicationWillTerminateDelegate is not called, your guess is as good as mine, but I can imagine that different platforms handle this differently, too. This is an extreme example, but think about publishing a game to HTML where there really isn’t an application to terminate. At best, there’s a browser tab that closes, but I have no clue how UE handles any of that.