PlayerPrefs *Javascript*

I am having some trouble understanding playerprefs. I have a game where I want to be able to save what level you are on next time you load the game. Or every time you complete a level, its saves so that when you start the game, you start on that level. Can somebody point me in the right direction, with either some sample code or a tutorial that shows how to save the actual level you are playing? I never programmed any kind of save feature so I am completely new to this concept. I would like to also be able to load the saved game form a button on the menu screen, thanks.

Javascript Please

A little about the game:

2.5D sidescroller, you control a character that fights off waves of spawning enemies, two waves per level than the next level loads. For each level you start over with new health so really all that needs to be saved is which level you last beat so it loads the correct level.

Well the easiest way certainly is to simply store the current level name or int via PlayerPrefs, load it up again once needed and then use LoadLevel() to load a new level.

PlayerPrefs themselves are completely separate from the logic deciding what to do with the values you get from them. See them as a way to store and load data, it’s nothing more than that. You can load any value which you have previously stored, at any time.

What you do with those values you get is actually fully up to you.

I understand what you are saying conceptually, thank you for the response. I’m confused where to put the code within my game, however. Should I make a gameobject that stores this data, like a GameSettings gameobject? If I want to save what level the player is on, what is an ideal setup in other words, code and gameobject wise? I don’t get where to apply the script I make or the actual workflow of typing in the code is what I am saying lol.

A Javascript example would be GREATLY appreciated.

It heavily depends on your code.

Just write the value when the user presses the quit button. Then when you load the level in one of the start() functions check the value of that int and then arrange the level based on the value of it. Perhaps in the script that handles all your spawning of waves of bad guys?

You can just make blank objects called code, and attach all the code that doesn’t feel like it fits on an object there, at least that is what i do.

Super simple example:

function Start () {
	// --------------------- When player has picked the item before, then destroy it at start of frame so that he cannot pick it again
	if (PlayerPrefs.GetInt("queststatus")==1){	
		Destroy(GameObject);		
	}
}

function onCollisionEnter(other:collider){
	// --------------- Player picks questitem, We save the value of the current queststatus.
	PlayerPrefs.SetInt("queststatus", 1);	
	Destroy(GameObject);
}

Attach this to a quest object that the player can pick up. He will now only pick it once in the game, no matter how often he will enter the frame.

I wouldnt put gameplay important stuff in playerprefs as it stores in the PC registry and any player could easily cheat his way with just openning a registry editor, find the key, and edit it to any value he likes (basicaly if you put the ingame money value in playerprefs, you would invite the player to change it to 999,999,999 ).

I’m pretty new to unity but i would still use a “gameManager” object with a script attached that would look after all those stats. (and maybe put “Application.DontDestroyOnLoad(this)” in case you’re making a larger game).

I disagree. PlayerPrefs is a convenient and easy way to deal with game data. Which is good enough in most cases. For hobby games it shouldnt be necessary to protect player content at all. Means the normal PlayerPrefs are good enough. For commercial indie games and up theres a Secure Playerprefs plugin in the assets store, with encryption :slight_smile:

I see the need to use cheats more like a proof that the game isnt correctly balanced. Else the player wouldnt look for cheats. When somebody wants to cheat then he will always find a way. And be it by writing a trainer to modify the ram content.

As long as its not a Multiplayer game, why would it matter? Most games allow “cheating”; sometimes we are just looking to mess around not really play the game “correctly”. Ie: In Skyrim I’ll spawn a thousand cheese rolls, and surf them down a mountain, not the “correct” way to play Skyrim, but fun as heck. Isn’t he point of games to have fun?

This only works for loading between levels, not game sessions.

Thanks for all of the suggestions. Tiles, awesome example thats exactly what I needed. I am going to give this a shot this week and post back.

Quick question, is “queststatus” an example of a variable that I would set up before using playerprefs?

You don`t need to create a variable here. We work directly with the registry. “queststatus” is no variable, but the name under which the value gets stored in, and read from the registry. The script should work as is.

Oh yea I see what you mean now. Let’s take this a step further, what if I want to be able to save two players data? For example, on the main menu load screen, you can load game save 1 or game save 2, what is the process for setting something like that up?

The process is the same in all cases. You write and read the playerprefs, and do what you want with its values.

Playerprefs are just a way to store and retreive values and strings. Nothing more, nothing less. The rest is usual coding.

Ok thanks for all of the help guys.