PlayerPrefs boolean value

It’s noted in the video that using 1 for level unlocked being true is awkward, but we don’t have boolean values. PlayerPrefsManager is a wrapper we’re inventing, so we can add in whatever we’d like there. Naively, as we’re using string constants for all the keys, it would be trivial to do:

PlayerPrefs.SetInt (LEVEL_KEY + level.ToString (), TRUE);

or

PlayerPrefs.SetInt (LEVEL_KEY + level.ToString (), UNLOCKED);

Or to use a string instead of an int to store it in the PlayerPrefs. Doesn’t make a huge difference but no real reason to use a magic number when constants are used so readily everywhere else.

For bonus usability, we could add a couple convenience methods like so:

const int TRUE = 1;

public static boolean GetBool(string key) {
	return PlayerPrefs.GetInt(key) == TRUE;
}
1 Like

Privacy & Terms