Advertisement

Questions with regards to PlayerPrefs

Started by December 04, 2016 02:19 PM
10 comments, last by Zesi 8 years ago

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.

2) Specifically I am asking about PlayerPrefs.SetInt and GetInt.

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!

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 )

Advertisement

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!!

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.


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.

Hi. Thank you for your reply!

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.

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.
Advertisement

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!

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.

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"

:(

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.

This topic is closed to new replies.

Advertisement