2 Replies - 354 Views - Last Post: 04 May 2012 - 12:50 PM

#1 Gonzo0193  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 49
  • Joined: 16-December 09

Map loading issues

Posted 03 May 2012 - 02:51 PM

I am loading maps into an array from a text file using this code:

            using (StreamReader sr = new StreamReader(layerFile))
            //using (StreamReader sr = new StreamReader("Content/levels/" + map + "/layer1.txt"))
            {
                string line;
                int[,] partsArray = new int[(int)mapsize.X, (int)mapsize.Y];
                int partCounter = 0;
                int lineCounter = 0;
                while ((line = sr.ReadLine()) != null)
                {
                    string[] parts = line.Split(',');
                    
                        //build the parts array
                        for (int rowWidthCounter = 0; rowWidthCounter < line.Length/2; rowWidthCounter++)
                        {                               
                                partsArray[lineCounter, rowWidthCounter] = Convert.ToInt32(parts[partCounter]);
                                partCounter++;
                        }
                    lineCounter++;
                    partCounter = 0;
                }
                //layer1 = partsArray;
                holderLayer = partsArray;

                switch (layerNum)
                {
                    case 1: layer1 = holderLayer; break;
                    case 2: layer2 = holderLayer; break;
                    default: break;
                }

            }



This works fine loading from a file in this format:

1,1,1,1,1,1,1,1,1,1,1,
1,1,1,6,7,1,1,1,1,1,1,
1,1,1,8,9,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,



As long as the number between ,'s is 0 - 9 then it all works fine, i can even add a double digit number to a line, but if i try to add two double didgit numbers to a line like:

1,1,1,1,1,1,1,1,1,1,1,
1,1,1,6,7,1,1,1,1,1,1,
1,1,1,8,9,1,1,1,1,1,1,
1,1,1,10,11,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,



then i get this error:

A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
Input string was not in a correct format.



and i have no idea why. =[

Is This A Good Question/Topic? 0
  • +

Replies To: Map loading issues

#2 lesPaul456  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 173
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: Map loading issues

Posted 03 May 2012 - 04:59 PM

It's a pretty simple solution. The problem lies is with your for loop. This part in particular: rowWidthCounter < line.Length/2.

I understand what you're trying to do here, but it simply won't work with multiple digits.

Instead, try this:
// ...

for (int rowWidthCounter = 0; rowWidthCounter < mapSize.Y; rowWidthCounter++)
{                              
    partsArray[lineCounter, rowWidthCounter] = Convert.ToInt32(parts[partCounter]);
    partCounter++;
}

// ...



Notice that I changed the conditional-expression to this: rowWidthCounter < mapSize.Y

Hope this helps point you in the right direction! :)
Was This Post Helpful? 1
  • +
  • -

#3 Gonzo0193  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 49
  • Joined: 16-December 09

Re: Map loading issues

Posted 04 May 2012 - 12:50 PM

Thankyou ^^, its so obvious when you point it out haha.

Thanks for that anyway :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1