# How can I create a flexible system for tiling a 2D RPG map?

Using libgdx here. I've just finished learning some of the basics of creating a 2D environment and using an OrthographicCamera to view it. The tutorials I went through, however, hardcoded their tiled map in, and none made mention of how to do it any other way. By tiled map, I mean like Final Fantasy 1, where the world map is a grid of squares, each with a different texture.

So for example, I've got a 6 tile x 6 tile map, using the following code:

Array<Tile> tiles = new Array<Tile>();

//... x5 more times.


Given the random nature of the environment, for loops don't really help as I have to start and finish a loop before I was able to do enough to make it worth setting up the loop. I can see how a loop might be helpful for like tiling an ocean or something, but not in the above case.

The above code DOES get me my final desired output, however, if I were to decide I wanted to move a piece or swap two pieces out, oh boy, what a nightmare, even with just a 6x6 test piece, much less a 1000x1000 world map. There must be a better way of doing this.

Someone on some post somewhere (can't find it now, of course) said to check out MapEditor. Looks legit. The question is, if that is the answer, how can I make something in MapEditor and have the output map plug in to a variable in my code? I need the tiles as objects in my code, because for example, I determine whether or not a tile is can be passed through or collided with based on my TileTyle enum variable. Are there alternative/language "native" (i.e. not using an outside tool) methods to doing this?

I believe that Tiled Map Editor (TME) can create files of different types, however the main feature that caught my eye was it's ability to export to JSON files.

JSON is somewhat similar to XML and you will find good supporting APIs for parsing JSON files in Java. See here.

Once you have created a map in TME you will be able to export it as one of these JSON files. Then you will be able to load and parse that file to create java objects. You might find it convenient to parse to some intermediate set of java objects, then extract from them only the information you want and use it to make YOUR tile objects.

See this question for some more information. One answer mentions this tool which might help you out. This tool will allow you to generate the Java Object equivalent to the JSON objects created by TME.

• +1 for recommending TME & GSON, which is a great library for creating Java objects from JSON. However, it may be expensive due to having to use reflection, but usually worth it . – Cooper Aug 17 '12 at 18:08

A map editor of some sort is what you are going to need (and want!) when your game moves more to the level design phase from the initial engine/coding phase.

If you are using any kind of map/level editor, you will need to code a class (or set of classes) that can:

2. Translate the data into actual objects (and sets of objects). Your tiles look simple now, so the map file would just be X, Y pairs with a tiletype name you can translate into a Tile enum.
LibGDX has now added support for loading and rendering Tiled Map Editor's own .tmx file format. This should most certainly be your preferred method of loading and rendering TME created tile maps in LibGDX now, rather than having to manually parse through the JSON/XML data yourself.
Link to LibGDX tutorial on using .tmx tile maps.