- Home /
How to add strings in a list AND be able to save it and load it? Using playerpref OR serialization
I been searching this for a while and I am not moving forward at all. Any comment will help. So it you guys have any ideas just comment it. I tried to do one with playerPref, (Below is the code), BUT It is not working.
public List<string> Levelnames= new List<string>();
void Start()
{
for (int i = 0; i < Levelnames.Count; i++)
{
if (PlayerPrefs.HasKey("levels" + i))
{
PlayerPrefs.GetString("levels" + i, Levelnames[i]);
print("load");
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Q))
{
Levelnames.Add("levels");
for (int i = 0; i < Levelnames.Count; i++)
{
if (!PlayerPrefs.HasKey("levels" + i))
{
PlayerPrefs.SetString("levels" + i, Levelnames[i]);
print("save");
}
}
}
}
Answer by bobisgod234 · Sep 04, 2017 at 06:49 AM
Perhaps something like this?
This add "levels" string to the Levelname list, and then save it to PlayerPrefs when you hit Q. When you start, it will load the previously saved levels.
public List<string> Levelnames = new List<string>();
void Start()
{
for (int i = 0; PlayerPrefs.HasKey("levels" + i); i++)
{
Levelnames.Add(PlayerPrefs.GetString("levels" + i));
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Q))
{
Levelnames.Add("levels");
for (int i = 0; i < Levelnames.Count; i++)
{
PlayerPrefs.SetString("levels" + i, Levelnames[i]);
}
}
}
Hey Bob, Thank you a lot, I forgot to mention that the list already have 3 strings in it and the "levels" string is the one I want to add to the list and save it.
If I try the code your wrote with the 3 strings in the list, the 3 strings get repeated and then I get the "levels" string, which kind of make sense with the above code.
Now the problem is how do I prevent the 3 strings from repeating itself?
ALSO there will be other more strings that I would like to add in the list and save it.
the list already have 3 strings
How are these 3 strings added to the list? Are they just assigned in the inspector?
If so, try:
public List<string> Levelnames = new List<string>();
private int initialLength;
void Start()
{
initialLength = Levelnames.Count;
// only load extra entries, ignore any strings that were a
for (int i = initialLength; PlayerPrefs.Has$$anonymous$$ey("levels" + i); i++)
{
Levelnames.Add(PlayerPrefs.GetString("levels" + i));
}
}
void Update()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Q))
{
for (int i = initialLength; i < Levelnames.Count; i++)
{
PlayerPrefs.SetString("levels" + i, Levelnames[i]);
}
}
}
This will only load and save entries added after Start() is called. (Note: Untested).
Hey Bob, Yes, the 3 strings are assigned in the inspector. I tried the code above and It works..... I am able to save only the "levels" string and load it. It would be awesome if you could help me with one more thing.
If I want to add more strings in the list by pressing certain keys. Like pressing key E and I add one more string. Pressing key R and I add one more string and so on. How can I achieve that?
I do not want to do the whole thing again if I want the add another string to the list.
I know we need to change the "levels" string to something else, but I have no idea how to proceed from there.
Thank you for helping.
Answer by jasonlu00 · Sep 03, 2017 at 06:04 PM
Since you didn't add any string to your Levelnames, Levelnames.Count is always zero. So you will never enter for loop. What you want to do is setup the list properly on Start:
public List<string> Levelnames = new List<string>();
private const int _numOfLevels = 10;
private const string DEFAULT_NAME = "Unknown";
void Start()
{
//Init Levelnames
for(int i = 0; i < _numOfLevels; i++)
{
Levelnames.Add(PlayerPrefs.GetString("levels" + i, DEFAULT_NAME));
}
}
void Update()
{
if(Input.GetKeyDown(KeyCode.Q))
{
for(int i = 0; i < Levelnames.Count; i++)
{
PlayerPrefs.SetString("levels" + i, Levelnames[i]);
print("save");
}
}
}
Hey Jason, What does the "$$anonymous$$" means?
When we are adding: Levelnames.Add(PlayerPrefs.GetString("levels" + i, DEFAULT_NA$$anonymous$$E)); in Start(), are we really putting the save string or are we just adding a new string. I am a little confuse, can you explain a little. I am pretty new in this platform.
Thank you.
Hey Jason, I updated my scripts, I forgot to include the "add string" in the update function, under the if statement.
Your answer
Follow this Question
Related Questions
BinaryFormatter - saving and loading a list containing sprites. 0 Answers
Using BinaryFormatter for deserialization - ArgumentException: method argument length mismatch 0 Answers
A node in a childnode? 1 Answer
Is it possible to name a list by a string variable? 2 Answers
Using Player Prefs 0 Answers