• 12
• 27
• 9
• 9
• 20

# how to load map from text file

This topic is 4840 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I know this has been asked amny times but the search function in the forums does not work I just keep getting sql search error. So any whay wat is a basic way to load a map array from a text file? I now can get my tile mpa to display using an array inside the source but now I want to make map files. so can any one explain how this is spose to be done? sorry for asking a question that is asked alot but like I said I cant search for it.

##### Share on other sites
You better start using binary files right away, they're smaller and easier to parse. Just write your in-code array to a file and write a new routine that can read it back. Afterwards you can start on writing your own exporter/converters for import of generic data.
Hope this helps.

##### Share on other sites
Quote:
 Original post by Prototype write a new routine that can read it back

this is the part I need help with. would I make to for loops the same way I draw them or ho do I get my tile function to read from my file read function or are they going to be the same?

##### Share on other sites
I strongly recommend you use a text based file format, at least to start with. Its much easier IMHO to be able to open the file in notepad to check/edit it, and its also easier to watch the parsing in your debugger and make sure everything is working.

file: map.txt
First line contains the width and height of the map. Following that are height lines, each containing width ints.
5 51 1 1 1 11 0 0 0 11 0 0 0 11 0 0 0 11 1 1 1 1

Now to parse it:
class Map{    int *   mTileData;    int     mWidth;    int     mHeight;public:    void SetTile(int x, int y, int data)    {        mTileData[(y * mWidth) + x] = data;    }    bool Load(const std::string & filename)    {        ifstream mapfile(filename.c_str());        if (mapfile.is_open() == false) return false;        mapfile >> mWidth >> mHeight;        mTileData = new int[mWidth * mHeight];        for (int y = 0; y < mHeight; y++)        {            for (int x = 0; x < mWidth; x++)            {                int temp;                mapfile >> temp;                SetTile(x, y, temp);            }        }        return true;    }};

Alan

##### Share on other sites
You just write out your data sequentially and read it back in the same way. When reading you can just increase a counter until the end-of-file is reached.
Even better is to add a header to your file with basic information such as # of items etc. Basically you will have your own file format this way.

##### Share on other sites
Quote:
 Original post by kingpinzsI know this has been asked amny times but the search function in the forums does not work I just keep getting sql search error.

You could always google just this site by typing site:www.gamedev.net [your search terms]. For example, site:www.gamedev.net XML will show all results from this site that mention XML. This is pretty useful as it'll search all forum topics, articles, journals, etc for the mention of XML. I've found it to be pretty handy on many occasions. I hope you find this useful :)

##### Share on other sites
sure

first of all if this is going to be a big map then you need to get a map editor that will export your map to text files if you want to know were to find one let me know. then you need a file parser whitch will skip over all the [] {}, crap that is no doubt in your array because none of those are ints. (or you could make a program to strip arrays of them like i did)
then just read in the file one int at a time modifying the string so that it dosnt contain any of the above mentioned chars (only if you dont want to had edit them) then create an ofstream to spit them back into a file making sure that there is a space in between the ints then when you want to load it again read the file back. this is all very confusing even to me so how bout some code

#include <iostream>#include <fstream>#include <string>using namespace std;int main(){    string reader = "";    string name = "";    ifstream fin;    ofstream fout;    string::size_type pos;     fin.open("bla.txt");    cout << "this new map will be called:";    cin >> name;    fout.open(name.c_str());    while(fin >> reader)    {    if((reader != ",") && (reader != "{") && (reader != "}") && (reader != ";") && reader != "}," && (reader != "};"))    {        pos = reader.find (",",0);        if(pos !=string::npos)        {        reader.erase(reader.length()-1,1);        };            fout << reader << "\n";       };     };    fout.close();    fin.close();               system("pause"); };

thats the write

and heres the read back in my project

int Map::Set_Equal(const char* SZ_FILENAME){    ifstream fin;    string reader = "";    fin.open(SZ_FILENAME);    for(int i = 0; i < 50; i++)    {        for(int j = 0; j < 50; j++)        {           fin >> A_Map[j];        };    };    while(fin >> reader)    {        if(reader == "END")        {            break;        };            Map_Images.push_back(SDL_LoadBMP(reader.c_str()));    };};

hope you can get what i mean[smile]

NOTE: that if you dont have spaces in between your number (not just commas spaces)this wont work

##### Share on other sites
I'll just explain the logic behind what every ones saying here in case your not 100% on what the codes doing.

The easiest way is to step through your data when writing it is to use nested loops.

The array that holds your level data in memory will look like a bigger version of this

(|/| = 1 array element)

|/|/|/|/|/|/|/|/|/|
|/|/|/|/|/|/|/|/|/|
|/|/|/|/|/|/|/|/|/|
|/|/|/|/|/|/|/|/|/|
|/|/|/|/|/|/|/|/|/|
|/|/|/|/|/|/|/|/|/|
|/|/|/|/|/|/|/|/|/|
|/|/|/|/|/|/|/|/|/|
|/|/|/|/|/|/|/|/|/|

you need to fill your horizontal cells sequentialy then move to the next row down and fill them sequentialy. i.e
fill Row 0 cells 1,2,3,4,5,6,7,8,etc
then fill Row 1 cells 1,2,3,4,5,6,7,8,etc.
then fill Row 2 cells 1,2,3,4,5,6,7,8,etc.
and so on.

this loop:

for(int i = 0; i < mapHeight; i++)// Increments i which we will use to refference the Array Row we want to write to.
{
for(int j = 0; j < MapWidth; j++)//This will increment j that we use to reffer to the array cell of the particular row we need
{
ReadDataFromFile to MapArrayAtCoord[j]; //reads data in and puts it in cell [j]
};
};

the loop will do this:
i = 0 -> j = 0 ->writes line from file to Array[Row 0][Column 0].

still inside the inner loop(it has to do it as many times as the Array has columns.) j is incremented while i stays the same.... so now it writes the next line from file to Array[Row 0][Column 1]. and so on until it has writen up to the end of the top row of MapArray elements.
It then drops out of the inner (j incrementing loop) and increments i. it starts again on the inside loop(resetting to 0) so now its writing to [Row 1][Column 0] then [Row 1] [Column 2] and so on until it fills up all the elements on that row then goes to the next row.

sorry if im insulting your inteligence with this but its always best to know what your codes doing rather than just patchworking it together from posted code.

##### Share on other sites
so this it what i am getting

first you opoen the text file

using
fin.open("text.txt"); fin.close();
OR
ifstream Mapfile("text.txt");

then need to read the file using

for(int y=0; y<45; y++)
{for (int x=0; x<25; x++)
{
mapfile >> map[y][x]; or map[y][x].tile;
}
}

##### Share on other sites

I can get it open but I iam having trouble reading that data into my array.

iam not sure how to