# Questions with regards to PlayerPrefs

This topic is 444 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hi all. I am going through some tutorial with regards to PlayerPrefs.

there is 1 part of the script I do not seems to be able to understand.

Hope someone can help me. Thank you!

Okay, correct me if I am wrong about PlayerPrefs. Here's what I understand from the unity documentations

1) Only able to set int, float or string. PlayerPrefs give players access to set preferences.

From Unity Documentations:

public static void SetInt(string key, int value);


I understand the key part. the key part is the string I have set to.

3) What does the "int value" represent? Anything I have set it to right? Okay here's what I do not get.

	public static void UnlockedLevels (int levels)
{
if (levels <= SceneManager.sceneCountInBuildSettings - 1) {
PlayerPrefs.SetInt (Level_Key + levels.ToString(), 1); //use 1 for true
}
else
{
Debug.LogError ("invalid scene outside of build order");
}
}

public static bool IsLevelUnlocked (int levels)
{
int levelValue = PlayerPrefs.GetInt (Level_Key + levels.ToString ());
bool isLevelUnlocked = (levelValue == 1);

if (levels <= SceneManager.sceneCountInBuildSettings - 1) {
return isLevelUnlocked;
}
else
{
Debug.LogError ("Trying to query level outside of build order");
return false;
}
}


First part of the script, unlockedlevel,

PlayerPrefs.SetInt (Level_Key + levels.ToString(), 1); //use 1 for true


Now, what I do not get is, the int value assigned is "1", where 1 being true. Okay. 2nd part of the script, bool.

int levelValue = PlayerPrefs.GetInt (Level_Key + levels.ToString ());
bool isLevelUnlocked = (levelValue == 1);



How does the Getint allows me to return 1 or 2,?

The instructor in the video mention that the code

"PlayerPrefs.SetInt (Level_Key + levels.ToString(), 1); //use 1 for true"

but I want to know why. I am trying to wrap my head around this for a long time. there must be something I am missing!

thank you everyone!

##### Share on other sites

I understand the key part. the key part is the string I have set to. 3) What does the "int value" represent? Anything I have set it to right?

Never programmed any Unity, but yeah, the "int" looks like the value associated with the key, I'd expect something like

int answer = 42; // obtain the value you want to store (normally from your game data instead of a hard-coded number).
PlayerPrefs.SetInt("answer_to_life_universe_everything", answer);  // Stores value 42 with key 'answer_to_life_universe_everything'

Now, what I do not get is, the int value assigned is "1", where 1 being true.

My guess is, "level is unlocked" is either true or false, ie it's a boolean notion. For reasons I don't know, they decided to use the integer value 1 for "true", and presumably 0 for "false". Other integer values are not used, since the other values have no translation to/from the boolean notion "level is unlocked". It is not a limitation in PlayerPrefs.GetInt, it's a limitation in the range of values that you can usefully store for this particular player preference.

int levelValue = PlayerPrefs.GetInt (Level_Key + levels.ToString ());
bool isLevelUnlocked = (levelValue == 1);

How does the Getint allows me to return 1 or 2,?

The above code does 2 things:

- "int levelValue = .." retrieves the 'unlocked level' number you inserted with the 'SetInt call, ie the 1 you inserted with the 'SetInt' above

- "bool isLevelUnlocked = .." converts that integer number back to a "level is unlocked" boolean (ie from 0/1 to false/true).

To just get the number you inserted, don't do the second thing, ie skip the conversion to a boolean, ie something like

int answer = PlayerPrefs.GetInt("answer_to_life_universe_everything");
// Do something with the answer

In general, you do want to check that the returned value from the preferences is sane, people may have edited the storage in some way, and then you may get weird numbers like -83 or 123456789  (very useful if that number is the number of lifes that I have in your game :D )

##### Share on other sites

I understand the key part. the key part is the string I have set to. 3) What does the "int value" represent? Anything I have set it to right?

Never programmed any Unity, but yeah, the "int" looks like the value associated with the key, I'd expect something like

int answer = 42; // obtain the value you want to store (normally from your game data instead of a hard-coded number).
PlayerPrefs.SetInt("answer_to_life_universe_everything", answer);  // Stores value 42 with key 'answer_to_life_universe_everything'

Now, what I do not get is, the int value assigned is "1", where 1 being true.

My guess is, "level is unlocked" is either true or false, ie it's a boolean notion. For reasons I don't know, they decided to use the integer value 1 for "true", and presumably 0 for "false". Other integer values are not used, since the other values have no translation to/from the boolean notion "level is unlocked". It is not a limitation in PlayerPrefs.GetInt, it's a limitation in the range of values that you can usefully store for this particular player preference.

int levelValue = PlayerPrefs.GetInt (Level_Key + levels.ToString ());
bool isLevelUnlocked = (levelValue == 1);

How does the Getint allows me to return 1 or 2,?

The above code does 2 things:

- "int levelValue = .." retrieves the 'unlocked level' number you inserted with the 'SetInt call, ie the 1 you inserted with the 'SetInt' above

- "bool isLevelUnlocked = .." converts that integer number back to a "level is unlocked" boolean (ie from 0/1 to false/true).

To just get the number you inserted, don't do the second thing, ie skip the conversion to a boolean, ie something like

int answer = PlayerPrefs.GetInt("answer_to_life_universe_everything");
// Do something with the answer

In general, you do want to check that the returned value from the preferences is sane, people may have edited the storage in some way, and then you may get weird numbers like -83 or 123456789  (very useful if that number is the number of lifes that I have in your game :D )

Hi. Thank you so much for explaining. I *think* I may have got it after reading what you write but I am still not confident enough. Indeed, what I am trying to do is a boolean operation. However, "PlayerPrefs" can only set int float and string. Meaning a boolean operation is not allowed, so there is a need to do that code.

Do correct me if I am wrong.

the operation

PlayerPrefs.SetInt (Level_Key + levels.ToString(), 1);


returns 2 things:

1) the level_key + the int value to string.  (i.e level_key_01 as a string) that the player has set

2) returns 1 IF it satisfy my "if" statement.

As a result, when I perform a

int levelValue = PlayerPrefs.GetInt (Level_Key + levels.ToString ());


What I will GET is the int value of 1, since my previous operation returns 1.

And then the next if statement states that if my conditions are satisfied, it will return true, otherwise return false, making the whole IsLevelUnlocked() method a bool operations.

Am I getting it right so far?

Sorry, I have never coded before and I want to make sure I understand every line and not just copy and paste.

Thank you!!

##### Share on other sites

PlayerPrefs.SetInt (Level_Key + levels.ToString(), 1);

returns 2 things:
1) the level_key + the int value to string.  (i.e level_key_01 as a string) that the player has set
2) returns 1 IF it satisfy my "if" statement.

It doesn't "return" anything, it just calls the function with what you pass it. In this case the function wants a key and a value for that key, key being a string. in the file it might be KEY1732=1 if your key was "KEY1732." The details don't matter though. The 1 you're setting is the value the key is set to.

As a result, when I perform a

int levelValue = PlayerPrefs.GetInt (Level_Key + levels.ToString ());

What I will GET is the int value of 1, since my previous operation returns 1.

And then the next if statement states that if my conditions are satisfied, it will return true, otherwise return false, making the whole IsLevelUnlocked() method a bool operations.

Am I getting it right so far?

Sorry, I have never coded before and I want to make sure I understand every line and not just copy and paste.

Thank you!!

You have the idea right, yes. GetInt just literally grabs the number that is written in the file and interprets it as an integer, and then returns that integer to you. Then you're setting int levelValue to it. Everything in memory is just a number. The numbers: 10, 37, 4123 can all be interpreted as a boolean just by changing their type to bool. Usually any non-zero number is interpreted as true and 0 is interpreted as false. Edited by Satharis

##### Share on other sites

PlayerPrefs.SetInt (Level_Key + levels.ToString(), 1);

returns 2 things:
1) the level_key + the int value to string.  (i.e level_key_01 as a string) that the player has set
2) returns 1 IF it satisfy my "if" statement.

It doesn't "return" anything, it just calls the function with what you pass it. In this case the function wants a key and a value for that key, key being a string. in the file it might be KEY1732=1 if your key was "KEY1732." The details don't matter though. The 1 you're setting is the value the key is set to.

As a result, when I perform a

int levelValue = PlayerPrefs.GetInt (Level_Key + levels.ToString ());

What I will GET is the int value of 1, since my previous operation returns 1.

And then the next if statement states that if my conditions are satisfied, it will return true, otherwise return false, making the whole IsLevelUnlocked() method a bool operations.

Am I getting it right so far?

Sorry, I have never coded before and I want to make sure I understand every line and not just copy and paste.

Thank you!!

You have the idea right, yes. GetInt just literally grabs the number that is written in the file and interprets it as an integer, and then returns that integer to you. Then you're setting int levelValue to it. Everything in memory is just a number. The numbers: 10, 37, 4123 can all be interpreted as a boolean just by changing their type to bool. Usually any non-zero number is interpreted as true and 0 is interpreted as false.

Got the first part!.

So am I right to say that if I were to set,

PlayerPrefs.SetInt (Level_Key + levels.ToString(), 1);



since that "1" is just a float, I can set any number I want it to be. For example. 999.

So what happens during this

int levelValue = PlayerPrefs.GetInt (Level_Key + levels.ToString ());


Since I set the key to 999,

GetInt will give me 999.

If i dont satisfy the conditions, it will return false.

Am I getting it right so far? Sorry if I got it wrong again. :(

thank you very much.

##### Share on other sites

So am I right to say that if I were to set,

PlayerPrefs.SetInt (Level_Key + levels.ToString(), 1);

since that "1" is just a float, I can set any number I want it to be. For example. 999.

It isn't a float, it's an int, as the name implies. If you look at the Unity documentation it gives this signature: SetInt(string key, int value); Meaning it takes an int. If you give it a float then it will implicitly convert it into an int, so basically it will shave the decimal part off.

So what happens during this

int levelValue = PlayerPrefs.GetInt (Level_Key + levels.ToString ());

Since I set the key to 999,

GetInt will give me 999.

If i dont satisfy the conditions, it will return false.

Am I getting it right so far? Sorry if I got it wrong again. :(

thank you very much.

What happens is it grabs the int based on the key you give it. If we look at the documentation:

public static int GetInt(string key, int defaultValue = 0);

Returns the value corresponding to key in the preference file if it exists.

If it doesn't exist, it will return defaultValue.

You can see that it returns an int, and if it can't find the key you gave it then it will return defaultValue. If you didn't provide a defaultValue then it will return 0. It doesn't return true or false, it just returns a number.

##### Share on other sites

So am I right to say that if I were to set,

PlayerPrefs.SetInt (Level_Key + levels.ToString(), 1);

since that "1" is just a float, I can set any number I want it to be. For example. 999.

It isn't a float, it's an int, as the name implies. If you look at the Unity documentation it gives this signature: SetInt(string key, int value); Meaning it takes an int. If you give it a float then it will implicitly convert it into an int, so basically it will shave the decimal part off.

So what happens during this

int levelValue = PlayerPrefs.GetInt (Level_Key + levels.ToString ());

Since I set the key to 999,

GetInt will give me 999.

If i dont satisfy the conditions, it will return false.

Am I getting it right so far? Sorry if I got it wrong again. :(

thank you very much.

What happens is it grabs the int based on the key you give it. If we look at the documentation:

public static int GetInt(string key, int defaultValue = 0);

Returns the value corresponding to key in the preference file if it exists.

If it doesn't exist, it will return defaultValue.

You can see that it returns an int, and if it can't find the key you gave it then it will return defaultValue. If you didn't provide a defaultValue then it will return 0. It doesn't return true or false, it just returns a number.

Ohhh! Sorry my mistake. Got it about the float and int. I messed it up.Thank you!

I can see the whole picture now.

I understand that it returns either the key I have set, in this case "1". and if there is no key, it's default will be 0. No, it doesn't return true or false.

However, it goes through this process in the IsLevelUnlocked () method.

1) GetInt gets the int that I have assigned in the preferences (from the int I have SetIn)

2) I have assigned this int into another name, levelValue

3) so now it goes true a boolean operations;

bool isLevelUnlocked = (levelValue==1) this means that if my GetInt was "1", the bool isLevelUnlocked is true.

4) Goes through the conditional, and as a result it will return either a true or a false.

Am I getting the process right so far?

thank you very much!

Edited by Zesi

##### Share on other sites

Sounds like it, do you try these things in a program too?

I often do quick experiments to test whether things work in the way I think it does.

##### Share on other sites

Sounds like it, do you try these things in a program too?

I often do quick experiments to test whether things work in the way I think it does.

Yup! I use debug.log and debug.logerror to check if I am right. Most of the time I am confident when it's straight forward.

This is one of those times when I have no confident whether I understand or not.

I keep doubting myself: "Do I fully understand the process or am I just copying whatever that was written"

:(

##### Share on other sites

That happens. I agree it's always good to truly understand what happens.

Things do get easier when you get more experience for cases like this.

##### Share on other sites

3) so now it goes true a boolean operations;
bool isLevelUnlocked = (levelValue==1) this means that if my GetInt was "1", the bool isLevelUnlocked is true.

Correct, at the basic level the code is going: "I have this variable, isLevelUnlocked, which is a bool. I need to take whatever is on the other side of the equal sign and convert it into a bool." In this case (levelValue==1) is a boolean expression. It literally checks if levelValue is 1 and returns a true or false. Since it returns a boolean already, we can just stick it into isLevelUnlocked without any conversion.

On the other hand you could also take the number and explicit convert it into a bool.
bool isLevelUnlocked = (bool)levelValue;

In this case we are explicitly casting levelValue into a bool. Think of a bool as just a number. If you cast like this then all it is doing is converting the type of it. So if you had levelValue set to 1, 3, or even 700, they are all non-zero values. When it's just sitting in the bool that doesn't mean much, however when you place the value into a boolean expression, then it suddenly means something.
if(levelValue)
Will equal true if it is any number besides 0(at least that's the case in most languages.) On the other hand if you do an expression like:
bool isLevelUnlocked = (levelValue==1)
you are not just interpreting a number as a bool, you are evaluating an expression that returns a bool. Usually expressions that return bools will return a 1 for true and a 0 for false.

In most case it is considered bad form to convert numbers straight into bools, your method of using an expression to figure it out is actually probably a good practice. But I thought I'd explain how the behavior works in case you ever get confused from a conversion giving an answer you might not expect it to. Implicit conversions(automatic conversions) can often do confusing things. Edited by Satharis

##### Share on other sites

thank you so much Satharis! I really appreciate your help!

Finally got to figure this out! :)

• 9
• 11
• 21
• 10
• 14