Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Alternatives to StreamReader

Discussion in 'Scripting' started by urzathran, May 17, 2019.

  1. urzathran

    urzathran

    Joined:
    Sep 9, 2018
    Posts:
    7
    What I want to do:
    I want several different sets of data in a table that a random number can be checked against. Each entry has a varying range of numbers so that they don't all have identical probability.

    How I did it:
    I created new Class for each table I want to be able to roll on. A StreamReader looks at a .txt file and uses comma splits to give each Object a lower bound, upper bound, and text property.

    Why I want to change:
    I'm having a heck of a time making sure the .txt files are in the appropriate place in my builds, and while looking for solutions for Android specifically, the recommendation was "you can use the resources folder, but you really shouldn't." I haven't fully figured out how the resources folder would solve my problems, but I figured I'd rather ask the community if there's simply a better way I don't know. This is my first project.

    An example of the code I'm using:

    Code (CSharp):
    1.  void Start()
    2.     {
    3.         StreamReader defaultFile = new StreamReader("Risk Roll.txt");
    4.  
    5.         while ((line = defaultFile.ReadLine()) != null)
    6.         {
    7.             int lowerBound = System.Convert.ToInt32(line.Split(',')[0]);
    8.             int upperBound = System.Convert.ToInt32(line.Split(',')[1]);
    9.             string travelText = line.Split(',')[2];
    10.             myTravels.Add(new travelTime(lowerBound, upperBound, travelText));
    11.         }
    12.     }
    13.  
    14. private class travelTime
    15.     {
    16.         private int lowerBound;
    17.         private int upperBound;
    18.         private string travelText;
    19.  
    20.         public travelTime(int lowerBound, int upperBound, string travelText)
    21.         {
    22.             this.lowerBound = lowerBound;
    23.             this.upperBound = upperBound;
    24.             this.travelText = travelText;
    25.         }
    26.  
    27.         public int getLowerBound()
    28.         {
    29.             return lowerBound;
    30.         }
    31.         public int getUpperBound()
    32.         {
    33.             return upperBound;
    34.         }
    35.         public string getTravelText()
    36.         {
    37.             return travelText;
    38.         }
    39.     }
    My brother (who has not worked in Unity specifically) thought I should just add the text/numbers directly to the code. While that would work, it seems to me you'd have some awfully ugly chunks of data. Is that my best option? Do you have better?
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,292
    Do you really need to store it as .txt?
    ScriptableObject could be an alternative for that.
     
  3. urzathran

    urzathran

    Joined:
    Sep 9, 2018
    Posts:
    7
    I doubt I need to, no. I just don't know what I'm doing. Would a ScriptableObject provide a place for me to dump tab or comma delimited text?
     
  4. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,921
    A scriptable object is no different than a gameObject with a script, as far as typing your own text goes. A, ummm... TextArea(?) can be told to display several lines in the Inspector, with line-feeds. But a textfile is just better if you want to use a real, fullscreen editor, with a nicer font and so on.

    I've never used Android. iOS textfiles (which also worked in the build) was a pain. But the thing is, once you figure out where things go it works just fine (not in Resources? You can't write to things there, so maybe that's why. Writable goes into persistent-something).