Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Aug 20, 2017 at 03:20 PM by maymo for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by maymo · Aug 20, 2017 at 12:33 PM · multiplesave datalevels

(SOLVED!) Saving/loading data for EACH LEVEL with MULTIPLE VARIABLES

Hi guys! A basic question about saving/loading data for different levels, that I can not find anywhere!

I basically want to save the results (multiple variables such as the "score", "time", etc.) of EACH level in a file.

For that, I used a class whit all that variables inside and then create a new copy of that class, naming them with the different level numbers. Is there any other way where I can access my data without writing 1000 lines for each variable and each level? Here's a short example of what I am intending:

 public class playerScore 
 { 
 public int score;
 public float time;
 public bool levelUnlocked;
 }
 
 //Save Method
 void Save(){
 if (levelNum == 1){
 playerScore scoreLev1 = new playerScore ();
 bf.Serialize (file, scoreLev1);
 }
 if (levelNum == 2){
 playerScore scoreLev2 = new playerScore ();
 bf.Serialize (file, scoreLev2);
 }
 //if (levelNum == 3)
 //etc...
 }
 
 //Load Method
 void Load(){
 if (levelNum == 1){
 score = scoreLev1.score;
 time = scoreLev1.time;
 levelUnlocked = scoreLev1.levelUnlocked;
 }
 if (levelNum == 2){
 //etc...
 }


Can someone tell me how to do it following the example above? Or is there any better way? Thank you so much in advance :)

SOLUTION: answers.unity3d.com/comments/1396275/view.html

Comment
Add comment · Show 1
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 Cherno · Aug 20, 2017 at 01:37 PM 0
Share

Have a custom class that holds the data for each level:

  [System.Serializalbe]//attribute for viewing and editing it in the inspector
 public class LevelData {
      public int score;
      public float time;
 }

Have an array or list or other type of collection with the class type as the element type and then you can access each level data by simply using the level number as the array index.

 public LevelData[] levelData_array = new LevelData[5];//holds data for 5 levels
 
 //create a new LevelData instance at position 2 of the array
 levelData_array[2] = new LevelData() {
      score = 34;
      time = 186.45f;
 };
 
 //print the time of level 2 to the cconsole
 Debug.Log(levelData_array[2].time);

  //change the score value of level 2
  levelData_array[2].score = 0;
 
 

Remember that arrays start at index 0.

Since the LevelData has only basic variable Types like ints and floats, there should be no problems serializing single LevelData instances or even the whole levelData_array array.

2 Replies

  • Sort: 
avatar image
2
Best Answer

Answer by fafase · Aug 20, 2017 at 01:54 PM

Instead of saving each level, save them as a list.

Get the file, open it, deserialize and you now have a list of playerScore. Then append the new score (or update the existing ones).

Serialize again and done.

Now what does that simplify? First off, the length of your list tells right away whether a level is unlocked. If you have a list of 20 items, then it means level 21 has not been saved yet. Make sure to keep them in order so you can retrieve any level with the list index for update.

 void AddNewItemToList(parameter list){
         BinaryFormatter bf = new BinaryFormatter();
         MemoryStream ms = new MemoryStream(Convert.FromBase64String(str));
         List<PlayerScore> ps =  bf.Deserialize(ms) as List<PlayerScore>;
         ps.Add(new PlayerScore(parameter list));
         ms = new MemoryStream();
         bf.Serialize(ms, ps as List<PlayerScore>);
         SaveDocumentOnDrive();
 }
 List<PlayerScore> GetPlayerScore(){
         BinaryFormatter bf = new BinaryFormatter();
         MemoryStream ms = new MemoryStream(Convert.FromBase64String(str));
         return bf.Deserialize(ms) as List<PlayerScore>;
 }


Comment
Add comment · 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
0

Answer by maymo · Aug 20, 2017 at 03:15 PM

Thank you so much guys for your quick, perfectly explained and clear answers! That's exacly what I needed it. I'll use an array to do it, as I can understand it better than a List.

But thank you both @Cherno and @fafase, you are a star! :)

Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

68 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 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 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 avatar image avatar image

Related Questions

One scene, many levels?? 2 Answers

Save quantities 1 Answer

Need a clue. The best way to save level data without duplication 0 Answers

Multilevel shooter? 2 Answers

Attaching multiple objects to one object with script. 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