16 Replies - 3488 Views - Last Post: 05 January 2012 - 01:26 PM
#1
Reading a text file into a 2d array
Posted 04 January 2012 - 10:33 AM
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!
Replies To: Reading a text file into a 2d array
#2
Re: Reading a text file into a 2d array
Posted 04 January 2012 - 11:22 AM
#4
Re: Reading a text file into a 2d array
Posted 04 January 2012 - 11:44 AM
#5
Re: Reading a text file into a 2d array
Posted 04 January 2012 - 11:57 AM
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?
#6
Re: Reading a text file into a 2d array
Posted 04 January 2012 - 12:03 PM
#7
Re: Reading a text file into a 2d array
Posted 04 January 2012 - 12:07 PM
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);
}
Davis123987, on 04 January 2012 - 03:03 PM, said:
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
#8
Re: Reading a text file into a 2d array
Posted 04 January 2012 - 12:10 PM
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.
SixOfEleven, on 04 January 2012 - 02:07 PM, said:
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.
#9
Re: Reading a text file into a 2d array
Posted 04 January 2012 - 12:13 PM
#10
Re: Reading a text file into a 2d array
Posted 04 January 2012 - 12:15 PM
SixOfEleven, on 04 January 2012 - 02:07 PM, said:
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).
SixOfEleven, on 04 January 2012 - 02:13 PM, said:
Ok, Il look at that starter kit.
#11
Re: Reading a text file into a 2d array
Posted 04 January 2012 - 12:27 PM
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.
#12
Re: Reading a text file into a 2d array
Posted 04 January 2012 - 12:38 PM
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.
#13
Re: Reading a text file into a 2d array
Posted 04 January 2012 - 12:52 PM
Kilorn, on 04 January 2012 - 02:38 PM, said:
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!
#14
Re: Reading a text file into a 2d array
Posted 04 January 2012 - 01:12 PM
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 { ... }
#15
Re: Reading a text file into a 2d array
Posted 05 January 2012 - 08:44 AM
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.
|
|

New Topic/Question
Reply



MultiQuote







|