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 Conect11 · Sep 23, 2013 at 05:02 PM · savexml

Loading / saving ALL my data

Ok, editing this to make it much more concise. Here are the basics:

I'm using a save / load script which works, except I need to plug in more variables. In other words, the script is designed from the go to save player position, but nothing else. So I'm trying to call stats from a couple of different scripts for saving (such as current health, experience, etc.) with no success. Below is a small chunk of the script, the area I've identified as being pertinent to this question. (I realize that with my lack of experience I may be completely wrong)

 //LoadingPlayers ************************************************************      
     if (GUI.Button(_Load,"Load")) {
       
       GUI.Label(_LoadMSG,"Loading from: "+_FileLocation);
       // Load our UserData into myData
       LoadXML();
       if(_data.ToString() != "")
       {
          myData = DeserializeObject(_data);
          VPosition=new Vector3(myData._iUser.x,myData._iUser.y,myData._iUser.z);             
          player.transform.position=VPosition;
          curExperience = Playerhealth.curXp;
          Debug.Log("Load Successful");
       }
             
    }
    //EndOfLoadingPlayers *******************************************************
    
    //SavingPlayers *************************************************************
    if (GUI.Button(_Save,"Save")) {
             
       GUI.Label(_SaveMSG,"Saving to: "+_FileLocation);
       //Debug.Log("SaveLoadXML: sanity check:"+ player.transform.position.x);
       
       myData._iUser.x = player.transform.position.x;
       myData._iUser.y = player.transform.position.y;
       myData._iUser.z = player.transform.position.z;
       myData._iUser.name = playerName;
       myData._iUser.curExperience = Playerhealth.curXp; 
       myData._iUser.curExperience = Playerhealth.curHealth;

Look directly above. The last two lines are what I modified to try and pull from those two scripts, whereas everything preceding those are original, before I modified anything. AND... nothing changes. Player position loads fine, but nothing I tried to save. There is much more to the script, and maybe a variable needs to be saved somewhere, I don't know. I can place the entire script in a couple of comments below, if necessary. Thanks to anyone who is able to help me with this problem, which has me pulling my hair out. God bless.

Comment
Add comment · Show 8
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 Tanshaydar · Sep 23, 2013 at 09:02 PM 0
Share

Why not use a free plugin for an easier way? http://whydoidoit.com/unityserializer/

avatar image Conect11 · Sep 23, 2013 at 09:04 PM 0
Share

tried using the serializer the first few days of learning Unity, and it made my head spin, lol. Now that I've grasped a few more concepts it might make more sense, but have you ever gone so far with a project that you're worried about doing more harm than good by changing things up at that point?

avatar image Tanshaydar · Sep 23, 2013 at 09:20 PM 1
Share

Well my friend, for that, back up, back up, back up. And back up.

avatar image Conect11 · Sep 23, 2013 at 09:22 PM 0
Share

wise words. BTW, retried unityserializer, and while I understood it much better, I received a host of errors. Going to the support section of whydoidoit.com revealed that the maker ($$anonymous$$ike) has been mia for a while because he's had to focus on a project. The consensus there was "if you're working on a deadline you're best going somewhere else." :(

avatar image Tanshaydar · Sep 25, 2013 at 07:21 PM 0
Share

Since I don't know what your poject includes, I don't really have an idea what might be causing these issues. But it's probably because you are using non-serializable objects/references.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by infinitypbr · Sep 25, 2013 at 09:52 PM

If I could help you with PlayerPrefs then:

 var playerLevel : int = 14;
 var playerMoney : float = 2391.50;
 var killedBosses : int = 24;
 var otherVariableEtc : String = "other Variable Can Be String";

 var playerDataString = "" + playerLevel + "," + playerMoney + "," + killedBosses + "," + otherVariableEtc + "";
 PlayerPrefs.SetString("PlayerData", playerDataString);

The variable playerDataString which you can use print (playerDataString); on the next line to double check, would look something like this: 14,2391.50,24,other Variable Can Be String

The entire string is saved as one playerPref -- but can include floats, strings, ints or booleans. The 2nd line saves the playerPref as a string (SetString), called "PlayerData", with playerDataString as the content.

Getting the data back isn't much more complicated:

 var playerDataCombined = PlayerPrefs.GetString("PlayerData");     // GetString saved in PlayerPrefs called "PlayerData"
 var playerData = playerDataCombined.Split(","[0]);      // .Split will create an array (called playerData).  Each comma is removed, and what's between is a unique entry into the array.

 playerLevel = parseInt(playerData[0]);
 playerMoney = parseFloat(playerData[1]);
 killedBosses = parseInt(playerData[2]);
 otherVariableEtc = playerData[3]);

That last four lines puts the data from your PlayerData array into your original variables -- so you'd do that on Start(). The data in the array are all strings, so for floats and ints, you need to use the parseInt or parseFloat to change the type from a string to a float or int.

Hopefully that makes sense!

EDIT: For your items, do the same thing, but make a unique playerPref for items, perhaps called "PlayerItems". When saving, do a for loop to include all of the item names or IDs in the string you will be saving (the one with commas).

On start, do this same retrieval method but make it specific to your items, since your items may be stored differently.

--- The same thing can be done with enemies and item pickups, including instantiating enemies at the proper spots with the proper HPs and such, if you think about it a bit more ---

Comment
Add comment · Show 2 · 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 Conect11 · Oct 17, 2013 at 01:24 AM 0
Share

thanks so much! I was avoiding PlayerPrefs for the longest, (even though everyone recommended them) because I had the hardest time understanding them. Something "clicked" the other night, and now I see why they come so highly recommended.

avatar image infinitypbr · Oct 17, 2013 at 06:49 PM 1
Share

Awesome! Love the click moments!

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

16 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

Related Questions

Editing an XML File 1 Answer

XML Encryption 1 Answer

Saving Game Problem 1 Answer

PlayerPrefs for Mute/Unmute button 1 Answer

Questions about XML save/load 0 Answers


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