Saving Game Maps - best way

BlitzMax Forums/BlitzMax Beginners Area/Saving Game Maps - best way

Rico(Posted 2010) [#1]
Hi i am writing a level editor for a simple platform game. I have almost finsihed it now. I need to save the game map, which is stored as an array in the game editor program.

What's the best and quickest way of saving it so that it can be loaded up quickly by the main program. is it best to use the normal openfile method? (not sure the exact name of the command but was Open something)

Thank you very much for any help.


Thareh(Posted 2010) [#2]

(I hope I didn't misspell anything, wrote it directly in my browser)

Here's just a simple solution example, and it requires you to have all the tile images in an array.
You can save the actual tile images in there aswell, but it'll be slighty more advanced.


Midimaster(Posted 2010) [#3]
This is not an example how to use it with a tile list ( I did not read, that you use a TileList...) I understood, you working with a simple 2 dimensional array. so this could be a solution:




Czar Flavius(Posted 2010) [#4]
Thareh shows how to use the basic write/read functions, but you're probably not using a list.

At the start of the file put basic info, eg write two Ints, the x and y sizes of the map.

Now loop through each cell in the grid and dump any info about that cell into the file stream. Eg the first Int might be its tile (1=grass, 2=tree etc), next might be if any item is present on the tile etc.

To load a map, you need to read the data in the same order as you saved it. For the above example, first read two Ints in order to know the size of the map. Now you can create the array for the map, and know how much to read from the file. Now using for loops read the data for each cell in the order you saved it, and put it in the array.

Advantage of method: simply and easy to implement; small file size
Disadvantage of method: if you change the structure of your level, old game maps will become obsolete. For example, if you add a new field to each cell to say whether it contains a hidden trap, you need to change your write/read procedure and old maps will no longer be valid.


BladeRunner(Posted 2010) [#5]
Get ChaosClone from www.chaos-interactive.de
With this module , saving your array is done with this single line:
SaveUDObject(filename,array)

Loading is just the same:
array = arraytype[](LoadUDObject(filename,new arraytype[0]))

As an example:



Czar Flavius(Posted 2010) [#6]
That's very interesting!

Does it work with arrays of Types, or only basic data?


BladeRunner(Posted 2010) [#7]
You can pass almost every object to it. There are some restrictions (mainly no pointers), but they can be resolved with the included ability to use own loading methods.
There is a documentation on the website and also included in the Module.
If there are questions I will try to answer them. I am developing the module further on, there are some minor changes by now. I will release the next version as soon as possible (it will be compatible to this module, just add some functionality).


Rico(Posted 2010) [#8]
oh wow thank you for all the help, and that module sounds very useful. i will see if i can get it working. Does it work with multi-dimensional arrays? i assume it does...

Yes i am using a 2 dimensional array, but i guess i can put into a list for saving purposes if i know the dimensions would be quite easy to retrieve,

Thanks very much!


Czar Flavius(Posted 2010) [#9]
At a minimum you'll need to store in the file the number of cells in a row - so after X cells have been loaded, it will know to go to the next line!

You can split a multi-dimensional array into a single array - of arrays! :D Or a list of arrays - I believe arrays are treated as objects, and so may be stored in a list.


BladeRunner(Posted 2010) [#10]
It is no problem to store multidimensional arrays, TList, TBank, TMap, etc.


Rico(Posted 2010) [#11]
Ok thank you, i used Midmasters method since it seemed nice and easy. It worked very well! Thank you Midmaster. Very quick to load and save too.

BladeRunner i will definitely have a look at that module though for future use. Thank you