First off, I’m very new to Godot, I’m learning the ropes.
So I have a CharacterBody2D Node (Player) and a CollisionShape2D inside of it. I have it walking around a TileMap. My TileMap has 2 Physic layers, one with a layer and mask to 1, and the other has layer and mask set to 2. In my Player Node I also set up a Area2D with it’s own CollisionShape2D. This Area2D has a signal setup to tell me when it hits a body, specifically the TileMap. I want it to act like a trigger to when it collides with the TileMap.
This all works fine, but my problem is now trying to get more info from the body of the collision/signal. Specifically I want to get the TileSet’s Physics layer from the Area2D. How can the Area2D tell me which TileSet physics layer it collided with?
Just testing if this is an XY problem, what are you trying to solve?
Otherwise a workaround is to duplicate the areas and have the first area collision mask set to 1 and the second area’s collision mask set to 2. Distinguish between them by connecting different signal handlers or binding an argument of the layer it represents to the signal handler.
I am not sure if this is the correct answer but you may want to look at this function:
int get_layer_for_body_rid(body: RID)
Returns the tilemap layer of the tile for given physics body RID. Such RID can be retrieved from KinematicCollision2D.get_collider_rid(), when colliding with a tile.
It would be something similar to this:
var layer= tilemap.get_layer_for_body_rid(collision_node.get_collider_rid())
If you also need to retrieve the tile, you may want to do something like this:
var coords = tilemap.local_to_map(collision_node.position)
var tile = get_cell_alternative_tile(layer_id,coords)
I’m trying to do what you do. However, I could not find a way to get the TileSet physics layer from the collision information.
What I ended up doing is query directly the PhysicsServer2D.
for i in get_slide_collision_count():
var collision = get_slide_collision(I)
var rid = collision.get_collider_rid()
var friction = PhysicsServer2D.body_get_param(rid, PhysicsServer2D.BODY_PARAM_FRICTION)
In this way, my code does not need to care about the collider’s type and directly get what I need from the source.