So I’m working on a game and I want to have maps and give the player the ability to travel between each map. Each map contains about 20 gameobjects and I need to preserve their positions, variables, and whether or not they are alive or dead when I transition back and forth between the scenes. So say I load scene 1 at the start of the game, I’m playing, I kill one guy and I get sent to scene 2, I need to be able to go back to scene 1 from scene 2 and have everything be just as I left it, including the dead guy being dead.
I feel like PlayerPrefs, DontDestroyOnLoad, and reloading scene 1 would not work. It seems PlayerPrefs would be too complex as each gameobject has quite a few varibles that change. DontDestroyOnLoad would carry gameobjects to scene 2 which I do not want. Reloading the scene would reset everything.
I was thinking, a solution might be to have one big scene and have different sections of it be different maps. So since I never load a scene, I don’t have to worry however this worries me because I feel like performance would suffer since there are gameobjects instantiated but super far from the player and not in use. I thought about maybe having an if statement in the update method that checks to see if its respective map is loaded. So if map 1 is loaded then gameobjects of map 1 would work but game objects of map 2 would not because map 1 is loaded. I also thought about possibly disable game objects of maps that aren’t loaded but i don’t know how much performance this would cost.
Any ideas guys?
The easiest way to do this would be to serialize/deserialize the game objects into memory when you change scenes. BinaryWriter and BinaryReader allow you to read and write to a memory stream. You can then return the stream bytes as an array and store it somewhere that will persist between scenes. If you need more help with this let me know.
You can Serialize objects as json or some other format binary even.
Basically if you want to free memory then you need to serialize them somewhere else so some file format is one of top options.
If you are ok with keeping them in memory then Singleton class that keeps game state or BinaryReader/Writer.
So I would serialize to file when I load scene 2 and then when loading scene 1, if file exists load items from file. If not, load them as if new game?? Or would it be each object checks file for stored values and acts accordingly.
You can do that in many ways I would avoid each object checking for stored values in file.
Generally one of good approaches for designing something like this would be:
-
Make class that would hold game state, because you want to serialize to a file you would need basically references for all objects that exist in scene so you can get their states. So this class would basically be like reference holder for everything.
-
Make game state serializer class (you can have this functionality withing game state class it self or implement it as interface and inherit it then by game state class this really depends on your architecture). Point of this is that you can call method for serializing game state and it would in turn get all object states serialize them to json or binary or some other format and save them to disk.
-
Have some SceneManager class that does deserializing (or better yet have game state deserializer class) that basically gets the file reads the data and creates you GameState class, then SceneManager reads game state class and creates all objects needed and sets everything as previously serialized.
This is just one way it may or may not conform to your architecture but roughly you would need to do:
- Serialize game state (meaning all objects, states and data you want to save)
- De serialize game state
- Make everything as per deserialized game state (recreate objects and data as it was de serialized)
You can implement Serialization as just one class reading from all objects or you can make some interface ISeriazable that each object will implement and each object would then be responsible for serializing its own small chunk of data and main Serializer class would gather all that and compose it in to larger file. Or it would allow uniform data gathering from all classes.
Sorry for throwing so many stuff here but it is a big subject and it can be done in many ways you probably need to research a little bit about it because it has great impact on architecture.
Hope it helps…
1 Like
It really does help, gives me various directions to go in. Also, thank you for “throwing so many”, gives me a lot to think about and now I’m beginning to realizing how great an impact it can have. Going to be googling away. Thank you again