- Home /
how to playerprefs.set array of strings with one key, then get individual element from that array to make edit ?
in my car racing game, I save best times of each track with playerprefs
there are currently 8 tracks buy I might add more in the future.
here's how I saved
private void BestTime1()
{
Bmin = PlayerPrefs.GetFloat("b1min", 01);
Bsec = PlayerPrefs.GetFloat("b1sec", 14);
Bmil = PlayerPrefs.GetFloat("b1mil", 78);
besttime[0].text = string.Format(BestTimeFormat, Bmin, Bsec, Bmil);
}
private void BestTime2()
{
Bmin = PlayerPrefs.GetFloat("b2min", 00);
Bsec = PlayerPrefs.GetFloat("b2sec", 53);
Bmil = PlayerPrefs.GetFloat("b2mil", 84);
besttime[1].text = string.Format(BestTimeFormat, Bmin, Bsec, Bmil);
}
and so on...
did it 8 times so scoreboard could show best times of all 8 tracks
as you can see, to save best time of 8 tracks I'm using 24 different keys.
so I want to know if there's easier way to do it
can I save entire array of all 8 best times as strings, and then get access to individual element when a best time is changed ?
I could only find single youtube video on this topic, but that was too pro level explanation, I'm noob, please help.
Answer by xxmariofer · Aug 26, 2019 at 11:02 AM
you cant add arrays in player prefs, what you could do is use strings, something like this but all this is pseudocode
void Example()
{
string bestTimes = "";
bestTimes += Bmin.ToString() +" : "+ Bsec.ToString() + " : "+ Bmil.ToString() + "-";
bestTimes += Bmin2.ToString() +" : "+ Bsec2.ToString() + " : "+ Bmil2.ToString() + "-";//this is just ab example
PlayerPrefs.SetString("YourKey", bestTimes);
string[] arrayOfResults = PlayerPrefs.GetString("YourKey").Split("-");
}
hi thank for the reply
so I got the string
made changes to single element
now how do I save it back ?
private void example()
{
// what I have $$anonymous$$yString = "1-0-0-0" // what I want $$anonymous$$yString = "1-1-0-0"
string $$anonymous$$yString = PlayerPrefs.GetString("$$anonymous$$y$$anonymous$$ey", "1-0-0-0"); // I got the string
string[] $$anonymous$$yArray = $$anonymous$$yString.Split('-'); // got individual element of string
$$anonymous$$yArray[1] = 1; // made changes to individual element of string
// now how do I save the edited string back to $$anonymous$$y$$anonymous$$ey ?
}
string newString = "";
foreach(string iterator in $$anonymous$$yArray)
{
newString += iterator + "-";
}
PlayerPrefs.SetString("$$anonymous$$y$$anonymous$$ey", newString);
Problem
adding "-" after every iterator, the string becomes "1-1-0-0-" ins$$anonymous$$d of "1-1-0-0"
so next time I PlayerPrefs.GetString("$$anonymous$$ykey").Split('-') array length becomes 5 ins$$anonymous$$d of 4 as 1-1-0-0-null
and after going through for each loop string becomes "1-1-0-0--"
understood ?
my english is not very good. I will explain again if you want.
Your answer
Follow this Question
Related Questions
How assign gameobjects to a string and reference them again? 3 Answers
String Access problem 1 Answer
Splitting String Into Array 2 Answers
"Extract" from a string 1 Answer
Array of Array 1 Answer