Search code examples
c#xna

XNA C# .txt file Tile Engine


I'm not very experienced when it comes to using .txt files in XNA and therefore i need some help.

I'm tring to make a Tile Engine read and put information depending on what number is in the .txt document. It looks something like this:

1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 etc

Now, this document contains 50 characters in X (25 without [ , ]) and has 15 lines in Y. The problem is that it only reads the first line in Y and not the other 14 remaining ones in Y, which is causing it to crash.

Here is the code:

public void LoadMap(string MapName)
    {

        using (StreamReader streamReader = new StreamReader("Content/" + MapName + ".txt"))
        {

            do
            {
                string line = streamReader.ReadLine();

                string[] numbers = line.Split(',');

                int temp = 0;

                    for (int y = 0; y < loopY; y++)
                    {
                        for (int x = 0; x < loopX; x++)
                        {

                            Blocks[y, x] = new Block();

                            //Crashes here when temp reaches 25
                            if (int.Parse(numbers[temp]) == 1)
                                Blocks[y, x].color = Color.Blue;

                            else if (int.Parse(numbers[temp]) == 2)
                                Blocks[y, x].color = Color.Violet;

                            else
                                Blocks[y, x].color = Color.White;

                            temp++;
                        }

                    }

            } while (!streamReader.EndOfStream);


        }

    }

Solution

  • Based on your reply to my question:

    "Index was outside the bounds of the array." Yes i checked it loopX is 25 and Numbers is 25

    numbers[] is zero-indexed, so the upper bounds is numbers[24]. If loopX is 25 then, yes, you'll see an exception.

    You're iterating through loopX and then loopY and incrementing temp every time. You'll either need to set temp back to 0 after each loopX iteration, or just use the numbers array instead of the loop values instead.

    I suggest changing your loop to use numbers instead:

    for (int x = 0; x < numbers.Length; x++)
    

    And then, to use your existing code, check value using:

    if (int.Parse(numbers[x]) == 1)
    

    EDIT: This is what I was trying to explain:

    using (StreamReader streamReader = new StreamReader("Content/" + MapName + ".txt"))
    {
        int y = 0;
    
        do
        {
            string line = streamReader.ReadLine();
    
            string[] numbers = line.Split(',');
    
            for (int x = 0; x < numbers.Length; x++)
            {
                Blocks[y, x] = new Block();
    
                if (int.Parse(numbers[x]) == 1)
                    Blocks[y, x].color = Color.Blue;
    
                else if (int.Parse(numbers[x]) == 2)
                    Blocks[y, x].color = Color.Violet;
    
                else
                    Blocks[y, x].color = Color.White;
             }
    
               y++;
    
            } while (!streamReader.EndOfStream);
    
    
        }