1

I'm developing a mobile puzzle game in Unity, when I exit my game to the home screen and return back- the game continues from the same spot and it seems to be fine (without overriding onPause/onStart), am I missing something? Do I need to store variables when exit? What do people usually save? I'm afraid to have bugs in the future.

SHAI
  • 789
  • 3
  • 10
  • 43
  • I assume you're talking about a mobile device - are you sure you're actually closing the application (ie. Opening running processes to kill it), or are you just suspending it temporarily then going back to it? If you're not actually closing it, the app will reside in memory and won't be missing any data when you return to it. But if you actually close the app, it will lose whatever game state data you had unless it was saved somewhere else. – Serlite Aug 23 '17 at 18:36
  • By "closing it" I mean- answering a phone call or even start playing a new app and then going back to my app after an hour... For example, on the iPhone no one really closes the apps when exits to the home screen. – SHAI Aug 23 '17 at 18:42
  • In that case, you're not actually closing the app - you're suspending it, since your phone will keep it in memory (until it runs out, at which point it starts purging data to make space for more recently-used apps). However, turning off your phone will actually close it. If your intent is to preserve game state even in the case of the app being stopped and restarted, then you will need to write code to save data to a static file, then reload that file when the game starts. And people do close apps on their own - with limited RAM, they'll often close idle ones so performance doesn't suffer. – Serlite Aug 23 '17 at 18:53

1 Answers1

2

I missing something?

Yes

Do I need to store variables when exit?

Yes

I'm afraid to have bugs in the future.

Yes, you will. You will run into lost variables issues. It is your responsibly to implement this. Just because everything looks and works fine now doesn't mean anything. The behavior is different on different platforms and devices and also depends on how many apps are already running on the background + current available ram. Usually, you use a class to store all important variables in your class then serialize and save them.

See this post that explains how to do this and provides a wrapper to easily save and load any class. You have to save your game state when Unity is about to be interrupted. These are the functions that can be used to detect this and you must know about:

You have to decide which ones to use to save the data. Sometimes, you must use multiple of them due to the behavior of each one in each platform. It's worth reading the Doc on each one to understand what they do on each platform.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Ok so I understand that I need to save every class to a json file. Do I need to create a different wrapper for every class I have or is there some tool that you can recommend (maybe from the asset store) that can automatically scan everything and restore. Also, what about the game objects preferences (some don't have attached scripts) what's the best way to save all the active gameobjects (their transform/other information)? – SHAI Aug 23 '17 at 19:25
  • No. The wrapper I provided uses C# generic which means it can save any class without modification. For the rest of your question, I get asked that a-lot. Don't save everything in the scene. Save what you need to be saved. Also, you don't save Transform. You create a simple class that holds the position, rotation and the scale of that transform and that you should save. I suggest you create a SaveManager script that every GameObject registers to when they are created. You can make each Object add itself to the List in the SaveManager so that they can be saved later on. – Programmer Aug 23 '17 at 19:35
  • What do you mean don't save everything in the scene? I have to save: new game objects (clones) that were added during the game, and also those who come with the scene when it starts (at least their information) and not only the transform but also other information that defines the objects. And about my classes, I guess I have to change now every private variable to be a SerializeField right? – SHAI Aug 23 '17 at 19:48
  • You can't save Gameobject. You save the scripts that are attached to them. That's what I meant. Like my answer suggests, each script that is attached to the GameObject should have class that stores the data of them. These classes should not inherit from MonoBehaviour. This simplifies the saving. For your last question I don't even know. You can do a simple test to see if private variables work. All I know is that you just add `[Serializable]` to the top of the class. – Programmer Aug 23 '17 at 21:48
  • Now, if you don't want to use SaveManger that stores what to save but just want to save everything. It complicated and requires reflection and more work. See the comments from [this](https://stackoverflow.com/questions/45388307/how-do-i-save-load-the-state-of-every-gameobject-in-a-sandbox-game) post. This comment section is getting longer. This post asked if you have to use onpause-onstart-ondestroy so I provided which functions must be used. – Programmer Aug 23 '17 at 21:50