Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ObviouslyInsane · Apr 28, 2012 at 08:30 AM · listplayerprefssaveconvertitem

Saving a list of items

Hi all, I asked this same question a couple months ago and got a lot of help, but I still could not figure it out so I just went on to work on other things. I'm at a point now where I really need to figure this out so I can finish my project. I have a better understanding now so hopefully I can phrase a better question. So here we go..

I have a list of items (Inventory) and each item in the list inherits from a custom class called 'Item' which contains string, int, and enum values as well as a texture2d. What I am trying to do is save all of the items in the Inventory list by converting the data in the Item class to strings so I can store them in a temporary string array, and then convert everything back to its original data when loading. I'm getting an error at the line I marked that says 'Format Exception: Input string was not in the correct format.' I'll put my current code up so you can see where I'm at.

 //GameSettings.js
 var tempArray : String[];
 var temp : String;
 var Inventory; 
 var convertVariables : Item;
                
 function SaveData() {
 Inventory = GameObject.Find("GUIElements").GetComponent("Inventory").inventory;
 for(i = 0; i < Inventory.Count; i++) {
                 
 if(i != Inventory.Count - 1){
 convertVariables.ConvertVariables();
 temp += Inventory[i].ToString() + "*";
 }
                                             
 else{
 convertVariables.ConvertVariables();
 temp += Inventory[i].ToString();
 }
 }
 PlayerPrefs.SetString("InventoryItems",temp);
 }     

   
 function LoadData() {
         
 temp = PlayerPrefs.GetString("InventoryItems");
 tempArray = temp.Split("*".ToCharArray());
             
 for(i = 0; i < tempArray.Length; i++){
 Inventory[i] = System.Int32.Parse(tempArray[i]); <<<ERROR OCCURS HERE
 }
 }
     
     
 //Item.js
 function ConvertVariables() {
 //Ints to convert
 price.ToString();
 minDamage.ToString();
 maxDamage.ToString();
 defense.ToString();
 healthAmount.ToString();
 essenceAmount.ToString();
 itemLevel.ToString();
 itemFireDamage.ToString();
 itemIceDamage.ToString();
 itemLightningDamage.ToString();
 itemElementDamage.ToString();
 curDura.ToString();
 maxDura.ToString();
     
 //Enums to convert
 rarity.ToString();
 jewelSlot.ToString();
 consumableType.ToString();
 armorType.ToString();
 armorSlot.ToString();
 weaponSlot.ToString();
 weaponSlot2.ToString();
 weaponDamageType.ToString();
 weaponType.ToString();
 itemArchetype.ToString();
 itemElementType.ToString();
     
 Debug.Log("Variables Converted");
 }


The 'ConvertVariables' function obviously is used to convert the ints and enums to strings. What I'm wondering is it possible or even necessary to convert the texture2d to a string? I'm curious because I'm not sure if that's what is causing the error or not. Anyways that's where I'm at right now, if anyone could give some insight it would be greatly appreciated. If you need any more info or code sample I'll be happy to provide it. Thank you!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by bompi88 · Apr 28, 2012 at 08:50 PM

You have "saved" your data to PlayerPrefs? Try to substitute this:

 for(i = 0; i < tempArray.Length; i++){
 Inventory[i] = System.Int32.Parse(tempArray[i]);
 }

with this:

 for(i = 0; i < tempArray.Length; i++){
 Debug.Log(tempArray[i]);
 //Inventory[i] = System.Int32.Parse(tempArray[i]);
 }

What does it print out? Maybe it`s about your enums? Try the Enum.Parse method for those.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ObviouslyInsane · Apr 28, 2012 at 09:28 PM 0
Share

Ok well its giving me a weird result. For every item in my inventory it just prints "Item". I'll have to look into the enum.parse method and see how it works. Give me a few $$anonymous$$utes and i'll report back.

EDIT: Not having any luck with enum.parse, any other suggestions?

avatar image ObviouslyInsane · Apr 28, 2012 at 11:38 PM 0
Share

Do I need to convert the texture2d variable somehow?

avatar image bompi88 · Apr 29, 2012 at 12:13 PM 0
Share

Texture2D.ToString() is only returning the name of the texture as string, but you can probably save the texture as png or save it as bytes in for example a string. Take a look at:

http://unity3d.com/support/documentation/ScriptReference/Texture2D.EncodeToPNG.html

You can encode it to png, and either save it to a file OR convert the array og bytes to string and then use PlayerPrefs to save it.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Saving lists 1 Answer

How to save an item as bought in player prefs? 0 Answers

How i can PlayerPrefs List<>? 0 Answers

Save and Load Function 2 Answers

Set int to object from list? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges