🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Levels & disk files

Started by
0 comments, last by Qoy 24 years, 6 months ago
I am making a level editor and level file format for my game, and I had a few questions about saving the structures to disk. Since one file is going to hold multiple levels, I thought it would make it easier to have a structure sort of like this for one level:
code:
struct Level{char levelNum, tileSet;int length, numEnemies, numPowerups;Tile* tiles;Enemy* enemies;Powerup* powerups;}

Of course, the pointers would be allocated as arrays when the level file is read in, using the length, numEnemies, and numPowerups variables. Would this still save to file correctly if I just wrote a Level object to file, or would I have to cycle through all the members? Also, for the enemies and stuff during the game, would it be better (faster) to use dynamically allocated arrays, or linked lists?

------------------
http://qoy.tripod.com

Advertisement
If you put your pointers in a structure like that, they'll only be written as pointers. When you re-load the game, what they point to is likely not going to be there anymore. If you already have counters for the number of items in those arrays, you can just leave the pointers as they are and save your tile/enemy data after you save this structure (since you know the number, just write that much data to the file). When you reload, read in your level structure, reallocate the pointers based on the number of tiles/enemies you need, then copy the data back into that memory from the file. If your tile/enemy pointers point to linear arrays, you can just use fread/fwrite to do all the data transfer.

Good luck

Starfall

This topic is closed to new replies.

Advertisement