Store and restore level contents

Hi everyone,

My game is made of multiple maps (scenes). Each map has some collectable points (automatically collected on collision). But when the player switches the map everything gets reset, thus allowing unlimited collecting of items.

Is there an easy way to get around this (IMHO very common) problem? My question is not how to keep the data in memory, that’s already solved. I wonder what approach would be the best to manage the “already collected” state. For example I could manually enter an ID for each coin but that’d slow down development heavily. I’m quite sory many people already stumbled in this situation and there’s a brillant way to solve the problem. I don’t really need code, only theory.

Please point me into the right direction.

Thanks,

Yhoko

you can use player prefs.

As JM mentions, if its simple things you want to store, like numbers or strings, you can store them in playerprefs, and load them from playerprefs at the start of the next scene (things like score, number of collectibles, lives remaining, etc).

If you have complex objects whose state you want to persist to the next scene, though, with components that can’t easily be written into playerprefs, you can use DontDestroyOnLoad().

Hm, sorry it’s a little more complex. This is going to be an RPG so later there will be chests with items. The player may pick up some or all items and everything should remain when switching scenes.

My question is not how to keep the data in memory, that’s already solved. I wonder what approach would be the best to manage the “already collected” state. For example I could manually enter an ID for each coin but that’d slow down development heavily. I’m quite sory many people already stumbled in this situation and there’s a brillant way to solve the problem. I don’t really need code, only theory.

well, there might be some “hack” to player prefs that you might use. I know that there is a “hack” to use arrays.

You specifically said across scenes; if you want the data to persist across scenes, then DontDestroyOnLoad() would do exactly that, I’m not sure where the confusion is.

For instance, say you had an Inventory script and a PlayerStatus script attached to your player object, and the Inventory script maintained a complex array of gameobjects stored in it that the player picked up in that scene, and the PlayerStatus script maintained the experience points he gained and the attribute points he has, if you used DontDestroyOnLoad on your player object, and loaded the next scene, all that data would remain intact.

If what you really meant was across gameplay sessions, then that is another matter entirely.

True, but when going back to the previous level all picked up items would be there again / all doors locked again / everything back to start. That’s my problem. Sorry for bad explaining.

Ahh, I see, you want to save the state of the level, not the player.

In theory, you could do the same thing, but you would have to have an object in the scene called something like “SceneManager”, and you would have to write a script that somehow keeps track of all the objects in the scene, what was collected and what was not, and use DontDestroyOnLoad on that. Then, whenever that scene is loaded, repopulate the scene with objects based on what the SceneManager says has been collected/not collected.

Yes, exactly!

How would I identify each object in the scene? Is there something like a unique ID (always the same ID when loading the map, of course) for each gameObject?

Sure I could use my own script class with “static i++;” but deleting one object would then cause massive problems.

Looks like GetInstanceID() does the job. The ID seems to stay the same when restarting the game.