How do I check what collision layer the tile i collided with was in [without extra area2d hitboxes]

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

func _on_area_2d_body_entered(body):
if body.get_collision_layer() == 7:
health -= 1
is_being_hit = true
elif body.get_collision_layer() == 4:
velocity.y = JUMP_VELOCITY * 3

How can I make this work
currently body is a tilemap, not the individual tile which is what i need

:bust_in_silhouette: Reply From: godot_dev_

I beleive you could use bit operations to acheive what you are trying to do, because the bits in the collision layer and collision mask are used to resolve collisions. For example:

 #shift the 1 (which is currently on the 1st bit) to be in the desired position (7 or 4)
var bit7Mask = 1 << 6 #i think its 6 (7 -1), because the 1 (0000 0001) is already in 1st bit's position
var bit4Mask = 1 << 3
#bit7Mask = (0100 0000)
#bit4Mask = (0000 1000)

Now that you have define the mask, you can use it in your if statement conditions by comparing the mask to the collision layer of your body using the bit-wise and operator &. The below code should do the trick, if I

func onarea2dbodyentered(body):
    if (body.getcollisionlayer() & bit7Mask)   == bit7Mask :
        health -= 1
        isbeinghit = true
    elif (body.getcollisionlayer() & bit4Mask  ) == bit4Mask :
        velocity.y = JUMPVELOCITY * 3

I recommend computing the masks only once to avoid recomputing them each time onarea2dbodyentered is called

Keep in mind, if one of your bodies has both the layer 4 and 7 bits enabled, your logic may bug out