Handmade Network»Forums
8 posts
How to store a 2D world in memory and on disk
Edited by ionut on Reason: Initial post
I'm thinking about making a 2D side scroll platformer (handmade). I've tried looking around but I haven't a good explanation about how one might go about storing the world in memory (also saves on disk)

A rough idea I have at the moment is have the world mapped into some square chunks (which may be tiles). Choose a minimum "unit" say however many pixels in a 4k monitor and divide the chunk into those. Store game object position as a struct consisting the X/Y of the chunk ad then the X/Y inside the chunk. Would it be better to store it as floats, and what is which datastructure/algorithm should be used with floats as they can't be array indexes.

For storing on disk, is it just a question of writing all of the above (along with the current state) in a file? (now that I wrote it down, I assume the answer is yes)

I would appreciate any idea/link/info
Simon Anciaux
1341 posts
How to store a 2D world in memory and on disk
It depends on the game you are making. I suggest you do the most simple thing (store everything as an x/y offset from the origin) and see where it leads you.

For storing to disk, you can start with just dumping the memory to a file and reload it. If it doesn't contains pointer it should work. But if the data layout changes, which will most likely happen as you develop the game, you will not be able to just read the file into memory and expect it to work. So you'll need to write the data in a format that you can parse and doesn't depends on the memory layout.