- Home /
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.
Why not use a free plugin for an easier way? http://whydoidoit.com/unityserializer/
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?
Well my friend, for that, back up, back up, back up. And back up.
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." :(
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.
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 ---
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.
Your answer
Follow this Question
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