Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question How Can I save game If user tries to forcefully close my game for example from task manager

Discussion in 'Scripting' started by kader1081, Mar 5, 2023.

  1. kader1081

    kader1081

    Joined:
    Oct 16, 2021
    Posts:
    311
    When I play game if something bad happens I try close from task manager but game saves itself how can I do that . I use a static method for saving game I call it If I want to save
     
  2. dogmachris

    dogmachris

    Joined:
    Sep 15, 2014
    Posts:
    1,373
    That's a tricky one, but fear not, for I have just the solution for you!
    Code (CSharp):
    1.     void OnApplicationQuit()
    2.     {
    3.         SaveGame();
    4.     }
    Here the SaveGame method, that's being called contains your code to save the game. OnApplicationQuit() should be called when the game is closed, even when the player forcefully closes it - if it's not frozen that is.
     
    kader1081 likes this.
  3. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    710
    One caveat: if the user hits "End Task" in the Task Manager, Windows kindly asks the program to quit. I'm not sure if it's identical to closing the window or hitting Alt-F4, but its' the same idea. You should be able to respond to that.

    If the use hits "End Process", Windows immediately kills the game. There is no way to respond to this (which is why it works even if the program is frozen).
     
    Bunny83 likes this.
  4. kader1081

    kader1081

    Joined:
    Oct 16, 2021
    Posts:
    311
    Does this script have to be attached to an object that on the scene .
     
  5. dogmachris

    dogmachris

    Joined:
    Sep 15, 2014
    Posts:
    1,373
    Well... yeah. Preferrably to one that will always be in the scene during the game, you know, you wouldn't wanna have the GameObject that saves your game run out of health or anything.
     
    chemicalcrux likes this.
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    4,521
  7. kader1081

    kader1081

    Joined:
    Oct 16, 2021
    Posts:
    311
  8. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    4,521
    Do I need to answer that?

    One is a unity message, the other is a static delegate.

    Just read the documentation.
     
    Kurt-Dekker likes this.
  9. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,043
    If your player is using the task manager to shut the game down, then they probably don't want to save anyway.
     
  10. dogmachris

    dogmachris

    Joined:
    Sep 15, 2014
    Posts:
    1,373
    Still there might be good reason to. Saving in OnApplicationQuit() may for instance create a temporary savegame and ask the user if he wants to restore the previous game state, once he comes back, just in case he just forgot to save or couldn't for some other reason. The player could still discard the option.

    Then on the reason of why a player wouldn't want to save: In older less sophisticated online multiplayer games, where the host was also one of the players, forcefully closing the application shortly before losing a game, was abused as a way to avoid messing up the win-loss-stats. That's a scenario in which a player really shouldn't be given the choice.
     
  11. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,043
    the player may also force-quit if the player gets stuck in a wall, falls out of bounds, or some other non-winnable, non-continueable scenario. So if you do save, then make sure the player can restore to a stable condition.
     
    kader1081 and spiney199 like this.