- Home /
PlayerPrefs logic and bool?
Hello! This is yet another PlayerPrefs related question, but I'm not here to ask you to write any code for me, just want to ask you guys to clarify the thinking process behind this function a bit since the Script Reference is pathetic.
I have a script with a ton of booleans that unlock levels in my game. I want to save these booleans when the player either OnApplicationPause's the game or on certain levels of the game.
An example would be:
static var tutorialUnlocked = true;
How would I go about saving that boolean? http://wiki.unity3d.com/index.php?title=BoolPrefs , I don't mind using this script, but since I don't even understand the logic behind PlayerPrefs I don't really know where to even begin.
Currently I have a script that has a function which unlocks levels in the game. The function gets called at the end of a short cutscene-ish animation at the end of each level, for example:
if(Application.loadedLevel == 12 && lifeManager.life > 2 && ProgressScript.caveUnlocked == false){
ProgressScript.caveUnlocked = true;
Now I wouldn't mind adding a PlayerPrefs in that function, I just don't understand the logic. Would I instead of saying "ProgressScript.caveUnlocked = true;" just say "PlayerPrefsX.SetBool(ProgressScript.caveUnlocked = true);"? (granted I'd be using the above mentioned work around) So instead of saving the state of a script, I am actually setting the boolean using the PlayerPrefs? Testing it, it doesn't work..
And then of course, I would like to load those prefs at the start of the game again (I have a blank scene at 0 for this - main menu is at 1), assuming I'd understand the logic behind saving, loading shouldn't be too far away.
If anyone could at least shed some insight in to how I'd go about saving the state of a script/gameobject's booleans I'd be able to sleep tonight, as that is the final piece of my little iPhone puzzle atm.
Thank you very much!
PS. I can't believe how uninformative the script reference is about this
Answer by Lukas H · Feb 05, 2013 at 08:32 AM
PlayerPrefs are not meant to be variables to hold your current game state, instead it is a very easy way to persist data between game starts.
Suppose we can unlock levels, I would create a LevelManager script which keeps track of all the level states. It would load the level states on start up and save the level state everytime I unlock a level. The LevelManager would look something like below (C#)
using UnityEngine;
using System.Collections;
public class LevelManager : MonoBehaviour
{
private bool level1Unlocked = false;
// Use this for initialization
void Start()
{
int state = PlayerPrefs.GetInt("Level1State", 0); //0 is default value, when "Level1State" has never been saved before
if (state == 1)
level1Unlocked = true;
else
level1Unlocked = false;
}
// Update is called once per frame
void Update()
{
}
public bool IsLevel1Unlocked()
{
return level1Unlocked;
}
public void UnlockLevel1()
{
level1Unlocked = true;
PlayerPrefs.SetInt("Level1State", 1);
}
}
Thank you for the quick reply! It being in C# is of course confusing me a bit, but I've tried to rework your script to JS.
I have a ProgressScript.js which will function as my level manager, so what I did is ins$$anonymous$$d of having all the levels lined up as static bools, I tried:
var foreststate = PlayerPrefs.GetInt("foreststate", 0);
if (foreststate == 1)
forestUnlocked = true;
else
forestUnlocked = false;
}
I put that in a function Start(); function, and I assume that I would then PlayerPrefs.SetInt("foresstate", 1); in whatever scripts I use for unlocking my levels ins$$anonymous$$d of just calling "forestUnlocked = true".¨ Unity has a problem with:
var foreststate = PlayerPrefs.GetInt("foreststate", 0);
though, the ever occuring ";" error..
I will try to hack around a bit more.. I guess I have a bit more understanding of this, not to the extent where I can complete the save feature though :( hack hack
I think I get a bit of the way you're supposed to think with this. I've set up PlayerPrefs.GetInt("Level1State", 0); like you mentioned, and then an update function which checks the integer to see if it's 1 or 0.
I then set the int to 1 when unlocking a level.
It doesn't follow through to the main menu though, I assume since it isn't a static variable any more? I thought the whole PlayerPrefs saved this data?
If I GetInt("levelname", 0); every time, will it 0-out the int every time? why do I want to Get 0 if I've set it to 1? sounds like a non-static variable to me?
ok I guess I understand the way I should work using integers to set a boolean to true or false, easy enough. however, unity is giving me lip about one thing now.
I have one script that unlocks the levels:
var life$$anonymous$$anager : life$$anonymous$$anager;
var ProgressScript : ProgressScript;
function Update () {
if(Application.loadedLevel == 12 && life$$anonymous$$anager.life > 2){
ProgressScript.PlayerPrefs.SetInt("cavestate", 1);
}
and one script that stores the progress/unlocked levels:
function Start(){
cavestate = PlayerPrefs.GetInt("cavestate");
}
public var cavestate = 0;
public var caveUnlocked = false;
function Update(){
if (cavestate == 1){
caveUnlocked = true;
}
else
caveUnlocked = false;
}
except for the redundant use of variables, this works. IF I use:
ProgressScript.forestUnlocked = true;
which is the way I've done it up until now, and it works. if I use the SetInt, which I want to use to actually SAVE progress, I get this error in the console:
NullReferenceException: Object reference not set to an instance of an object
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cache$$anonymous$$eyName, System.Type[] cache$$anonymous$$eyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cache$$anonymous$$eyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args)
UnityScript.Lang.UnityRuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args, System.Type scriptBaseType)
ProgressScore.Update () (at Assets/Scripts/Points/ProgressScore.js:18)
which of course is silly, because the script works totally fine, except for when I want to use SetInt rather than just turning a boolean into True. the Null Esxception is also bull because I've set the object that has the attatched script to it in the inspector, and calling Debug.Log(life$$anonymous$$anager");
tells me that there is an object correctly assigned in the inspector:
healthbar (life$$anonymous$$anager)
UnityEngine.Debug:Log(Object)
ProgressScore:Update() (at Assets/Scripts/Points/ProgressScore.js:18)
so the script doesn't set any integers because it doesn't go through with the if-statement, because it can't find the script..that it can find.
ProgressScript.PlayerPrefs.SetInt("cavestate", 1);
Removing the ProgressScript part worked..
The previous help was not the correct answer, but it helped me along the way to figuring it out, thank you very much!
Glad it helped. Sorry I didn't saw your follow-up questions earlier!
Your answer
Follow this Question
Related Questions
Saving and load from player prefs in Unity3D on mobile devices?? 0 Answers
Problems with Playerprefs script 1 Answer
Saving Scenes and loading GameObjects? 4 Answers
How to efficiently hand out rewards based on the level that the player completed? 3 Answers
Saving & Loading the scene (or at least one array) via Javascript 1 Answer