Tilemap, custom tiles and pathfinding

My 2d strategy game is tilemap based (think Civilization) What I would like is to have different tiles each with its own functionality and attributes that I can edit from the editor. For example, some tiles could be different types of buildings. Is it possible to add this kind of logic to the built-in tilemaps?

My second question is there is this pathfinding library called Jumper Can I implement it with the built-in tilemaps in Defold? I reckon all I need is a table of walkable tiles values. Or maybe there is another pathfinding solution you could recommend?

1 Like

What you want is possible to do.

You have to store this data within a table where you associate the table cells with what you display in the tilemap.

So for each x,y cell that can be a table value id.

Or do something like
world_data[x][y] = {}
And store the cell’s data in that x,y table

It would require you to loop through all values of x and give them a blank table value in y when you init.

There is an astar path finding example in this set of examples

1 Like

Thanks for the reply.

I’m not quite sure I get what you mean. Are you saying that I need to create a new table, that table holds value tables that each one of them represents a tile on the map?
If so, then this would be kind of a tedious process. First, I draw my map in the editor, then I manually insert the x,y of each tile into a table in order to store its values, and when I change the position of certain tiles I need to manually do that in my table?

What I wanted was a way to attach a script to each tile with its own functionality and edit its values from the editor. Kind of like a normal GameObject

1 Like

The table data is the representation of your simulation which you display on your tilemap. So when you update a tile’s information within the table you would use a function to also update the graphic of that tile if necessary. You can have multiple layers within your tilemap too and handle those layers within a single x,y cell of the table.

It is possible to attach behaviors to individual cells but you have to go about it differently than with GOs. You need manager scripts which loop through all cells and handle behavior within all of them depending on what their current properties are along with the rules of your game.

You could use GOs too, you don’t have to use tilemaps. Then you could attach a script to every GO block/tile.

So right now my options are:
1- Use code to configure each tile one by one.
2- Use GameObjects and manually draw a grid in the scene and manually give each one its ( x,y) values. And this would require a table of all tile GameObjects to work with in code and also to feed it to the pathfinder script.

You should try option 1 first.

Option 2 would let you have infinitely expanding worlds too. You can use negative ranges for positioning cells like I did in this https://www.pixcade.com/cosmos/web/ Where the compass points is 0,0. All of the clicking on blocks in this and the behavior is done by a single manager script.

Having your world simulation data all in one place will make dealing with the simulation much easier. Fragmenting the data over hundreds of GO scripts for each tile would be harder to deal with, and you’d still need a central index.

In option one I would have to do something like this, right:

tiles[0][0] = {buildingType = “”, numOfEnemies = 2, loot = 45, isWalkable = false}
tiles[0][1] = {buildingType = “”, numOfEnemies = 2, loot = 45, isWalkable = false}
tiles[0][2] = {buildingType = “”, numOfEnemies = 2, loot = 45, isWalkable = false}

And so on until I configure all tiles? so a basic 10x10 grid would require me to write 100 lines of code to do this.

Yes, like that, but you would do it through loops. You can setup the tables similar to this, and then put whatever data you want in the cells.

local tiles = {}
local width = 10
local height = 10
for x=1, width do
	tiles[x] = {}
	for y=1, height do
		tiles[x][y] = {value = math.random()}
	end
end
pprint(tiles)
print(tiles[2][4].value)
print(tiles[10][10].value)

You could code a custom level editor for your game’s unique data too. This editor would run in engine and be able to save / load to a directory you specify. Think of like an editor other strategy games might have.

Or use something like Tiled and then export .lua files with it. I believe it allows you to specify extra data to cells which you can then import through lua. Here’s an old example which may help Big List of Defold Pro Tips!

A custom editor may be the way to go though. It’s not as hard as it may sound!

I’ve never had the need to do this myself, but if I ever create a tile-based game where I need to edit maps and embed a lot of meta-data in the map then I’d probably use Tiled.

Is there any kind of Tiled imported script for Defold that also supports custom properties? The official website doesn’t have any mention of defold.

Defold export is available as a plugin that needs to be enabled in the tool (preferences or something like that). I believe Tiled can export the additional meta-data as both JSON and Lua, both of which are very suitable for use in Defold.

For Hammerwatch Coliseum I created a wrapper around Jumper that takes a tilemap and creates a Jumper Grid from it. If you are interested I can probably dig it out and share the source, so give me a shout if you haven’t found another working solution.

3 Likes

Ok, I’ve created a map in Tiled and exported both defold file .tilemap and a lua file. How do I import them into defold and work with them?

The .tilemap file should pretty much work fine, just like a .tilemap you make with the Defold editor, except you will need to set its “Tile Source” in the properties panel after exporting it from Tiled.

The exported Lua file is just a normal Lua table. When you require it it will return a big table with all of your data.

If I understand your problem correctly, you probably want to export the tile set to Lua. Inside that there will be a ‘tiles’ table with the tile ID numbers and any properties you have set.

...
  tiles = {
    {
      id = 84,
      properties = {
        ["Name"] = "Bob"
      }
    }
  }
...

That’s accurate. Only one problem, When I create an “Object Layer” in Tiled it doesn’t show in defold. A normal tile layer does show, but an “Object Layer” doesn’t, even though I have the same image source in both Tiled and defold, and not just the assets, but doesn’t even show in the outline.
I’m not sure if I’m missing something here, but I need the object layer because this is the one that allows me to manipulate data for each object individually.

Object layers will not show in the Outline. We do not support object layers. But you get all the data from an object layer as a .Lua file right? Require it and use it in your code to create the objects from a script.

1 Like

Thanks, but, I’m going to think of an entirely new approach to this.