Tilemap interaction like _on_body_entered()

Godot 4

Hi, I am trying to have a layer of a tilemap interact with my player where when the player stands on a tile in that layer it calls a function in the player movement script and I would need it to work like the area2d signal “_on_body_entered”. If something like this does not exist I would highly appreciate a workaround, one possible workaround I have thought of is having a seperate tilemap but even the I have no idea how to implement it. Thanks.

Sounds doable - what are you wanting to happen?

I’ve done a few similar things in my learning projects including changing the players walk speed and jump height.

If you’re interested I have an itch.io page where you can play my games.

Hi, I would like to have a tilemap which has 3 layers: a layer I collide with, a layer I interact with, and a background layer. In the meantime however I have found this video which achieves what I want however, I tthink that this solution would require me to have 3 seperate tilemaps, one for each layer, and I was wondering if there was a more elegant solution.

You can set custom data layers in tilemaps, such as a boolean for whether a tile is considered enterable. I don’t know if you can set custom polygons like when you set up collisions, I think it’s just fields per tile.

If you only need to know if a tile is entered, you could make a script which checks which cell is under the player. If the tile in that cell has that boolean set to true, and it wasn’t last frame, trigger a signal.

Is that to do with multiple physics layers?

No, this way wouldn’t interact with the physics system. It’s more manual and not scaleable :(.

Could you please show me how?

Read this reddit post

What do you need an area_intered signal from a tile for? This post doesn’t show how to do that, but rather a way to do something that might be what you need. It’s possible you can solve your problem a better way that trying to have a signal on the tilemap.

When my player goes over a certain kind of tile in a tile map I want to change a variable in the player movement code from 1 to 0.

Edit: I just saw that you linked to a reddit post which does what I want it to. The comment about physics layers intrigues me and I had thought of that as a possibility but I have not idea how to implement it.

1 Like

Okay, should be simple.

In the tileset, make a new physics layer. Set the collision layer to an available layer. Ideally name that layer so you can reuse it. Set the collision mask to nothing, as you don’t want the tile to search for nodes. It’s an inert thing that will be scanned by other nodes, such as the player.

Now go to the appropriate tiles in tile set and paint the collision polygon for that physics layer. You’ll just have to figure that out.

In your player, add an Area2D with an appropriate collision shape. You can’t reuse the movement hitbox, which has a CollisionShape, unless you want the tile to stop your player too.

Set the collision mask of that new area to the physics layer of tile. Keep the collision layer empty, as this area is scanning other things each frame and doesn’t need to be scanned. Now you can use the body entered signal however you like.

Your player is the thing that has to affect itself given external things. The Tilemap exists to be detected. Doing your code this way is easy to work with and testable.

Edit: use body entered signal, not area entered signal.

1 Like

Thanks a lot! However I should mention that the area entered signal wasn’t working so I went to the docs and looked at the area2d signals and found that the body entered siignal woulld work in his application. Thanks again for all your help!

1 Like

No worries!

Most of my work has two layers:

  • A background which has no interaction but fills out the screen
  • Walls, platforms, etc - which the player interacts with due to physics layers.