How can I add a tile by clicking in the game?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Parkurist

I want to add building to my game, so I need to have tiles that change when you click on their spot in the game.

Are you using a TileMap?

exuin | 2021-04-27 00:48

:bust_in_silhouette: Reply From: BakouKai


Your title states something different from your explanation. Do you need to add a tile or to change a tile?

Changing a tile is quite easy, you just need to get the coordinates of your mouse on a click event. With world_to_map() you can convert it to your tilemap and get the corresponding cell. You can now change the cell.

On my projects, I build my tilemap based on a dictionary (each cells has properties, like which building is on it, etc.). So I only have to get the cell coordinates, update the dictionary on the corresponding cell, and reload the map.

Adding a tile is something I have not done yet, but it seems doable : you can get_used_cells() check is the click event is on an existing cell or not. If not, you can add the cell definition in the dictionary on which you can base your tilemap for instance, and add properties.

Hope it helps.