Reading a text file into a 2d array

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 6293 Views - Last Post: 05 January 2012 - 01:26 PM

#1 Davis123987  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 25-September 10

Reading a text file into a 2d array

Posted 04 January 2012 - 10:33 AM

Hello, Im new here to xna and Im trying to make a tile map.
I want to import a text file like this:

1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
1,1,1,1,1,1,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,3,3,1,1,1,1,1,1
1,1,2,1,1,1,1,1,1,1,1,1,1,1,1

And have numbers representing the tiles.

I looked at nick gravelyns tile engine tutorial series, but its way outdated and I cant find the source to it so i keep having to look through bunches of videos to find a part where he looks at a class and scrolls up or down or something.
If someone has the source to that it would be amazing.

But, I dont know how to load this file into a 2d array of ints. first, I dont know how to load files. And second, how would I load it and every time a , comes up then move to the next number in the array. I need the commas because I will have more then 10 tiles, so i can put 11,436,3 stuff like that.
Im very sorry, my grammar isnt very good.

Thank you!

Is This A Good Question/Topic? 0
  • +

Replies To: Reading a text file into a 2d array

#2 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Reading a text file into a 2d array

Posted 04 January 2012 - 11:22 AM

First thing you'll want to do is make sure the file is added to the Content project. After that, you'll need a StreamReader to read the file. A simple while loop checking to make sure that the file.ReadLine() != null should do the trick. Inside the while loop, it's just a matter of setting the current read portion of the file to the next element in the array. Given that you haven't supplied any code, I'm hesitant to offer code that solves the issue for you. Have a go at writing this and I'd be more than happy to help should you run into any issues.
Was This Post Helpful? 0
  • +
  • -

#3 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Reading a text file into a 2d array

Posted 04 January 2012 - 11:43 AM

After you've read a line of the file as Kilorn has suggested the next step is to parse a line. You can split the line into parts using the Split method of string using the comma to split the line.
Was This Post Helpful? 1
  • +
  • -

#4 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Reading a text file into a 2d array

Posted 04 January 2012 - 11:44 AM

Good catch there, SixOfEleven. I forgot to mention that.
Was This Post Helpful? 0
  • +
  • -

#5 Davis123987  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 25-September 10

Re: Reading a text file into a 2d array

Posted 04 January 2012 - 11:57 AM

Ok, thanks I researched stream readers and thought a bit and this is what I came up with:

            using (StreamReader streamReader = new StreamReader(MapName))
            {
                int x = 1;
                int y = 1;
                while (streamReader.ReadLine() != null)
                {
                    string line = streamReader.ReadLine();
                    string[] numbers = line.Split(',');

                    foreach (string e in numbers)
                    {
                        int tile = int.Parse(e);
                        MapLayers[currentLayer].SetTile(x, y, tile);
                        x++;
                    }

                    y++;
                }
            }



The only thing I think may be wrong is I am calling readline() twice so I may lose a line of text, since Im not saving the first one in a variable like I am in the second call. Any ideas?
Was This Post Helpful? 0
  • +
  • -

#6 Davis123987  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 25-September 10

Re: Reading a text file into a 2d array

Posted 04 January 2012 - 12:03 PM

Oh, and also, I really want to see the code for nick gravelyns tutorial. Does anybody have it?
Was This Post Helpful? 0
  • +
  • -

#7 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Reading a text file into a 2d array

Posted 04 January 2012 - 12:07 PM

Try changing it to the following:

using (StreamReader streamReader = new StreamReader(MapName))
{
    int x = 1;
    int y = 1;

    do 
    {
        string line = streamReader.ReadLine();
        string[] numbers = line.Split(',');

        foreach (string e in numbers)
        {
            int tile = int.Parse(e);
            MapLayers[currentLayer].SetTile(x, y, tile);
            x++;
        }

        y++;
    } while (!streamReader.EndOfStream);
}



View PostDavis123987, on 04 January 2012 - 03:03 PM, said:

Oh, and also, I really want to see the code for nick gravelyns tutorial. Does anybody have it?


I did have it at one time. I'll search my documents and see if it is still laying around. I have a couple sets of RPG tutorials on my site in my signature that might help you out with your tile engine.

*edit*
At the end of his tutorials he was using an XML document to save his tile maps. That would probably be an easier route to go for storing your maps. Just a thought for you to consider.

This post has been edited by SixOfEleven: 04 January 2012 - 12:09 PM

Was This Post Helpful? 0
  • +
  • -

#8 Davis123987  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 25-September 10

Re: Reading a text file into a 2d array

Posted 04 January 2012 - 12:10 PM

Thanks!

Sorry to ask another question, but would this approach be practical if im trying to load levels with approxamatly 1000 tiles in each direction? Think world of runescape big.

View PostSixOfEleven, on 04 January 2012 - 02:07 PM, said:

I have a couple sets of RPG tutorials on my site in my signature that might help you out with your tile engine.


Yes, Ive been reading your tutorials for a long time now. In fact I have the source to EyesOfTheDragon from the latest tutorial open in visual studio right now, Im trying to see how you did a lot of this tile stuff.
Was This Post Helpful? 0
  • +
  • -

#9 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Reading a text file into a 2d array

Posted 04 January 2012 - 12:13 PM

It might take a little while to load a map that contains 1,000,000 tiles. Another approach would be to break the map into multiple sections and loading those sections as needed, say for quadrants of 250 * 250 tiles. That is the approach that they took in the RPG starter kit.
Was This Post Helpful? 0
  • +
  • -

#10 Davis123987  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 25-September 10

Re: Reading a text file into a 2d array

Posted 04 January 2012 - 12:15 PM

View PostSixOfEleven, on 04 January 2012 - 02:07 PM, said:

*edit*
At the end of his tutorials he was using an XML document to save his tile maps. That would probably be an easier route to go for storing your maps. Just a thought for you to consider.


I was thinking about that too, but then I decided no as 1000x1000 maps may not work to well with xml. I hear that xml is very finicky at times, I have some experience with it in another game I made a few months back (never finished it).

View PostSixOfEleven, on 04 January 2012 - 02:13 PM, said:

It might take a little while to load a map that contains 1,000,000 tiles. Another approach would be to break the map into multiple sections and loading those sections as needed, say for quadrants of 250 * 250 tiles. That is the approach that they took in the RPG starter kit.


Ok, Il look at that starter kit.
Was This Post Helpful? 0
  • +
  • -

#11 Davis123987  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 25-September 10

Re: Reading a text file into a 2d array

Posted 04 January 2012 - 12:27 PM

I am having problems with my tile engine. This is what I have right now:

TileEngine > array of MapLayers and loads maps, I want to have all the layers in one file, but I think i may have to have a separate file for collisionlayer, object layers, and ground layer.

MapLayer > has a 2D Tile array and draws the tiles

Tile > has a tile index, the number in the text file

I think this is a really bad design I would like to do something better. If you look at the code I have its bad and would be hard to expand.

Im looking at your tutorials to see what I can learn.
Was This Post Helpful? 0
  • +
  • -

#12 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Reading a text file into a 2d array

Posted 04 January 2012 - 12:38 PM

I'm definitely going to agree with SixOfEleven here, XML would definitely be a great way to go. For the tile editor/engine that I've kind of toyed with making for a while, each tile is an instance of the Tile object. It has a sprite, a layer, a position, a passable bool, and possibly some other possible features. Using the IntermediateSerializer in XNA makes creating XML files that represent objects and even large collections of objects considerably easier. Then it's just a matter of reading in that XML file and creating an array of Tile objects from the data in the file, drawing the sprites in the proper location, and doing some logic for collisions with passable and non-passable tiles.

Example tile from an XML file that I would create with the above mentioned method:
<Tile>
  <Sprite>low_grass</Sprite>
  <Layer>0</Layer>
  <Position>1,1</Position>
  <Passable>True</Passable>
</Tile>
<Tile>
  <Sprite>low_grass</Sprite>
  <Layer>0</Layer>
  <Position>1,2</Position>
  <Passable>True</Passable>
</Tile>



In this example, all of the data that is required for every tile is saved into the XML file. Once the data is read using the Deserialize method of the XMLSerializer class, this data is automatically assigned to the proper properties for each instance of the Tile object.
Was This Post Helpful? 0
  • +
  • -

#13 Davis123987  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 25-September 10

Re: Reading a text file into a 2d array

Posted 04 January 2012 - 12:52 PM

View PostKilorn, on 04 January 2012 - 02:38 PM, said:

I'm definitely going to agree with SixOfEleven here, XML would definitely be a great way to go. For the tile editor/engine that I've kind of toyed with making for a while, each tile is an instance of the Tile object. It has a sprite, a layer, a position, a passable bool, and possibly some other possible features. Using the IntermediateSerializer in XNA makes creating XML files that represent objects and even large collections of objects considerably easier. Then it's just a matter of reading in that XML file and creating an array of Tile objects from the data in the file, drawing the sprites in the proper location, and doing some logic for collisions with passable and non-passable tiles.

Example tile from an XML file that I would create with the above mentioned method:
<Tile>
  <Sprite>low_grass</Sprite>
  <Layer>0</Layer>
  <Position>1,1</Position>
  <Passable>True</Passable>
</Tile>
<Tile>
  <Sprite>low_grass</Sprite>
  <Layer>0</Layer>
  <Position>1,2</Position>
  <Passable>True</Passable>
</Tile>



In this example, all of the data that is required for every tile is saved into the XML file. Once the data is read using the Deserialize method of the XMLSerializer class, this data is automatically assigned to the proper properties for each instance of the Tile object.



Ok. I was thinking a Tile class approach because each tile only have certain characteristics, I dont want that varied in the game, if a character expects water to be unwalkable then it should always be. Then a number or something should mean what tile it is, im not sure how im going to do that, maybe a dictionary of ints and tiles, you can just use an int and get a tile. Im just not sure yet, if you have any suggestions how to make my tile engine, please tell me.

Thanks!
Was This Post Helpful? 0
  • +
  • -

#14 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Reading a text file into a 2d array

Posted 04 January 2012 - 01:12 PM

Just to bring this up if you go with Kilorn's approach with a tile class then represent your maps with the tile as Dictionary<Point, Tile>. That can be serialized easily using IntermediateSerializer. It can also be iterated easily over and your maps drawn.

I still like the layered map approach to maps rather than a single layer map. However, I've moved from the idea of have a variable number of layers to a fixed number of layers with fixed functions. Either approach will work and it will end up being what works best for you, which you like better. I also use an interface for my layers so I can do something as follows:

public interface ILayer 
{
    void Update(GameTime gameTime);
    void Draw(GameTime gameTime, SpriteBatch spriteBatch); 
}

public class TileLayer : ILayer { ... }

public class CollisionLayer : ILayer { ... }

public class TileMap
{
    List<ILayer> layers = new List<ILayer>();    
}



This way it is easy to say add a layer that represents items in your world and the tile map will be able to update and draw it easily because the methods are implemented by the interface. It can also easily be added the map.

public class ItemLayer : ILayer { ... }


Was This Post Helpful? 0
  • +
  • -

#15 Davis123987  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 25-September 10

Re: Reading a text file into a 2d array

Posted 05 January 2012 - 08:44 AM

Ok, thanks. One more problem I have though. If I have a layer, when I initialize it I have to initialize the array of numbers, right? So then I need the maps width and height. If I load the map file how do I initialize the array without first having the array to see how big the map is?

I really dont want to use xml files, even though its a LOT easier to load, the map files will be much bigger then with using text files, or even a custom .map file thaht I can create in my map editor. I know how to make a map editor, I have before, but I just need to sort out my tile engine before I get to that.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2