- Home /
Saving Data! PLEASE HELP
Im making an achivements type thing in my game where once you get a certain nnumber of points you complete a challenge and the game saves it. But its not working. Im new t Player.Prefs and what not.
var challenge = 0;
var challenge2 = 0;
var challenge3 = 0;
var challenge4 = 0;
var challenge5 = 0;
function Start () {
OnSave();
}
function Update () {
if(CollectGem.score >= 10) //If score is 10 or more Challenge 1 is COMPLETE
{
challenge = 1;
OnSave();
Debug.Log("CH1 IS DONE");
}
}
var help = false;
var credits = false;
var challenges = false;
//var shop = false;
function OnGUI()
{
//GUI.Label(Rect(720,20,300,200),"HINT: RED = The path of the shuriken");
//HELP DISPLAY
if(help == true)
{
GUI.Box(Rect(400,210,300,200),"HELP");
GUI.Label(Rect(420,250,300,200),"GOAL: Collect Gems & Dodge Shuriken");
GUI.Label(Rect(420,280,300,200),"Gems = +10 points");
GUI.Label(Rect(420,310,300,200),"Press Space Bar to use power-up");
GUI.Label(Rect(420,340,300,200),"Hearts give you 1 EXTRA LIFE (MAX)");
GUI.Label(Rect(420,370,300,200),"Hearts & Power-Ups spawn every 20 secs");
if(GUI.Button(Rect(650,210,50,25),"Close"))
{
help = false;
}
}
//CREDITS DISPLAY
if(credits == true)
{
GUI.Box(Rect(400,210,300,200),"CREDITS");
GUI.Label(Rect(420,250,300,200),"Programmer: Emek Umoh");
GUI.Label(Rect(420,280,300,200),"Graphics Artist: Emek Umoh");
GUI.Label(Rect(420,310,300,200),"Music: Ryan Little AKA TheR4C2010");
if(GUI.Button(Rect(650,210,50,25),"Close"))
{
credits = false;
}
}
//CHALLENGES DISPLAY
if(challenges == true)
{
GUI.Box(Rect(400,210,300,200),"CHALLENGES");
GUI.Label(Rect(420,250,300,200),"Earn 10 points"); //challenge 1
GUI.Label(Rect(420,280,300,200),"Earn 50 points");
GUI.Label(Rect(420,310,300,200),"Earn 100 points");
GUI.Label(Rect(420,340,300,200),"Earn 200 points");
GUI.Label(Rect(420,370,300,200),"Earn 400 points");
GUI.Label(Rect(420,400,300,200),"Get a Shuriken");
if(challenge == 1)
{
GUI.Label(Rect(500,250,300,200),"[COMPLETE]");
Debug.Log("C1 COMPLETE");
}
if(GUI.Button(Rect(650,210,50,25),"Close"))
{
challenges = false;
}
}
if(GUI.Button(Rect(210,150,100,50),"PLAY"))
{
Death.spawnWeapon = true;
Application.LoadLevel("Scene1");
credits = false;
help = false;
challenges = false;
}
if(GUI.Button(Rect(210,210,100,50),"HELP"))
{
help = true;
credits = false;
challenges = false;
}
if(GUI.Button(Rect(210,270,100,50),"CREDITS"))
{
credits = true;
help = false;
challenges = false;
}
if(GUI.Button(Rect(210,330,100,50),"CHALLENGES"))
{
credits = false;
help = false;
challenges = true;
}
}
function OnSave()
{
PlayerPrefs.SetInt("Challenge",challenge);
PlayerPrefs.Save();
challenge = PlayerPrefs.GetInt("Challenge");
print(PlayerPrefs.GetInt("Challenge"));
}
Did my answer not help you? It's bad form to ask a question and then abandon it.
Answer by iwaldrop · Dec 24, 2013 at 05:56 AM
Your problem is probably due to the fact that you call OnSave in Start with a challenge value equal to 0. That will save 0 to your key which effectively resets it's value each time you start your game.
Also, you have no clear way of determining which challenges were completed since you will be saving all of your challenge info into "Challenge". Probably the simplest approach to keep all of your values in one key is a bitmask. It will allow you to store many flags in one integer.
The following is code that you can drop onto a GameObject and play around with. It's designed so that you can select which challenge to 'complete' by using the enum drop-down in the inspector and then enabling the script. Additionally, you can reset the script (via the gear icon in the upper-right-hand side of the script on the game object) and start over. It's not designed to be used in production, so you'll have to glean what's relevant and refactor your code base to use the parts that you like.
#pragma strict
private var CHALLENGE_MASK_KEY : String = "CHALLENGE_MASK";
enum Challenge
{
ScoreTenPoints = 1,
BeatUpTwentyBaddies = 2,
DeflowerTheMaidens = 4,
}
var whichChallenge : Challenge = Challenge.ScoreTenPoints;
var challengeFlags : int = 0;
function Awake()
{
LoadChallengeScore();
enabled = false;
}
function Reset()
{
PlayerPrefs.SetInt(CHALLENGE_MASK_KEY, 0);
}
function OnEnable()
{
CompleteChallenge(whichChallenge);
enabled = false;
}
function CompleteChallenge(challenge : Challenge)
{
var challengeBit : int = challenge;
challengeFlags |= challengeBit;
PlayerPrefs.SetInt(CHALLENGE_SCORE_KEY, challengeFlags);
print (String.Format("setting flags = {0}", challengeFlags));
}
function LoadChallengeScore()
{
challengeFlags = PlayerPrefs.GetInt(CHALLENGE_MASK_KEY, 1);
print(challengeFlags);
}
There are, of course, many ways to solve the problem, but this seems to mate closely with your existing actions.
Your answer
Follow this Question
Related Questions
PlayerPrefs.DeleteAll() not deleting unless app restarts 0 Answers
PlayerPrefs not saving after i quiet application 1 Answer
saving highscores 1 Answer
Adding Value to Already Saved Values 1 Answer