A File I/O Question

Started by
13 comments, last by PepsiPlease 21 years, 9 months ago
Hey,

I had the same question as well on reading maps. I am doing a top down puzzle game. I want to have a level editor. Now that I know how to load the levels, that helps a lot, but how do I save a level that is made in the level editor? My levels are going to be stored in numbers (an array)... I will define the numbers as stuff like LEVEL_FLOOR for easy refrence. How would I go about saving a level I have made (I think I asked that twice... anyway)

- Thanks
Thanks
Advertisement
Hello,
Well, its pretty much just the reverse of loading a file, but here is a code example...Hope it helps


  ofstream outfile("myMapData.txt",ios::out)//replace myMapData.txt with whatever name you want...if(outfile == NULL)//error could not create file for whatever reason   outfile.close();for(int x=0; x<mapWidth; x++)   for(int y=0; y<mapHeight; y++)      outfile << myMapDataArray2D[x][y];outfile.close();  

Now check your file to see if things output correctly...
If you have any other questions let us know.
Have fun.
Here's a new dance everyone can do, do the Kirby:<(*.*<) <(^.^)> (>*.*)> <(^.^)> <(*.*<) (>^.^)>
quote:
If these numbers are all 0s and 1s, it''s a better idea to use bools or chars to make your program a little bit more efficient. I''m an efficiency freak :D.


Just thought id mention that the original poster did in fact use chars.
Thanks a lot for the help. It''s working pretty good now. Now, I have another question now.

All my levels are the same size. They are now each stored in a 30x30 array. A 0 is nothing, a 1 is a wall, and a 2 is a window. Now, when I get to the end of the first level, I want to load the next level, but do I have to have a routine like this for each one?

What I''m trying to say is, do I keep having to write this code for each level, or can I have the same code for all the levels?


Thanks
If your levels are stored as different files simply pass the filename to your loading function. If on the other hand you wish to store them in the same file, you need to move about and find the level you wish to use. If all your levels are the same size then you do not need to worry about an index, or some other form of telling where a level end and begins. You simply have to move past N numbers of chars.

To load a level N
mapsize = 100*100 (chars);
filepointer = (N*mapsize);
....normal loading function ie read mapsize chars into array.

,Jay

This topic is closed to new replies.

Advertisement