So I understand the basics of PlayerPrefs and how to save data, one thing I can’t figure out though is how to save a list of items. For example: equipped items, items in inventory, etc. I was thinking a ‘forloop’ would work for this but so far I have not been successful. Can anyone give me an idea of how to go about this? Any input is much appreciated. Thank you!
4 Answers
4with playerprefs there are two ways to do it... one is using a for loop as you tried. Like the below
for(int i=0;i<TotalInventoryItem;i++)
{
PlayerPrefs.SetString("InventoryItem"+i,"*insert your item value or name*");
}
even while retrieving you can do the same. This is one method..
But if the Items in inventory is an array or list!! you can easily save and retrieve it in this method. (unless number length or array is way too big!!)
for instance let the array name be Inventory
int[] Inventory;
string temp="";
for(int i=0;i< Inventory.Length;i++)
{
if(i!=Inventory.Length-1)
temp+=Inventory_.ToString()+"*";//note that the last character you add_
*//is important*
*else*
_temp+=Inventory*.ToString();*_
_*}*_
_*PlayerPrefs.SetString("InventoryItems",temp);*_
_*```*_
_*<p>While retriving you have to just get the InventoryItems key and slplit it and explode it back, like this</p>*_
_*```*_
_*string temp=PlayerPrefs.GetString("InventoryItems");*_
<em>_string[] tempArray=temp.Split("*".ToCharArray());_</em>
_*for(int i=0;i<tempArray.length;i++)*_
_*{*_
<em><em>Inventory_=System.Int32.Parse(tempArray*);*_</em></em>
<em><em>_*}*_</em></em>
<em><em>_*```*_</em></em>
<em><em>_*<p>Hope it is clear:)</p>*_</em></em>
Wow, thank you very much guys. You gave far more info that what I was even hoping for. I just woke up and haven't had a chance to try anything yet but I'm sure i'll have no problem figuring it out now.
– ObviouslyInsaneUse ArrayPrefs2 (as long as it’s arrays of basic types like Vector3, String, etc.).
Thank you Eric, I'm headed to bed now but I will definitely look into that in the morning. One more thing I think I should mention though now that I think of it is the items in the list I am trying to save contain both string and int values. Will using just SetStringArray or SetIntArray work for saving both values? Sorry if that's confusing I can't think straight anymore.
– ObviouslyInsaneAlso the data is in one of my own Classes if that makes a difference. (Wasn't letting me edit my last comment)
– ObviouslyInsaneSetStringArray only works with String[] arrays, and SetIntArray only works with int[] arrays. You could use the techniques in that script to make a custom function, or you could convert the data in your classes to String[] and int[] arrays before saving, then convert the String[] and int[] arrays to your classes when loading.
– Eric5h5Ok I guess I can use a little more help. I should have mentioned I’m using JS and trying to save items in a List. Here’s what I have so far:
import System.Collections;
var Inventory;
var temp : String = "";
var tempArray : String[];
var i : int;
//SAVE DATA
Inventory = GameObject.Find("GUIElements").GetComponent("Inventory").inventory;
for(i = 0; i < Inventory.Count; i++) {
if(i != Inventory.Count -1)
temp += Inventory.ToString()+"*";
else
temp += Inventory.ToString();
}
PlayerPrefs.SetString("InventoryItems",temp);
//LOAD DATA
Inventory = GameObject.Find("GUIElements").GetComponent("Inventory").inventory;
temp = PlayerPrefs.GetString("InventoryItems");
tempArray = temp.Split("*".ToCharArray());
for(i = 0; i < tempArray.Length; i++)
{
Inventory _= System.Int32.Parse(tempArray*); <<<ERROR HERE*_
}
i made 2 typos there sorry in the first for loop... i have updated in my answer now and regarding missing method exception add import System.Collections; in the beginning of the file.... also while declaring use String, like var temp:String; var tempArray:String[]; and in your last for loop condition it is tempArray.Length not Count
– flamyOk, I changed everything as you said and added the import System.Collections but I am still getting that same error. In case it makes any difference I want to clarify that the variable (Inventory) I am referencing is a list and not an array.
– ObviouslyInsanehmm never had this problem it work fine for me... i copied ur code and even it works fine here... can copy the present code here?
– flamyCertainly, I updated the code in my second post to show what I have now. I'm not getting the Missing Method error anymore, but I am getting a new one at the line I marked. It says 'FormatException: Input string was not in the correct format'
– ObviouslyInsanefor(i = 0; i < Inventory.Count; i++) { if(i != Inventory.Count -1) temp += Inventory_.ToString()+""; <<this line_ else _temp += Inventory.ToString(); <<this line*_ } PlayerPrefs.SetString("InventoryItems",temp); you have to add i there, is that done also try printing the temp vatiable when u save it and also when u retrive it
– flamyIf the data that you’re trying to serialize is in your own classes, then you may look into .NET serialization. But keep in mind, that it doesn’t work when you want to store MonoBehaviours or other class instances from Unity namespaces, because deserialization tries to do “new” on MonoBehaviours and not Instantiate() as Unity requires.
A very tiny example of the concept:
http://blog.kowalczyk.info/article/Serialization-in-C.html
Has anyone found a solution to this yet? I have run into the same problem where I want to save a list of data, but I am getting the exact same error on the same line: FormatException: Input string was not in the correct format'
– iteis