_on_hit_box_area_entered area collision_layer method output doesn't match layers involved in collision

Godot Version

Godot 4.2.1

Question

I don’t understand the methods attached to colliding bodies. I have a Player Scene with and Area2D as a hitbox. The player hitbox is on layer 2. The Player has a mask set to layer 5 (Enemy Bullet)

when I connect _on_hit_box_area_entered it takes an argument called area. If i print area.collision_layer() to the terminal the collision layer prints a layer number that I am not using for anything, 8 in this case.

Similarly area.collision_mask() prints 16 which is also not used by anything.

Could anyone explain what these methods are actually printing? Why don’t they just print the layer or mask corresponding to the collision?

Blockquote
func _on_hit_box_area_entered(area):
print("LAYER: ", area.collision_layer)
print("MASK: ", area.collision_mask)
print(“PLAYER HIT:”, area)

Blockquote

collision layer and mask are bitsets, a binary number. In the editor it shows as values 1 through 32, but in the computer it is a single 32-bit number, so your bitset for the player is 0b10 and for the bullet is 0b1000, in decimal these numbers are 2 and 8. The latter (mask) matches what you are finding the bullet’s layer to be.

If you had multiple masks or layers set then the binary number would have more 1s, like layers 1 and 4 would be 0b1001 in binary, and 9 in decimal

1 Like

Thank you for the info.

I’m sure you must be right but isn’t that output less useful from a development point of view?

Wouldn’t it be more useful to print the collision layer that the developer sets instead? Or maybe there is another use for this information that I’m not aware of. I’m pretty new to programming games.

In the project settings you can name these layers if you need it to be more easily read.

1 Like