Hello! Sorry but again concept question from my side..

So, started simple RPG 2d (top down), using TileMaps. Created a level, setup a collision areas with Physics Layers for some of tiles from tiles set. So, my hero (CharactedBody2D) walking & collecting trees (well, do not ask why trees 😉. Anyway. So, needs to track collision with trees (and they are diff colors so logic will be different), and understand - color of trees & what is really trees, not stones & other things I put in tile map. Logically, somehow I would put to any tile in TileSet a like 'group name' -- taking into account, I already setup a collision shape in Physic layer of TileSet -- (different for each color & object type - now this is just trees, in future 100+ diff type of objects from tileset), and just check with what exactly tile hero (object) collided..

And after day of googling/ trying/ testing, I found only one solution for understanding how to deal with this - to add to every tree tile below TileMap an Area2D+CollisionShape2D, and have almost 50 (I have a near 50 trees in my map) functions like _on_tree_N_body_entered(body) - to track collision..

So, could you please advise, is any other NORMAL way to track collisions within TileMap node?..

Thanks in advance!!

  • Hi.. Well, solved it, put on Custom Data Layers "ground_type" trees color as int

    Hero:

    @onready var tiles : TileMap = get_node("/root/MainScene/tilemap_MainScene")
    
    func _input(event):
    	if event.is_action_type():
    		var clicked_cell = tiles.local_to_map(tiles.to_local(position))
    		var data = tiles.get_cell_tile_data(0,clicked_cell)
    		if data:
    			var tt = data.get_custom_data("ground_type")

    So i can see where is my Hero & what is Title with it now) Super!
    Thanks!!

Yes, there is a normal way to track collision within TileMap node, and this is a fairly common type of situation that game developers encounter. The key is to add an invisible collision layer to the TileMap and assign collision area objects to particular tiles types in that layer. That way, the tiles themselves can act as physical objects, allowing your CharacterBody2D to detect when it is colliding with a tree.

You can use the TileSet editor to assign collision shapes to any tiles that need to be physically interactive in your game. When you design the level, you can then assign the correct tiles to the appropriate areas in the collision layer. In this way, your CharacterBody2D will be able to detect collisions with objects in the TileMap as appropriate.

Hi.. Well, solved it, put on Custom Data Layers "ground_type" trees color as int

Hero:

@onready var tiles : TileMap = get_node("/root/MainScene/tilemap_MainScene")

func _input(event):
	if event.is_action_type():
		var clicked_cell = tiles.local_to_map(tiles.to_local(position))
		var data = tiles.get_cell_tile_data(0,clicked_cell)
		if data:
			var tt = data.get_custom_data("ground_type")

So i can see where is my Hero & what is Title with it now) Super!
Thanks!!