- Home /
Save system for a Motherload-like game.
My game automatically generates the world at startup by using pre-placed gameobjects which turn themselves into something or entirely disappear so every earth chunk has the same name (like Dirt, Iron(clone) e.t.c.).
I want to make a save system which basically saves the player's money, location, inventory, fuel, health and the world(earth chunks, not shops and stuff). What is the best way to do this?
Like $$anonymous$$ecraft ? And do you prefere to save that in an database or simple text file ?
Answer by ryba · Feb 03, 2013 at 02:45 PM
Let me guess, minecraft copy ? Is your world just giant set of cubes ? For such tiled worlds saving map is quite easy, but it also depends on how big your world is, because if there is more data to save, you should do some optimization.
Minecraft world is divided to chunks and blocks. Chunk is small set of blocks. I dont remember how big is it, but it has static size, lets say 20x20x100 (100 is hipotetical height from bottom to the sky - as i said i dont remember those values).
So chunk is set of 40000 blocks. When you enter game, it loads chunks that are near player. Amount of chunks depends on player settings (render distance).
Lets say player has low distance, which means that game will load only chunks in range of 3 (manhattan distance), like that (d is distance):
Your database model should be designed like that:
chunk_id (int, index, not null) - index of chunk that block belongs to, it must be index for fast finding
id (int, index, not null) - index of particular block
block_type (int, not null) - enumeration of your block type (dirt/iron/stone etc)
Make sure you wont do id columns as unique, because magic is that they should repeat.
If your chunk is 20x20x100, and contains 40000 blocks, than every block should have index from 0 to 39999. Why is that ? Because block id should relate position in chunk, not in whole world - you have chunks for that. Now when you calculate indexes of all your chunks, you do simple
select * from blocks where chunk_id=id1 or chunk_id=id2 or chunk_id=id3
etc for each chunk.
I think you should get the idea right now. For saving other data, you can simply save it to the another simple key-value table.
That solution will be quite fast, because with such chunks you wont have big amount of indexes. If your world will be 2000x2000 wide (its really big assuming block size is like in minecraft), then you will have only 10000 chunks, and search engine will have all those blocks sorted in 10000 data clusters though there is 400000000 blocks !
If you dont want to use database, you can store each chunk in separate file, named by its index, and containing serialized set of blocks, or even csv (this will be too easy to access to player however, he will be able to edit his generated world, for example add an valuable blocks, like diamonds everywhere :P), example:
chunkId,blockId,blockType
1,1,4
1,2,4
1,3,4
1,4,4
2,1,4
2,2,3
2,3,4
No ,the game works differently, by chunk I meant a single block.
I was looking at asset store and found something interesting(free ^_^). I will try it out.
Your answer
Follow this Question
Related Questions
Save multiple scriptableObjects in just one binary file 2 Answers
im getting a serialization exception eror in my load method 0 Answers
Help with serialization 0 Answers
Save and load serialization problem 0 Answers