Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Serializing an entire scene?

Discussion in 'Scripting' started by Lethn, Apr 2, 2016.

  1. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    So I'm studying Serialization and it does make sense, I'm looking specifically at the Data Persistance youtube tutorial that the Unity channel itself posted up.

    However what I want is to record the entirety of my scene like with most games that using saving and loading and have the player position along with all the destroyed gameobjects and so on. How would I do this? I read in the comments page you can actually Serialize entire scenes but I have no idea how you would make the scene an integer as the comment said.
     
  2. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Hope you don't mind me bumping this, I'm searching around but I haven't found an answer yet to Serializing scenes.
     
  3. seldom

    seldom

    Joined:
    Dec 4, 2013
    Posts:
    118
    Are you trying to save and load the gamestate? Then you don't need to store the entire scene, but only the changes it went through: the difference between the scene's initial and current state. There any multiple ways to implement that. Which one is best for you depends on your project.
     
  4. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Well what I want to worry about most are the pickups and the player position and yes, that's the main thing, I have a ton of collectibles and I want the game to know whether they have been destroyed or not after I load back in again when I quit.
     
  5. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Okay, so I found this Serializer asset in Unity which in theory does exactly what I wanted.

    https://gitgud.io/TheSniperFan/unityserializer-ng

    The older version could be installed but naturally I came across a bug so I went and grabbed it off this guys site where it's supposed to have proper support for Unity 5, however I came across this error.

    Does anyone already know how to fix it? I've sent an email to the address provided on the website just in case.

    I did get it running by the way by deleting the code that was causing the errors but unfortunately I still get this weird whitescreen which doesn't go away when the game is supposed to have loaded up a save this happened no matter what.
     
    Last edited: Apr 5, 2016
  6. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    I don't even know enough about serialization yet to know about gamestates, but if it lets me record the positions of gameobjects and so on or whether objects have been destroyed then that could be just what I need.

    I just need a decent tutorial that explains how to get things set up and saving a basic cube changing positions for example and then I could work from there, so If I move the cube from one position to the other position and save everything to a file, then exit the runtime, load up the runtime again and then load the saved file that has been created in the directory I've chosen.

    Or alternatively if I have a collectible object I pick up, destroy within runtime and keep it destroyed after I come back after exiting runtime entirely.

    The serialization tutorial I've looked at teachers you have to do this with basic integers but not with complicated gameobjects which is what you need for a finished game.
     
    Last edited: Apr 5, 2016
  7. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    Check out this tutorial on saving/loading: https://unity3d.com/learn/tutorials...ining-archive/persistence-data-saving-loading

    After watching that: your data-saving class can contain an array of your own serializable class, which would have the prefab name object, its position/rotation, and any other information about the object you may need. I'd recommend attaching a "SaveableObject" (or whatever) class to every object you want saved, and it has a string property for generic information that other scripts on the object can write to and read from. It also would have a string property containing the name of the prefab that can be used to respawn the object.
     
    olonge and Wrymnn like this.
  8. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    AHA!! Thank you! That's just what I was looking for, I have already been going through that tutorial actually but I was looking for something very specific, I had no idea it would be that easy.

    So it was just a case of the Serialize function itself not liking gameobjects rather than Unity not being able to do it? How weird -_- I take it what you're doing with the class by putting it within an array somehow turns it into data that serialization is more happy with?

    After that is it just a matter of creating public voids as normal and then just adding that information in if I want to use it on buttons and so on?

    Thanks for your help, but now my big question is how would you implement save game slots? Oh and for getting the game object in the actual unity editor and run time I can just use public gameobject like normal right?
     
    Last edited: Apr 5, 2016
  9. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    Easy enough: Ask the player for a filename, and save into Application.persistentDataPath+"/saves/"+saveGameName+".dat" rather than "playerInfo.dat" in the tutorial. When loading, use System.IO.Directories.GetFiles(Application.persistentDataPath+"/saves/"); this will give you a list of file paths which you can present to the user as their save game slots.
     
  10. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    lol wow, thanks for the detailed answer, now I swear this will be the last one, how would I check whether a gameobject has been destroyed or not? I'm aware of dontdestroyonload but I need the exact opposite, would saving the position data work here?
     
    ThermalFusion likes this.
  11. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    Generally, you would save additively (e.g. here's a list of all the things that ARE in the scene) rather than subtractively (here's a list of things that have been destroyed since the scene started). This is faster and simpler, and if you do it subtractively, you will need to create unique ID's for every object in the scene so that you know you are destroying the right one.

    If you need to, though, this can be done, taking advantage of the OnDestroy function - when one of those objects is destroyed, OnDestroy is called on that object, and it can tell the save-game manager "hey, object with id XYZ was destroyed", and the save game manager just keeps that in mind.
     
  12. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Okay thanks for all of that, you've been really helpful as I now have things to look up as well. I'm baffled by how few tutorials there are on this subject because it's such an important part of just generally making games, it reminds me for instance of when I was asking around about Centre Pivot/Pivot Level because I had absolutely no idea how important they were for raycasting especially if you wanted to do base building and such like I do.
     
  13. Cherno

    Cherno

    Joined:
    Apr 7, 2013
    Posts:
    515
  14. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Not strictly true. How to save is very game dependent, there are plenty of games where saving the game state is much more efficient then saving changes to the game state.
     
    Bunny83 likes this.
  15. MV10

    MV10

    Joined:
    Nov 6, 2015
    Posts:
    1,889
    I think a big problem is that it's heavily dependent on the specifics of your game. How to save the game state is really something that you need to decide upon almost at the very start of building the game. It isn't something you come along and add later. It's much easier if every system in the game just updates something that is tasked with dumping the game state on demand.

    There was another thread about this recently and somebody pointed out that if you do this correctly, the initial game state for a new game can even be treated like loading a saved game...
     
    Kiwasi likes this.
  16. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    I know this is a total noob question but people keep referencing the 'game state', what is it exactly? If you're using vocabulary I haven't heard of I won't be able to look it up myself.
     
  17. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    The state of the game! So where everything is and everything important that has happened.

    In the context of save games, it's all the information needed to put the game back to where it was when the player last saved.
     
  18. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    *shakes fist at programmers*

    http://gamedev.stackexchange.com/questions/4005/what-is-game-state

    lol it definitely isn't some magical bit of code that allows you to save the whole thing is it? I'll still need to make IDs for my gameObjects and so on.

    By the way, I'm asking about this particular stuff because I am very close to releasing my first game and this is all the major work that I need to do before I can upload it.
     
  19. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    State is a short hand way of saying 'all of the important variables'. Its the way the system is at a certain point in time.

    State is often opposed to path, which is how the system got to where it is. In proper terms state is independent of path.

    Here is the visualization we used in thermodynamics. Not sure if it helps.



    While the paths x and y are vastly different, the state at the summit is the same.
     
  20. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    There is no such thing as a magical "Save" function that would save the game your are making. You will have to implement a saving mechanism yourself by selecting which pieces of information are important to restore your game state.

    Your saving function can be as simple as writing a number to file that indicates the current level the player has reached, or can be as complex as saving the complete history of the player, what he has done and how into a full fledged database...

    It's really up to you and to your game how and what stuffs get saved.
     
  21. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I do wonder, all this talk of state and stuff. Is it possible to simply save an entire process, memory, threads and all, much the same way the OS saves things for stand by mode. Maybe a magic save function could be built this way.

    To the OP: Don't pursue this option. Its really programming navel gazing and wouldn't really be useful for games.
     
  22. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    LOL! Don't worry, I'm not that bad, I do now know though what sort of stuff to look for, what I find annoying is most of the tutorials all involve just numbers rather than gameobjects and so on, I'm sure I'll find documentation or something eventually but it's frustrating looking throught this stuff sometimes which is why I made this thread.
     
  23. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    HURRAY!



    Look what I found, a whole series on how to save gameobjects and load them up including keep them destroyed, that's perfect, thanks for telling me the new vocabulary guys because yet again just having different keywords to search for helped me find stuff.

    Hope this thread helps other noobs who are looking to learn about proper saving and loading.
     
    Last edited: Apr 6, 2016
    ArtyUwU and Kiwasi like this.
  24. Cherno

    Cherno

    Joined:
    Apr 7, 2013
    Posts:
    515
    I only watched the first video, but I see that it's a very game-specific approach to saving and loading data, meaning that it requires a lot of customization to make it work between projects/games. Personally, I prefer a more holistic approach that automates as much as possible and can easily be ported between projects. Your mileage may vary, but if you work on multiple projects it becomes important to share assets between them.
     
  25. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    This seems to be how save states on console/handheld emulators work. Want to replay an old JRPG, but don't want to experience the agony of 1 hour between save points? No problem, the emulator just dumps the ENTIRE MEMORY OF THE MACHINE to file at the press of a button.

    Of course, this is working with emulators of things like the PS1, PS2, GameCube, GameBoy, and other old pieces of hardware. It's viable because the size of their memory is really small. The emulator is also a VM, so I imagine that does making loading that stuff a lot easier than if you're working with the c++ runtime of Unity.
     
    Alverik and Kiwasi like this.
  26. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    I know I'm still a noob at serializing and so on, but wouldn't that cause a huge amount of problems on modern machines and in game engines if somebody did that? You can only get away with it on emulators because the games are so small right? If you did this with say, Fallout 4 for example, or some other similar open world game, you would be absolutely screwed, even on a high end PC.

    The Fallout 4 loading times on release were already bad enough to begin with :p
     
  27. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    If you are trying to build fallout four you are screwed anyway. ;)

    Small games like my Pond Wars could be memory dumped. But then again, the game is so small that saving it doesn't actually make that much sense anyway.
     
  28. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    I'm one of those gamers who utterly despises things like unskippable cutscenes and so on so I think it would be utterly hypocritical of me if I released a game with in-game cutscenes for example that didn't allow you to save just after a cutscene during a big boss battle or whatnot.

    Plus even if it's a small game with lots of building going on for example people are going to be very pissed off if they can't save their creations whenever they want and make the game keep track of their progress properly.
     
  29. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Pond Wars is a really, really small game. Think early 90s arcade style. There was no need to save many of these games, because the game play sessions are so short.

    It's entirely possible to use game design elements to eliminate or reduce the need for complex saving systems. Short play sessions is one tool. Checkpoints/levels is another.
     
  30. MV10

    MV10

    Joined:
    Nov 6, 2015
    Posts:
    1,889
    Run everything in a VM...
     
    Kiwasi likes this.
  31. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    So I've gone through the tutorials and yey it all seems to make sense, however typically I'm left with more questions, while the overall so called 'structure;' of the code is pretty easy to understand I think I need more tutorial videos to properly know the code behind how to keep gameobjects destroyed and so on, the instantiating looks simple enough but I know I'm not going to get through this with guess work.

    If anybody knows of a tutorial dealing specifically with the subject of using OnDestroy as StarManta described to save the state of the gameobject please let me know.

    Oh oh! Because of the tutorial series I've been following a have a proper error code now as well.

     
    Last edited: Apr 13, 2016
  32. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Oh! Oh!

    Nevermind! It looks like in his very last detail he actually goes into a lot of detail about all of this sort of thing! HURRAY! :D



    Here's the little bit of magic code he references, it all makes sense now.

    Code (CSharp):
    1. SaveGameManager.Instance.SaveableObjects.Remove (this);
     
    Last edited: Apr 13, 2016
  33. Wrymnn

    Wrymnn

    Joined:
    Sep 24, 2014
    Posts:
    373
    I am wondering, what are the advantages & disadvantages of this method, to save object & world data to string, and second method to have real serializable classes inside SaveableObject.

    1.
    - Advantage of first method is, in my opinion versioning and control. If you refactor your class, the serializer wont break, since its in string and you control what data it contains.
    - But disadvantage is that you need to manually read all data from string, and assign values to the gameObject upon creation. Same goes for saving that object.

    2.
    - now the second method, does this all manually. So for example, have real variable string prefabName for prefab name, float for remaining health, or serializable Inventory class. This can be directly used by GameObejct during gameplay.
    - now, however, when you refactor your class, change architecture, or make some tiny mistake, you can break all previous saves. This is very dangerous and is what I mostly fear. So you post made me look at some other side.

    What do you think?
     
  34. DearUnityPleaseAddSerializableDictionaries

    DearUnityPleaseAddSerializableDictionaries

    Joined:
    Sep 12, 2014
    Posts:
    135
    Why is it not good to pursue this option? Game Maker currently has a magic save function. Perhaps one could go hybrid? Depending on the game, it can be very tedious to implement a save system.

    Does such a magic function exists or has someone made one yet? I am interested in using it.
     
    Last edited: Oct 31, 2021
  35. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,612
    If Game Maker is still similar to when I last used it, it's very much a small, closed garden environment, making it possible for them to manage all that on their end.

    Unity is quite the opposite - a fully fledge game engine with many aspects of it open for the user to prod at - so it'd be impossible for them provide a system that works for everyone without being monumentally heavy or fragile. This isn't mentioning that Unity exists in both C# and C++.

    Yes it's hard, but it's one of those things that the more you do the easier it gets. Someone on these forums once mentioned that you should be thinking about saving alongside every feature you implement. That titbit has been good advice for me to avoid putting myself in a position where I either can't save, or not without it being greatly difficult.

    I believe there are assets that can do it all for you, but from what I've heard they're both incredibly slow and make dramatically large save files. It's in you best interest as a coder to learn the correct way to do it and add that to your skillset.

    And yes, this is a necro, but I felt like having a ramble there.
     
  36. Well, actually saving isn't hard. It's daunting. And it's because planning a saving system requires two things: critical thinking and thorough knowledge of your game. Obviously the critical thinking is to decide what data you need to save and load in order to preserve the state of the game and thorough knowledge required to know what data you actually have and why you have it. This is where the "copy-paste" code-champions fall on their faces and slide out to the cold.

    Also, it is very hard to write proper generic saving system because of these two things. And this is the reason Unity doesn't write one. It's nearly impossible.
    I know there are assets doing it, but actually, no, they don't. They are serializing some common types. And they call it "saving". You still required to analyze your own game and make decisions what to save, but you don't have a decision how to save it anymore. So they aren't really that much of a help. Obviously having the helper methods to write out the common types is nice, but wouldn't call it saving, so they are mildly useful in general.
     
  37. DearUnityPleaseAddSerializableDictionaries

    DearUnityPleaseAddSerializableDictionaries

    Joined:
    Sep 12, 2014
    Posts:
    135
    Thank you for the discussion points, this has been very useful. I will continue implementing my save system.
     
  38. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,612
    I think most new things are hard for programmers when in their beginner phase. ;p But you aren't incorrect, of course.

    It took me a good week of futzing about to get the core data I needed into a position where it was saving and loading without any bugs. Stuff like the profile's name, difficulty selections, and what zone the player is in/their specific location.

    The serialising and de-serialising was the easy part, honestly. The easily-fallen-into-foibles of Addressables turned out to be the source of the bugs, but it was good to learn those sooner rather than later.

    Then it took a number of days more to be able to save the players inventory (the secret: GUID's). But after that, saving all the inventories in the game was magnitudes easier.

    No doubt when I implement equipment and further mechanics, saving those will be just as easy.

    Long story short is to keep at it. You're better off asking folks here for assistance rather than chasing the impossible and improbable.
     
    Lurking-Ninja likes this.