How to use set_collision_layer_bit (or mask)?

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

The docs for Area2D state

void set_collision_layer_bit( int bit, bool value ) Set/clear
individual bits on the layer mask. This makes getting an area in/out
of only one layer easier.

void set_collision_mask_bit( int bit, bool value ) Set/clear
individual bits on the collision mask. This makes selecting the areas
scanned easier.

What is the bool for?

This thread makes it look like the bool is for using decimal (instead of binary) numbers.

What is the max decimal number we can use: 32, 20, or something else?

Edit: I ask about the max index because the docs state

int collision_layer - The area’s physics layer(s). Collidable objects
can exist in any of 32 different layers. A contact is detected if
object A is in any of the layers that object B scans, or object B is
in any layers that object A scans. See also collision_mask.

:bust_in_silhouette: Reply From: hilfazer

Bool is a desired value of a bit. Int is an index of that bit.

set_collision_layer_bit( 4, false )

will set 5th bit to false.
That’s how it works for me in Godot 3.0.2.

According to Inspector there are 20 bits for layer and 20 for mask so maximum index would be 19.

Ah ok. Lol of course. I guess I’ll stick with 20 but the docs are confusing, because they claim 32.

jarlowrey | 2018-05-15 17:25

I did this

set_collision_layer_bit(306, true)

and it appeared to work. But it’s likely NOT SAFE to do.

20 is plenty. If you don’t need more than that then it does not matter if there are more than 20 bits :slight_smile:

hilfazer | 2018-05-15 19:06

Wow that’s strange 306 worked. I assume it was represented as a 32 bit unsigned integer under the hood, that’s why 32 seems like a reasonable unit. Maybe the 306 just overflowed and wrapped around.

jarlowrey | 2018-05-16 20:47