How to copy a predefined playerprefs over the old playerprefs (or inject) first run?

Hiya,

I have a music app.

When the player first opens the app, for the first time, I will check a variable in playerprefs, to see if there’s no saved songs. If there are no saved songs, I would like to either replace his playerprefs with my premade one (so the list is interesting for a first-user) or somehow painlessly overwrite it using playerprefs commands.

Do you have any thoughts how I might do this?

I must be dense because I seem to be missing something obvious. You want to load playerprefs (understood) to check for the existence of a variable (also understood). If the variable is empty or missing (meaning that the user has never run your game before), create a new player prefs, populated with data, and save that.

This is all doable. It’s how I check for a “game in progress” on puzzle games where the user can exit the application to browse the web or wander off and check their stats, before finally coming back and picking up where they left off.

So which part exactly are you having issues with?

Why do you want to overwrite it?
Makes no sense and for iOS its not possible anyway I think as it maps to NSUserDefaults not a file that unity manages itself.
So there are easy ways to overwrite if you want but it makes totally no sense if you ask me

To solve your problems: You can just use the “what I would use if no previous prefs are found” value as the default values that the GetXXX returns if the value is not found and you get your default setup free out of the box :slight_smile:

Thats exactly what the purpose is.

If this is a problem about versioning: Store the version in a string upon saving the prefs, if you see the version to missmatch → PlayerPrefs.DeleteAll(); → thats it and you are ‘fresh on predefined again’

The problem is my playerprefs is very complex, with 24 sets of data which contain complicated physics and sound sample states and thousands of variables.

So I need some way of easily injecting a premade playerprefs into an existing playerprefs.

My current plan is to ship with a playerprefs I make on the desktop, read in the data and write it to the normal playerprefs on a first run. I was thinking there may be a slightly easier way?

With that much data, it would make more sense not to use PlayerPrefs at all.

–Eric

I’m with Justin on this one.

What part are you having trouble with? Mass writing?
If thats the case, do you have something against parsing a text/XML file?

It is getting late, so I do have an excuse if I missed something obvious :slight_smile:

I’m with Eric5h5 on this one, PlayerPrefs is probably not your best bet for “thousands of variables.” That said, if you make your own property bag, which is what player prefs really is, but a kind of slow implementation, and then serialise the property bag in to a binary representation that you can then stuff inside of a player prefs entry.

If you have hierarchical or structured data, consider your own custom file save format, binary or XML, or a class or struct that can be serialised directly to storage.

Again, you can always stuff a more complex data structure inside of player prefs if you want the convenience of easy save and restore of your data.

Use SQLite if you try to superbomb the system.

I would not even go with XML anymore with that many variables due to the simply fact of how much data the RAM will have to hold even when not needed just to parse anything.

All good suggestions guys. I think I’ll do something like that.