How to implement Playerprefs into my game template?

I bought a game template off of the assets store to help speed up my development. I am now stuck however as I want to add a level select screen that locks levels based on progression. I know I want to use playerprefs to do it as it seems to be the most logical way.

So I have already made the level select screen using UI buttons for my levels 1-4 and using raw images to cover up the buttons that are locked. I created a simple java script to be able to disable the raw image game object using if(PlayerPrefs.GetInt(“savedgame1”)==1) changing savedgame for each rawimage.

Now I have the problem of figuring out how to add the key into the game over. The game uses two factions one being a player faction and the other an ai faction. I just do not have the c# knowledge to do this. Here is a snip it from the code in the game controller. player faction id is set to 0 by default. I just need to figure out how to add in a playerprefs key for faction id 0 winning and also a variable that will let me set different levels (a scene identifier) IE. gamesaved1, gamesaved2, gamesaved3 ect for each scene.

public static void GameOver(int factionID){
			if(FactionManager.IsPlayerFaction(factionID)){
				PerkManager.GainPerkCurrencyOnVictory();
			}
			
			if(onGameMessageE!=null) onGameMessageE("GameOver");
			
			gamePhase=_GamePhase.Over;
			
			FactionManager.GameOver();
			
			if(onGameOverE!=null) onGameOverE(factionID);
		}

Hi,

I’m not sure to understand perfectly what you want but let’s give it a try ^^

First of all, if you want a variable to appear in the inspector as a “textfield”, you need to set it as public

Hence, if you want to replace “savedgame1”, which is a string, you’ll need a variable like so:

public string savedGameNameToPP;

And you need to declare it with the other variables at the beginning of your script.

Now we have the variable, editable through the inspector you need to change what you call in your gameover function:

if (FactionManager.IsPlayerFaction (factionID)) {
   PlayerPrefs.SetInt(savedGameNameToPP, 1);
}

And that should be it :slight_smile: