PlayerPrefs Performance Questions

Alright so I made a generic level code for my game that saves your rank for that level in playerprefs

I’m going to then put the rank on each level’s button, so i could just do like:

if (GUI.Button(Rect(10,70,50,30),""+PlayerPrefs.GetString("1")))

on every single button

but i’m worried this will lag once i get a lot of level buttons, so i’ve been thinking about putting all the ranks into an array then using that:

private var levelRanks = new String[11];  

function Start () {

	//load saved ranks
	//not using [0] in the array so the level number names line up (just for ease)
	levelRanks[1] = PlayerPrefs.GetString("1");
	//levelRanks[2] = PlayerPrefs.GetString("2");
	//. . .
}

if (GUI.Button(Rect(10,70,50,30),"" + levelRanks[1]))

my questions are: will playerprefs actually make lag later? Is the second way faster? Is there a way better(much faster to load AND still easy) way to achieve what i want?

thanks in advance for even reading all that :wink:

I’m not an expert on this but…

Number one point: PlayerPrefs are not SAVED until you use PlayerPrefs.Save() and that causes a bit of lag (so you’d use it only at certain instances). However, if I recall correctly, the PlayerPrefs will still be temporarily changed for the current scene, so if you access the same PlayerPrefs, it would be like accessing a static public variable (…which means there are better ways to do that, particularly if you’re not planning on saving them)

Number two: The second way IS faster, during run time. I usually throw a whole bunch of stuff into the Start() function which would ordinarily slow down the game, from quite a few GameObject.Finds and GetComponents to getting serialized data to getting a bunch of PlayerPrefs data. There may be some instance where it would be smarter to NOT cache things like this, but for me at least, I generally use PlayerPrefs as much as possible only in the Start() and end of levels.

Number three: Can’t comment on if this is the “right” or “best” way, as I’m unsure what you’re trying to do here. Oh wait, I get it. You’re making each level get its own data. Got it. I actually do a variation of this for my level+world selection screens, and its even LESS efficient as you’re during here (I get both some data from previously self-created data in .asset files and ALSO use PlayerPrefs to get quite a few pieces of information). Basically, you’re all good here. Second way is still faster, as that code would get called EVERY time OnGUI would be called, so PlayerPrefs would be continually called. Now that I think about it… Use second way. The first way is quite slower. My tip: Cache the data you can into variables to save time, for “simple” data like this.

(By the way, while writing number three, the entire thing’s full purpose and behavior finally snapped into place, so sorry if Numbers 1 and 2 seem a bit awry)

:slight_smile: thanks!

this gives me more confidence in using it knowing you have done something similar, ill probably just do the second way then

and now that im looking at it i could probably just use a loop to set my array as well. . . anyways thank you

By the way, when I said the second way is still faster, “as that code would get called EVERY time OnGUI…” I meant that the first way would call upon PlayerPrefs every time OnGUI is called. Reread that part real quick right now, and realized it was a bit unclear :wink:

Well, good luck then! And using a loop would make sense. I actually focused on making general scripts as much as possible, so I made it so you would place the name of the level into the Inspector on the respective GameObject and then it would use that level name/id to get all the respective data. And I’d also save it in a similar way, so the data would be saved in:
PlayerPrefs.SetInt(levelID+“typeofdata”,whatever);

Made it more flexible, although I ran into the issue where I was reordering every level for a world and forgot to change one of the level IDs… Luckily, a friend caught my mistake :smile: Pros and cons to generalized and one-time use scripts!

yeah right now im using like the level number as the name, so if i reorder the levels it will totally screw up, im gonna have to be careful about that