Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Tryz · Jun 05, 2014 at 08:01 PM · savescene-loadingsaveloadsave scenegame save

Save Data Strategies

I've seen several threads about saving/loading game data, but I'm still feeling like I'm missing something. I know how to save information to files, but I'm a stuck on the general approach to use.

We're making a large game and I expect we'll need several scenes. Let's say one per dungeon level. What approach should I take so players can go up and down the levels and things stay the way they left them? If I killed a goblin, I want the goblin laying in that position when they come back. If a chest was broken into, I want it to still be broken and open.

I understand how to save data to a file, but there are some odd issues:

  1. GameObject IDs change when a scene is loaded. So I can't just save a transform and associate it with an ID.

  2. What about objects that have bones or sub-objects. How should I save the goblin so his rag doll bones are laying in the exact same position?

  3. Objects may now exist when they didn't in the original scene. Maybe there's an arrow I shot and left on the ground...

  4. Objects in the original scene may be totally gone.

  5. How about objects that are unique, but in all the scenes? Assume there is a specific bad guy who is following you around. You may have hurt him in scene #1 and I want him to still be hurt in scene #2 (if he was).

Item 1 Seems like the major issue to me. Since there's no reliable and consistent GUID, I can't reliably store the information in a file or database and map it back to the GameObject in the scene.

Any advice on creating and saving the progress of a larger game more complicated game would be appreciated.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by christoph_r · Jun 06, 2014 at 12:27 AM

Have you watched and understood this video?

  1. GameObjects are serializable, see link text.

  2. If you have a prefab for a dead/unfunctional goblin it's as easy as storing the rotation and position of each limb. If you're not already storing that dead goblin as a serialized GameObject, that is.

  3. See above, store the rotation and position as well as the type of GameObject (how you do that is up to you and your needs, you can make a list of Prefabs and store the index, you could serialise the actual GameObject etc. etc.).

  4. That one is a bit more tricky, but you should be able to reference objets via their transforms and destroy them when you load the level, based on your save-data.

  5. Have you looked at DontDestroyOnLoad?

Anyway, persistent data can be a pretty tricky and daunting subject which might require some careful planning ahead. It's generally a good idea to break it down into smaller parts to get familiar with the concept. You could think of very low level information (say an int and/or a string) you want to store per level, implement that and then move on. Another good place to start or something to try after that would be keeping your "hunter" enemy persistent with DontDestroyOnLoad, as saving to files and using DontDestroyOnLoad are more powerful when they complement each other.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tryz · Jun 06, 2014 at 02:03 AM 0
Share

Thanks Chris,

I did watch the video and I get how to save each piece individually. I guess I was looking for how massive amounts of data gets stored and accessed.

In engines I've created myself, I'd have data structures (similar to database tables) that would hold all the information. A "game object" would have a reliable ID that can be used in the tables to hold hit-points, damage, etc. Then loading or saving scenes just looks into the data structures using that ID.

This also allowed me to do global changes in one spot. Think database query to update all swords at once.

In Unity, everything is stored on the object itself and that object's ID isn't reliable. Since it's not reliable, I can't tie data structures back to a specific object.

1: The doc says it can be serialized, but I get this error: UnityEngine.GameObject is not marked as Serializable.

 GameObject lCube = GameObject.Find("Cube");
 BinaryFormatter bf = new BinaryFormatter();
 FileStream file = File.Create(Application.persistentDataPath + "/save.dat");
 bf.Serialize(file, lCube);
 file.Close();

In some posts, people are saying to serialize each component individually. In that case, I still need to tie the data back to as specific GameObject.

3: I suppose this is easy enough using reflection.

5: I have, but I keep thinking more in terms of database tables...maybe that's my problem. If I knew the boss's unique ID is 123, I'd look it up in the table and see what his hit points should be. Without a reliable ID, I can't do that.

So, I think my question is all co$$anonymous$$g down to: How do you associate external data to GameObjects across play sessions when the GameObjects GetInstanceID() isn't consistant?

$$anonymous$$aybe I just need to create a "Unique ID" component for every object I care to persist...

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to save user levels? 0 Answers

how to save the progress on scene1 when it goes to scene2, so when it goes back to scene1 its still the same saved scene 2 Answers

How to save my settings 1 Answer

Saving some aspects of game (save strategy) 1 Answer

How i can save persistent data for each scene? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges