Is there a way to save the state of the game so if I hit the home button and then come back in, the game is exactly as I left it?
Not automatically - this isn’t unique to Unity made apps, the iPhone OS quits your game completely when it is interrupted by a call or the menu button. Your only option is to save out a bunch of information, which will be unique to your game’s needs and complexity (like positions of enemies, the current score, player health, etc.) and write a script that re-create the game state as it was the next time the game is booted up. This kind of information can be saved via PlayerPrefs - search for it in the scripting reference, it should be self explanatory.
Oh yes, I’m painfully familiar with Mister NSCoding.
Very cool, PlayerPref is exactly what I was looking for. Thanks!
Does Unity provide any simple way to perform last-minute saving, etc. when the application quits (like ObjC’s applicationWillTerminate method, for example)?
I think you’d simply use PlayerPrefs in conjunction with function OnApplicationQuit (), which runs when the app is quit by any means (phone call, menu button, etc).
Near as I can tell there’s no way to save an array with PlayerPrefs, right? And you need to know the string beforehand to call the get functions.
I guess, if I’ve got a variable number of objects of some type, I could make an array by appending a number to the key. EG: Object-1, Object-2, etc. then try "GetInt(“Object-1”) until I get defaultValue back.
but this would be a pain, as I’d like to save transforms (or at least vector3s), etc.
Is there a better way?
This seems to be one area where Unity is not providing much of an API. NSUserDefaults allows saving arrays and all kinds of data structures (including hashes so you could have a complex data structure.)
Hopefully unity is planning to provide some mechanism to serialize game objects and then de-serialize them transparently, and that’s why the API is thin right now.
![]()
you need to know the string anyway. How do you intend to load back the objects on the next start if you don’t know what you want to load?
Simple, I load the objects from a key I know and I get an array of them which can be variable size. I may not know how many objects were create in the last session, but with a data structure I can get this data.
Without it, there’s no way to store game state practically between launches.
Unless I’m missing something, this seems to be a one of the few areas where Unity doesn’t have a good implementation… (especially since the phone provides support for this already and they just need to map to it.)
Concatenate the array into a string and save that, then use Split to get the array back when reading. Also, store array data as a bunch of chars, and write that as a string (since a string is just an array of chars). I agree that using arrays directly would be best, but in the meantime it’s pretty simple to work around.
–Eric
I was thinking of how to simply implement A save game feature…and came up with the idea of just releasing passwords or symbols that the player has to write down at the end of each level not a new idea and Its old school and lacking the finness of a hardcore save state feature but it would work without to much hardcore codeing…
The only reason for ever using passwords was because of not having any random-access storage. If a password would be sufficient to save the game state, then you might as well just do the extra 1% work and save the password, and restore from that instead of making people do it manually, which I guarantee would go over as well as a lead balloon these days. ![]()
Really, there are no issues which prevent anyone from saving and loading game states as complicated as you want them to be. Realmaze3D automatically saves and restores the game at any time…the maze itself is just a couple of integer values because it’s generated from a random seed, but there’s plenty of other data, like the trail of breadcrumbs, all 100 high scores, etc.
–Eric
on reflection Your Point about physically writing save game states is probably correct and its probably not one of my hall of fame great ideas
So I’m guessing I’m fairly fubar’d if the game is relying on physics?
How do emulators handle “State Saving?” It seems like almost every retro game emulator has this feature and it doesn’t appear to be game specific.
Is there something specific about Unity and/or iPhone that prevents a Save State system from working properly?
emulators just save the whole memory like Virtual Machines do if you suspend the virtual OS
You can not do that on the iphone, you aren’t granted access to your app memory from outside to serialize it. But even without that it wouldn’t be possible. You have a few moments to save the state not minutes, after that your app is forcefully thrown out to go to the home screen / accept the phone call.
But the chance that you would ever see this happen goes towards 0 as apple wouldn’t accept your app and thus it wouldn’t appear on the appstore.
You need to store state relevant information and replicate the state at a later point again from those information
Thanks for the information dreamora. That makes sense. :o
No, why?
–Eric
I mean as far as saving the state in the strict sense.
Nothing preventing it as far as I know.
Just save the position, velocity, etc. for each object.
–Eric