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!