What's a good way of storing game level data?

I'm working on a little tile map engine and so far i'm defining all the objects programatically, so now i'm considering how to store them on disk.

I have four types of objects: tiles, entities, layers, and maps. Layers contain either tiles or entities. Maps contain an array of layers and do the drawing and scrolling. Tiles and entities are just graphics, but entities have position and movement properties.

So what's a good way of defining these objects in files? Some way that's easy to edit by hand and fairly easy to parse?
If you're just looking for a data format, give JSON a try: https://en.wikipedia.org/wiki/JSON .
If you're just looking for a data format, give JSON a try: https://en.wikipedia.org/wiki/JSON .


JSON is pretty nice, it works really well if you have a scripting system with something like Lua set up as well.

Also, using something like Tiled can make creating maps for your games MUCH easier.
Like others have said you are probably best off using a standardized file format like JSON or XML. The main reason being ease of use and readability. Using a format like those will allow you to easily modify map data without having to open up the file in a specialized program made for altering it. If you are making just a tile map editor tool like Tiled then you might want to consider supporting multiple different formats also.

My personal preference would be to use XML with a specialized XML Schema made for your specific implementation of a tile map. This will give you a standardized way of defining a tile map and validation of that data will be easier. A good example of this would be the TMX format which Tiled uses ( http://doc.mapeditor.org/reference/tmx-map-format/ ).

Otherwise if you are concerned about load speeds you could always go the binary route which will indeed have faster load times. Though a binary format also has its downsides some of which are they are downright annoying to debug, they don't sit well with versioning software and can't really be altered through standard text editors. Though personally I wouldn't recommend using a binary format unless you have good reason to do so, the load speed increase they give won't be that noticeable in most cases.
Last edited on
I would second JSON, the format is simple, there are relatively few things that can go wrong, and it gives you extra flexibility to add whatever parameters you want to add later.

XML is good as well, of course, but JSON is much much easier to write an interpreter/exporter for (all of the additional things that xml can have make it more difficult </br> <!DOCTYPE> <!-- comments--> <p>nested<p>elements</p></p> etc)

I've just written a universal interpreter for unrelated purposes, and xml was much more difficult than json.
@Zereo

It's also worth mentioning that it's going to be harder to keep your files portable using a binary format, should the OP have portability in mind.
Thanks guys. JSON actually looks perfect for this. I got json-cpp and it's very easy to use as well.
I'm having some difficulty coming up with a scheme for storing these objects with JSON. My first idea was to have an array of objects of type "tile-layer", "object-layer", and "map-data", but the file ends up very large. For example I typed this up, which would be a small fraction of a much larger map:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
[
    {
        "type":"tile-layer",
	"name":"forest_bg0",
	"tileset":"forest_tiles",
	"size":[16,16],
	"position":[0,0],
	"velocity":[0,0],
	"cells":[[01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01],
		 [01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01],
		 [01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01],
		 [01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01],
		 [01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01],
		 [01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01],
		 [01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01],
		 [01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01],
		 [01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01],
		 [01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01],
		 [01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01],
		 [01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01],
		 [01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01],
		 [01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01],
		 [01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01],
		 [01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01]]
    },
    {
        "type":"tile-layer",
	"name":"forest_bg1",
	"tileset":"forest_tiles",
	"size":[16,16],
	"position":[0,0],
	"velocity":[0,0],
	"cells":[[12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12],
		 [12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12],
		 [12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12],
		 [12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12],
		 [12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12],
		 [12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12],
		 [12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12],
		 [12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12],
		 [12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12],
		 [12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12],
		 [12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12],
		 [12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12],
		 [12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12],
		 [12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12],
		 [12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12],
		 [12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12],

    },
    {
        "type":"object-layer",
	"name":"objects",
	"objects":[["skeleton", 0, 0], 
		   ["skeleton", 20, 20], 
		   ["zombie", 16, 08]]
    },
    {
        "type":"map-data",
	"name":"Forest"
	"style":"staggered",
	"layers":["forest_bg0", "forest_bg1", "objects"]
    }
]


So how is my approach here? How would I accommodate much larger maps?
Last edited on
You say the file ends up very large, what exactly is large?

Saving data in a text format usually is going to lead to larger file sizes, just a sad truth. If you're worried about file size, remember you should save the minimum amount of info you need in order to recreate the map.

I believe Tiled may be a good place for you to get some inspiration. It can save decently sized maps to a text format with a small amount of kilobytes.

If it continues to be a issue, you may have to look into a binary format. This is trivial to do if you're not worried about being size and endian-safe, but if you want the map to be portable across different machines then that is a concern. Hopefully you don't have to go there.
You say the file ends up very large, what exactly is large?


I just mean the amount of text and how unwieldy it can get. I realize I should probably not be editing maps this way though so I might take the suggestion to use Tiled. I started writing the map loader and i'm getting more comfortable with JSON now.
Last edited on
Yeah, editing the map tile by tile in a text editor is going to be unwieldy no matter what really.
I had a similar problem when I was making a game which used levels like that. What I basically did is made a program that read the files and displayed them like the game does, but with a very simple cursor that could be moved from block to block with arrow keys, scroll keys to move from room to room and shortcuts for editing the square which the cursor is over. It greatly sped up my level design time and meant I never had to look at the huge arrays I was creating. This was made easier for me because I had a sprite assigned to each number, so I could get it to easily display the levels.
What shadowmouse is trying to say is, make a level editor. It doesn't have to be great, it just has to make your life easier.
Last edited on
Thanks again everyone. I really enjoy JSON :)
Topic archived. No new replies allowed.