- Home /
Save Time Button Settings in Player Prefs
I have a timer script where you can chose pre set time duration on the time with button methods like this:
//Timer Settings
public void GoTimerSetting60Sec()
{
timer = 60;
}
public void GoTimerSetting5Min()
{
timer = 300;
}
public void GoTimerSetting10Min()
{
timer = 600;
}
How would I go about saving a selected timer preset to player prefs so that the time would automatically be selected when the app is restarted?
Answer by ShadyProductions · Jan 24, 2016 at 09:48 PM
Something like this?
public void GoTimer(int amount) {
timer = amount;
}
PlayerPrefs.SetInt("timeVar", timer); //save it like this
int a = PlayerPrefs.GetInt("timeVar"); //get it like this
Answer by SterlingSoftworks · Jan 24, 2016 at 09:50 PM
You could set up PlayerPref bools for each one of these button types and then at the beginning of this script (or wherever you're needing it) check each player pref bool, and the one that's true, set the time to the corresponding timer bool you made.
If you go this route, make sure you set the other two to false when you mark one true.
public void GoTimerSetting60Sec(){
timer = 60;
timerBoolSixty = true; //Place holder bool name
timerBoolFiveMin = false; //Place holder bool name
timerBoolTenMin = false; //Place holder bool name
}
And etc etc.
Programming tip: Make this all ONE function, it's more dynamic, is easier to edit and looks better (to me at least)
public void GoTimerSetting(int time){
timer = time;
}
Then in your buttons' OnClick() stuff just put in the amount in the box for the corresponding button.
Thanks @SterlingSoftworks So at the top of my script I would set up the bools for each timer button something like this right?
public bool timerBoolSixty = false; //Timer 60
public bool timerBoolFive$$anonymous$$in = false; //Timer 5 $$anonymous$$
public bool timerBoolTen$$anonymous$$in = false; //Timer 10 $$anonymous$$
I made them public just so I can see that they will change in the inspector. If I have that right on the saving to player prefs side of it I would do what?
What do you need the bools even for, what is your main purpose of this timer?:P
Dude I know! I totally brain farted there! Your way is probably better than $$anonymous$$e! xD
Oh shoot! I apologize so much! Bools are not part of PlayerPrefs! That's a huge mistake on my part!
What you could do is ins$$anonymous$$d of bools you could jut have them be ints and have 0 be your "false" and 1 be your "true". I truly do apologize for the mistake! $$anonymous$$ajor brain fart on my part there.
Saving would go something like:
PlayerPrefs.SetInt("timerBoolSixty", timerBoolSixty); //or 1 ins$$anonymous$$d of the variable
PlayerPrefs.SetInt("timerBoolFive$$anonymous$$in", timerBoolFive$$anonymous$$in); // or 0 ins$$anonymous$$d of the variable
PlayerPrefs.SetInt("timerBoolTen$$anonymous$$in", timerBoolTen$$anonymous$$in); // or 0 ins$$anonymous$$d of the variable
You could always rename them to "int" ins$$anonymous$$d of "bool" too if it helps you not get confused by my stupid answer.
Thanks @SterlingSoftworks, Ok little confused, sorry :) O$$anonymous$$ I can only play one timer at a time so lets say I choose timer button for 60 seconds and that was the last time setting I used so when I restart my app that would be the one I would want to be active.
But just to be sure I would NOT need these bools?
//Timer Button Bools
public bool timerBoolSixty = false; //Timer 60 Sec.
public bool timerBoolFive$$anonymous$$in = false; //Timer 5 $$anonymous$$.
public bool timerBoolTen$$anonymous$$in = false; //Timer 10 $$anonymous$$.
public bool timerBoolTwenty$$anonymous$$in = false; //Timer 20 $$anonymous$$.
public bool timerBoolThirty$$anonymous$$in = false; //Timer 30 $$anonymous$$.
public bool timerBoolFourty$$anonymous$$in = false; //Timer 40 $$anonymous$$.
public bool timerBoolFifty$$anonymous$$in = false; //Timer 50 $$anonymous$$.
public bool timerBool1Hour = false; //Timer 1 Hour.
public bool timerBool1Point5Hour = false; //Timer 1.5 Hour.
public bool timerBool1TwoHours = false; //Timer 2 Hour.
public bool timerBool1TwoPoint5Hours = false; //Timer 2.5 Hour.
public bool timerBool1ThreeHours = false; //Timer 3 Hour.
Well when you click the timer button, you would save the int to playerprefs.
Say I press the button I just do something like this:
public void GoTimer(int amount) {
timer = amount;
PlayerPrefs.SetInt("timeVar", timer); //save the last used amount.
}
GoTimer(60); //call this on button press, it automaticly saves it to playerprefs
//and in your new scene you can get it like so:
int timer = PlayerPrefs.GetInt("timeVar");
//timer will be 60
And no you won't need those bools lol. What you can do to remember the times is put something like this
/*
*60 // 1 $$anonymous$$IN
*300 // 5$$anonymous$$IN
*600 // 10 $$anonymous$$IN
*3600 // 1 HOUR
* ETC...
*/
These are comments to remember yourself. $$anonymous$$akes it easier.
@ShadyProductions Thanks, would I not use something like this to enable my bools?
//Timer Settings
public void GoTimerSetting60Sec()
{
timer = 60;
timerBoolSixty = true;
}
And do something like what @SterlingSoftworks suggested and set all of the others to false and then save that button setting to prefs?
Your answer
Follow this Question
Related Questions
Resetting Timer Using PlayerPrefs? (JS) 1 Answer
Issues with Save / Load system 1 Answer
Playerprefs doesn't save anything. 2 Answers
How do you save timer with PlayerPrefs? 1 Answer
timer not ticking down 2 Answers