Is it possible to paint collectable tile on the TileMap?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By alexzheng
:warning: Old Version Published before Godot 3 was released.

I thought define a tile with sprite and a body as a brick, when a player hit the brick, it will get the body and I can remove it easily, however I was wrong, what I get was the TileMap,How can I remove the tile that collide with player?
Thanks.

Another more straightforward example, place coin tiles on the TileMap, is it possible to make it pickable?

alexzheng | 2017-05-12 00:02

I checked the platformer demo, and the TileMap is only used for paint static tiles such as ground, other objects are placed by scene instances.
I think if these objects can be defined as tiles then place them on the TileMap will be much easier.
Since each tile is defined as a sub scene, why we can not just treat the TileMap as a helper container for placing these scene?

alexzheng | 2017-05-12 02:44

Is your player a rigid or kinematic body? And I think what you need to do is get the collision information as a position (perhaps using contact reporting?), and then use the TileMap#set_cell(x, y, -1) call to clear that cell.

Max Aller | 2017-05-12 03:22

Thanks for your reply.
My player is a kinematic body, my request may be beyond just clear a tile when collided.
I need to know what kinds of body the player hit, and call some method of that scene.

alexzheng | 2017-05-12 03:53

Still not 100% sure what you mean by painting a tile (are you adding a tile or removing one? unclear), but if you look at player.gd file in the Kinematic Character demo, there’s a line that mentions

# You can check which tile was collision against with this
# print(get_collider_metadata())

Might start looking there if you haven’t already.

Max Aller | 2017-05-12 03:57

painting a tile I mean adding a tile to the TileMap, it’s much easier than adding lots of scene instances.
Thanks, I will check this api

alexzheng | 2017-05-12 06:00

:bust_in_silhouette: Reply From: MrMonk

changing a tile when you collide with it:

var tile_pos = ray_down.get_collision_point()
var tile_index = tilemap.world_to_map(tile_pos)
tilemap.set_cell( tile_index.x, tile_index.y, TILE_BRICK)

ray_down is a RayCast2D attached to the player, TILE_BRICK could be whatever tile you have in your tilemap. If TILE_BRICK is -1, the tile will just be deleted and not replace with anything.
For collectibles better use a staticbody2d instead of a tile in a tilemap, but, following the above example, you can also use a tile in a tilemap, there are many drawbacks though

Thanks for your code!
yes, it’s more straightforward to just use a static body, although place them maybe cost some more time.

alexzheng | 2017-05-12 13:26