Search Unity

Storing a large amount of values?

Discussion in 'Scripting' started by Deleted User, Apr 15, 2011.

  1. Deleted User

    Deleted User

    Guest

    Hello. Is there an alternative to player prefs for storing data? I am looking into making an RPG (specifically a pokemon like RPG), and it would be impossible to have a player prefs key for everything(mainly because new party members are always being added). Is there a way to basically create a prefab from within the game(the way the data for the monsters works is, they are all variants on a single monster template script, using public variables to access their statistics), and then load it up later? Thanks
     
  2. billykater

    billykater

    Joined:
    Mar 12, 2011
    Posts:
    329
    I would use a file for storing the values.
    You can store data at Application.persistentDataPath subfolders/files.

    Basically I would create a runtime representation class for each object to store (player, monster, ...) and either write them to a file myself using classes from the System.IO .NET namespace (like StreamWriter/Reader or BinaryWriter/Reader) or using the XmlSerializer for convenience.

    For the data template you can use the same method but just load it from your asset path (Application.DataPath) instead.
     
  3. Deleted User

    Deleted User

    Guest

    That sounds pretty complex, do you have an example?
     
  4. billykater

    billykater

    Joined:
    Mar 12, 2011
    Posts:
    329
    Monster.cs
    Code (csharp):
    1.  
    2. using System.Xml;
    3. using System.Xml.Serialization;
    4.  
    5. public class Monster
    6. {
    7.     [XmlAttribute("name")]
    8.     public string Name;
    9.    
    10.     public int Health;
    11. }
    12.  
    MonsterContainer.cs
    Code (csharp):
    1.  
    2. using System.Collections.Generic;
    3. using System.Xml;
    4. using System.Xml.Serialization;
    5. using System.IO;
    6.  
    7. [XmlRoot("MonsterCollection")]
    8. public class MonsterContainer
    9. {
    10.     [XmlArray("Monsters"),XmlArrayItem("Monster")]
    11.     public List<Monster> Monsters;
    12.    
    13.     public void Save(string path)
    14.     {
    15.         var serializer = new XmlSerializer(typeof(Monster));
    16.         using(var stream = new FileStream(path, FileMode.Create))
    17.         {
    18.             serializer.Serialize(stream, this);
    19.         }
    20.     }
    21.    
    22.     public static MonsterContainer Load(string path)
    23.     {
    24.         var serializer = new XmlSerializer(typeof(MonsterContainer));
    25.         using(var stream = new FileStream(path, FileMode.Open))
    26.         {
    27.             return serializer.Deserialize(stream) as MonsterContainer;
    28.         }
    29.     }
    30. }
    31.  
    monsters.xml
    Code (csharp):
    1.  
    2. <MonsterCollection>
    3.     <Monsters>
    4.         <Monster name="a">
    5.             <Health>5</Health>
    6.         </Monster>
    7.         <Monster name="b">
    8.             <Health>3</Health>
    9.         </Monster>
    10.     </Monsters>
    11. </MonsterCollection>
    12.  
    TestUsage.cs
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.IO;
    4.  
    5. public class TestUsage : MonoBehaviour
    6. {
    7.     MonsterContainer monsterCollection;
    8.    
    9.     // Use this for initialization
    10.     void Start ()
    11.     {
    12.         monsterCollection = MonsterContainer.Load(Path.Combine(Application.dataPath, "monsters.xml"));
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void Update ()
    17.     {
    18.        
    19.     }
    20.    
    21.     void OnGUI()
    22.     {
    23.         foreach(var monster in monsterCollection.Monsters)
    24.         {
    25.             GUILayout.Label(monster.Name);
    26.         }
    27.     }
    28. }
    29.  
    For all other methods you can search the keywords I gave you here
    http://msdn.microsoft.com/en-us/library/
     
  5. cemC

    cemC

    Joined:
    Dec 23, 2010
    Posts:
    214
    Good question... if do you share any examples, i will appriciate
     
  6. cemC

    cemC

    Joined:
    Dec 23, 2010
    Posts:
    214
    thanks
     
  7. HarvesteR

    HarvesteR

    Joined:
    May 22, 2009
    Posts:
    531
    Nice!!

    here I was thinking that I was going to have to write a parser to save and load stuff in my game...

    That's a hell of a lot easier!! Thanks!!

    Cheers
     
  8. Deleted User

    Deleted User

    Guest

    I know this sounds dumb, but the six or so months of programming with Unity, experience I have only be learning Javascript, so I am kinda confused on billykater's C# examples(I understand a little, but It would be easier if it were in JS) . Can anyone put them in JS for me? Can anyone direct me to a good tutorial for translating C# to javascript and vice versa? Thanks
     
  9. andorov

    andorov

    Joined:
    Feb 10, 2011
    Posts:
    1,061
    billykater is using a standard .NET reflection-based serialization library thats found in System.Xml.Serialization.

    Luckily, you can call any .NET library from javascript using import <libraryname>.