# How to build TileMaps dynamically from a database?

The game I'm building allows users to create their own tile maps essentially, so I want the server to respond with tilemap coordinates if / when a user enters their custom world.

• This is broad. You need an UI for the user to build them, a format to store them, a protocol to exchange them, and of course you need to rebuild them on runtime. – Theraot Apr 22 at 0:23
• Sorry, it had more detail but it was for some reason edited. I just wanted to know if it's possible in, Godot specifically, to load in tiles using code – James Nixon Apr 22 at 0:43
• What specific barrier have you encountered so far that makes it seem like it might be impossible? If you edit your question to describe that obstacle, we can help you overcome it. If you haven't encountered such an obstacle yet, then you should just keep working on your implementation until you either succeed or encounter a problem you need help with. – DMGregory Apr 22 at 0:54
• I can see the Godot tag, that is not the detail I'm looking for. And, yes it is possible. However, that isn't a very helpful answer, is it? I'd rather you break down the problem. I haven't done networking with Godot, so I would rather avoid that part of the question. – Theraot Apr 22 at 1:19
• Perhaps if you look for tutorials on procedural generation of TileMaps with Godot, that will give you an starting point. Yes, that is not what you want, don't let that stop you. By breaking the problem you'd see that the part about building the tilemap on runtime is very similar. – Theraot Apr 22 at 1:20

## 1 Answer

my main issue is storing the data for complex tilemaps.

You can think of tilemaps as arrays. So, you can just use an 2D array of numbers, where each number on the array represent a kind of tile.

If you want to serialize an array, for local storage or for network transmission, you can start by placing the dimensions (unless these are standard), followed by the value of each tile, in binary format, going row by row.

To deserialize, given that you know the dimensions, you can break the stream of tile values into rows. Knowing the number of columns tells you where it should end.

A couple notable alternatives to this approach are:

• Storing the tile values as characters in a string. If you use new line to mark the end of a row, it will be easy to edit and visualize in virtually any text editor.
• Storing the tile values as pixels in an bitmap image. This time you can visualize and perhaps edit with an image editor. Plus, you can seamlessly take advantage of lossless image compression such as the one provided by png. Plus, if you need to upload it to a shader, it is easy to use as a texture. Although, that is not your use case. See the Godot class image.

Now, if your tilemap is sparse, or if it is too large. There might be value in breaking it into chunks, and having a larger structure.

Some solutions: - An array of chunks. Very easy to query. - A quadtree of chunks. Will use less memory for sparse tilemaps. - Just a list of chunks with their coordinates. Easier to transmit.

Speaking of ease of transmit. You might want to have the game request chunks on demand based on the coordinates of the player. And those chunks can be png files. Well, how easy that is depends on your server technology.

If you also need to place other kinds of objects on the map, you may add them to same structure.

As you probably has seen in the documentation, you can use set_cell to write to tilempas in runtime. See How do I programatically lay tiles with the Godot engine?.