0

Okey. So I'm making this simple script that spawns blocks from an Array, almost like a tilebased 2d game. But it is not preforming as I want it to, I know why, but I have'nt found any solution. Please help me out if you can :)

So here I run the StreamReader with a space as a delimiter:

    StreamReader reader = new StreamReader (@"blablabla\path\levelOneMap.txt");
    string s = reader.ReadToEnd();

    while(s != null)
    {
        char[] delimiter = {' '};
        // Level holder is a string array, wish hold x, s or h.
        levelHolder = s.Split(delimiter, System.StringSplitOptions.RemoveEmptyEntries);

        s = reader.ReadLine();
    }

The file he's reading looks something like this:

x x x

x s x

x h x

And so on, so were the x'es are, it will spawn a block. s is a starting block, and h is just a hole in the ground.

I did a bunch of debug.logs and changed the x, s and h to 1 2 3 4... to see what was going on. Turn out the first 1 2 is fine, but the levelHolder[2] contains both 3 and 4. How can I make this script understand that it should skip both ' ' and '\n' ?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364

1 Answers1

0

Once you call ReadToEnd(), you have the entire file in a single variable, and the stream has reached end-of-file. Calling ReadLine() after that won't do anything. So you can remove the loop.

Then add whatever delimiters you want, possibly you want all ASCII white-space characters ({ ' ', '\t', '\r', '\n', '\v' }).

That's if you want all the blocks placed into a single levelHolder array. If you want each line in its own array, then keep the loop, and change the initial ReadToEnd() into another ReadLine().

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720