PlayerPrefs storage

Hi all,

PlayerPrefs.SetFloat("Player1_PosX", player1.transform.position.x);
PlayerPrefs.SetFloat("Player1_PosY", player1.transform.position.y);
PlayerPrefs.SetFloat("Player1_PosZ", player1.transform.position.z);
player1.transform.position.x = PlayerPrefs.GetFloat("Player1_PosX");
player1.transform.position.y = PlayerPrefs.GetFloat("Player1_PosY");
player1.transform.position.z = PlayerPrefs.GetFloat("Player1_PosZ");

Is there any way to store the position x,y,z of the player into one playerprefs only? Thanks.

You can combine the values into a string with a delimiter and then cut it up (explode, in PHP) when you take it out.

can u show me examples? thanks.

This is the code I created for a past project of mine. This game featured a number of mini-games and allowed for each mini-game’s scores to be saved individually.

As such, this code saves different lists of scores and loads it into a custom class when loaded, crKeyVal. crKeyval contains a name and value and contains functions to add, remove, change and simply report the values of each. Although my code uses this class it should be simple enough for you to cut that out of the code and use it any way you want.

When you first load the scores from disc, it only loads the scores that were saved and creates fictional entries for the positions that were not saved.

When a player gets into the leader board, the last player is removed, all other players that scored less than this player gets bumped down one spot and the new score is inserted where it belongs. During this process, the lower scores get saved to disc and the higher scores remain untouched. This means that if nobody ever gets into the leader boards then nothing is saved to disc, but of someone manages to get, say, position 8, then position 8,9 and 10 are stored and not the fictional ones.

Hope this helps

function InsertHighScore()
{
	var range		: int = Globals.currentRange; 
	var highScores	: Array[];
	var kv1			: crKeyVal;

	highScores = GetHighScoresFromDisc();
	mark = Globals.positionThisTurn;

	for(x = highScoresToSave - 1; x > mark; x--)
	{
		kv1 = highScores[range][x-1];
		
		PlayerPrefs.SetString("Name|" +range+"|"+x, kv1.getName() );			
		PlayerPrefs.SetInt   ("Score|"+range+"|"+x, kv1.getValue());			
	}

	PlayerPrefs.SetString("Name|"+range+"|"+mark, currentName.Join(""));			
	PlayerPrefs.SetInt("Score|"+range+"|"+mark, currentScore);
}

function GetHighScoresFromDisc() : Array[]
{
	var names = new Array("Ace","Jo","Jim","Bo","Sam","Edd","Ward","S Mave","Rick","Me");
	var kv : crKeyVal;
	var highScores : Array[];
	
	highScores = new Array[numberOfRanges];
    for(x = 0; x < highScores.length; x++)
    {
   		highScores[x] = new crKeyVal[highScoresToSave];
   		for (y = 0; y < highScoresToSave; y++)
   			highScores[x][y] = new crKeyVal();
    }
	
	for (y = 0; y < highScores.length; y++ )
	{
		var highest = 30000;
		for (x = 0; x < highScoresToSave; x++)
		{
			kv = highScores[y][x];
			kv.setName(PlayerPrefs.GetString("Name|"+y+"|"+x, names[x]));
			kv.setValue(PlayerPrefs.GetInt("Score|"+y+"|"+x, highest));
			highScores[y][x] = kv;
			highest -= 1500;
		}
	}
		
	return highScores;
}