[UNITY 5] How to save a scene state? c#

Discussion in 'Game Development (Technical)' started by PixelzGames, Oct 7, 2016.

  1. PixelzGames

    PixelzGames New Member

    Joined:
    Oct 7, 2016
    Messages:
    4
    Likes Received:
    0
    Title: Cefore
    Screenshots/Video:
    Game URL: www.playcefore.com

    Hey guys, so I'm currently working on a game called Cefore. I need to code a system where, when the player will hit the reset button to try the level again (if he fails) then all the bombs and tools he placed would keep their position and settings (as seen in the GIF above). How would you guys go about it?

    Thanks
     
  2. Taka

    Taka New Member

    Joined:
    Oct 9, 2016
    Messages:
    12
    Likes Received:
    2
    save a state of the current variables. Make an array which holds the key to recreating such a scene.
     
  3. PixelzGames

    PixelzGames New Member

    Joined:
    Oct 7, 2016
    Messages:
    4
    Likes Received:
    0
    I considered that but an array was not enough to save all the variables I needed unfortunately.
     
  4. Scoper

    Indie Author

    Joined:
    Nov 5, 2015
    Messages:
    90
    Likes Received:
    21
    You could add methods to your scripts to let them serialize and deserialize.
    Serializing a script creates a string from all its properties. Deserializing does the opposite - sets the values of all properties from a string.
    You probably have multiple GameObjects in a tree. So you'll want to serialize all of them - probably into one string that you can save in memory or to a file (xml for example).
    So when the player clicks Save, you call your serialize method on a script on the root GameObject. This then converts all its properties to a string. It then adds the serialized strings of all its children by calling their serialize method (you may need an interface or joint superclass). The children call their children etc.
    When the player clicks Reset, you take that string representing your tree of game objects, instantiate a prefab, and send that string into its deserialize method. That script then breaks up the string, sets its own values, instantiates its children and gives each of them their deserialization string.
    You now have your tree of game objects converted to a string and back again.

    Will that work for you?
     

Share This Page

  • About Indie Gamer

    When the original Dexterity Forums closed in 2004, Indie Gamer was born and a diverse community has grown out of a passion for creating great games. Here you will find over 10 years of in-depth discussion on game design, the business of game development, and marketing/sales. Indie Gamer also provides a friendly place to meet up with other Developers, Artists, Composers and Writers.
  • Buy us a beer!

    Indie Gamer is delicately held together by a single poor bastard who thankfully gets help from various community volunteers. If you frequent this site or have found value in something you've learned here, help keep the site running by donating a few dollars (for beer of course)!

    Sure, I'll Buy You a Beer