- Home /
Save & Loading Variables
Hello,
I want to have the ability to save my game variables when I leave the game. I'm not worried about positions of objects or anything like that. I literally want to grab all the variables I have in my scripts, and save them as an XML or something so next time, when I open up my game all my variables will be replaced with the saved ones.
I've tried messing around with a Save Load script, but I just can't get it to work. So like I said, is there a way to pull all my current game variables, and save them - and then load them back up?
Would appreciate any help or info.
Thanks!
-- EDIT --
So, Ive been messing around with the PlayerPrefs, and im not 100% sure on how it works. This is how I have my setup.
-- EDIT Update --
So i have this to save the current variables (well 1 at this current time):
if (GUI.Button(Rect(10,70,50,30),"Save")){
PlayerPrefsX.SetBool("inventoryKnife", StoryControl.inventoryKnife);
}
On my landing scene (0) - how would I go about loading the variables up into the game?
function Start(){
Application.LoadLevel(PlayerPrefs.GetInt("inventoryKnife")); //??
//or
Application.LoadLevel(PlayerPrefsX.GetBool ... //?
}
I have no idea how PlayerPrefs work.
Another thing I need to mention is that the variables I want to save are all booleans ... and I have hundreds of them!
Answer by Statement · Mar 16, 2011 at 11:34 PM
Are you sure you aren't supposed to call SetInt? I am under the impression you want to set the saved level on Save. And you can provide a default value to GetInt if it doesn't exist so you don't have to check if the key exists. It will also get rid of your static variable.
function Start()
{
// If no savedLevel was found it will return 1 as default.
Application.LoadLevel(PlayerPrefs.GetInt("savedLevel", 1));
}
And if you want to set the savedLevel to something, for instance set it to level 2:
if (GUI.Button(Rect(10,70,50,30), "Save"))
{
PlayerPrefs.SetInt("savedLevel", 2);
}
PlayerPrefs is kind of easy. You can think of it as having static variables that are saved between games. There is a Get and a Set for Int, Float and String. For ease, GetInt etc accept an optional parameter that is the default value if no key was found.
See also BoolPrefs and ArrayPrefs.
Given you have a script you want to save/load savegame data to/from:
// The data we want to save. var inventoryKnife : boolean;
function OnEnable() { Load(); }
function OnDisable() { Save(); }
function Load() { inventoryKnife = PlayerPrefsX.GetBool("inventoryKnife", false); }
function Save() { PlayerPrefsX.SetBool("inventoryKnife", inventoryKnife); }
Okay - so what about booleans? Example: my inventory, I have items that are laid out like so: inventory$$anonymous$$nife = false || inventoryPaper = true ... how can I save those values
I updated my answer with links to scripts on the wiki. If you want to you can encode the booleans as bitflags in an integer. That way you can use 1 int for 32 booleans ins$$anonymous$$d of using 1 int for 1 boolean as BoolPrefs do.
Thanks - please read my updated question, im struggling to load
It seems you're confused that you need to pass anything you get from PlayerPrefs to LoadLevel. This is not the case. We call upon LoadLevel with the value of the level in the first example. We don't want to pass the value of the inventory$$anonymous$$nife to LoadLevel, it doesn't make any sense at all. PlayerPrefs is just storing data that you can get or set. Nothing more than that. See my updated answer. Application.LoadLevel(PlayerPrefs.GetInt("inventory$$anonymous$$nife")); doesn't make any sense at all, it would read "load the level + do we have a knife?". Dont use LoadLevel on inventory$$anonymous$$nife :)
If you're still having problems with player prefs - I spent the entire night yesterday writing a simpler set of tools you might like. But I am in a hurry to go on a small holiday so I will have to post back to you earliest monday.
Answer by raul corrales · Mar 16, 2011 at 10:25 PM
hi, you can use PLAYERPREFS.
Documentation/Documentation/ScriptReference/PlayerPrefs.html
Okay,thanks - could you read my updated question. Thanks!