Search Unity

Reading from a text-file and writing certain snippets to variables

Discussion in 'Scripting' started by Lamprey, Aug 3, 2015.

  1. Lamprey

    Lamprey

    Joined:
    May 26, 2015
    Posts:
    36
    I have a class that is as follows:
    Code (CSharp):
    1. public class Item
    2. {
    3.     private int _ItemID;
    4.     private string _ItemName;
    5.     private string _ItemDescription;
    6.     private TypeItem _ItemType;
    7.     private Dice _ItemDice;
    8.  
    9.     public int ItemID { get { return _ItemID; } set { _ItemID = value; } }
    10.     public string ItemName { get { return _ItemName; } set { _ItemName = value; } }
    11.     public string ItemDescription { get { return _ItemDescription; } set { _ItemDescription = value; } }
    12.     public TypeItem ItemType { get { return _ItemType; } set { _ItemType = value; } }
    13.     public Dice ItemDice { get { return _ItemDice; } set { _ItemDice = value; } }
    14.  
    15.  
    16.     public Item (int ID, string Name, string Description, TypeItem Type, Dice DiceType)
    17.     {
    18.         ItemID = ID;
    19.         ItemName = Name;
    20.         ItemDescription = Description;
    21.         ItemType = Type;
    22.         ItemDice = DiceType;
    23.     }
    24. }
    And then a text-file that is as follows:
    Code (CSharp):
    1. //ID,"Name","Description",Type,Dice,Modifier,Value;
    2. 1,"Dagger","A short blade",Blade,d4,1,5;
    3. 2,"Short Sword","An average length blade",Blade,d6,1,7,5;
    4. 3,"Long Sword","A long blade",Blade,d8,1,10;
    What I want to learn, is how to separate each individual string of data in-between the commas, and assign it to a variable of an item, inside a list of items.
     
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    well, once you load the file, you can read each line and then parse each string (string.split).

    google string.split if youre unsure on how to progress.


    An alternative approach would be using Serialize functions on xml docs.