Reputation: 41
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);
}
}
Upvotes: 0
Views: 119
Reputation: 1496
I see following things in your code, which seem suspicious to say the least:
If your temp >= numbers.Length, than you will get "Index was outside the bounds of the array".
If that happens after reading the first line, of course you will not read any further lines, because you got an exception and it will break.
You are overwriting your Blocks with every new line: If you read the first line and get into x and y loops, at the very first iteration of x and y, they will both be 0. So you write Blocks[0,0] = new Block()
. After you read the next line, you get again into x and y loops. They start again with 0, so at the very first iteration of x and y while processing the second line you will write Blocks[0,0] = new Block()
. Which will in fact overwrite the Block you wrote previously on index [0,0] during the handling of the first line. The same problem exists for all other combinations of x and y.
Debug, step-through and watch variables. That will help you understand what is going on.
Upvotes: 2
Reputation: 1479
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);
}
Upvotes: 2