Saving a game automatically on app close or app background

I believe it is normal to save the game when the app is closed (on eg. a swipe app close operation from android task list)

I am tracking numbers of player lives, and I am comparing the “last saved datetime” to the now datetime on game load. But this is nothing worth when I can’t “last saved datetime” set to the app close time. I’ve tried google, but I don’t get anything else than that delegates could be my answer, but I find the delegate part bad documented, especially on the blueprinting side. As for what I’ve seen in other games, there must be solutions to this, but I am stuck. Do anybody know I can make the game save on close?

My alternative would be saving the game every second or so, on a timer, and that sounds a bit bad for performance, right?

Hi, you can use an event from game instance called “Shutdown”, and plug your save logic on it.
I’m using it to save datas when the app closes, and it works.

Thanks! Interesting. Do you know if the save action has to be in the game instance as well? Now, I have the save/load game handling in my pawn, and it probably shuts down the pawn before reaching to cast to it, would be my thought I can get the game instance init event to work, but not the shutdown.

You cannot bind this event inside your pawn (I’ve tried with an actor but the event doesn’t show up) but you can try to get your pawn and call its save function from the game instance on shutdown.

Yes, that’s the part that seems to fail. Perhaps it closes down the pawn before the gameinstance reaches to cast to it to run the save routine. :frowning:

Then I can’t help, I’m really sorry

Thank you anyway for taking your time. As we speak, I am restructurizing my entire game, so that it does follow a better practice for Game Instance use. I hope it will work. I will post my result here anyway.

No luck in moving the save game routine to the Game Instance. It still doesn’t save. When I fire the save / load from buttons, the exact same save / load routines work. So there must be something with the On Shutdown. Here’s the save. Not sure what else to try now. :frowning: Testing directly on the mobile from Launch.

Bumping this thread. Saving the game on Event Shutdown does not work for me. I’m really stuck on this, and would really appreciate if anybody could point me to a solution. I tried to package the project and install it on the test phone. So it neighter works on local testing directly from editor, or from installed package on the phone. Game save works when triggered from inside the game otherwise, but it’s the shutdown part that is the problem. The engine version is 4.26

By the time EventShutdown fires, everything (or at least the stuff you care about) is destroyed. You need to save your game from the actor itself through Event End Play. I’ve already tried this and it works; the test saved the location of the player when the game quit, and when the game starts back up, the location is loaded and the player is set to that location. The event also gives a reason, so you can save only if the game is quitting.

Also, instead of saving from the actors directly, you can have them send their data to the game instance, and then save all of it once from the EventShutdown event.

Maybe there’s something in the project settings that I’m missing. I don’t know. It just doesn’t work .

Tried it from the pawn, from EventEndPlay. It looks like this:

And the save game blueprint in the gameInstance, which should work in the first place, looks like this. (and it works in other situations than the shutdown process)

The way I’m closing the game on the mobile unit, is swiping the app off the mobile screen just the way you close apps on a mobile phone, or pressing cancel from the running instance notice in unreal engine.

Right now my workaround during development is to save the game with a loop every second.

This is what my test looks like:

If that doesn’t work, idk what the problem is. It could be that the game is closing before completing the save. If the editor can get errors from mobile devices, you can force an error by accessing a null reference to check if EventEndPlay is firing: