• Godot HelpProgramming
  • How do I get individual collision layer values from the Area2D.collision_layer property?

My question is probably more a math question than a programming question. The Area2D.collision_layer property returns a int value. So, if I have layer 3 (value 4), 4 (value 8) and 5 (value 16) enabled in my Area2D, collision_layer returns 28.

My question is: how do I return values 4, 8 and 16 from 28 (collision_layer)?

Nevermind, I've realized that I could simply use Area2D.get_collision_layer_bit(int bit) to check if an area can collide with others on the specified collision layer.

I did however find how to get the individual layer bits from an integer number (even though I didn't need it after all): # layers is the collision_layer int func get_collision_layer_bits(layers): var layer_array = [] for i in range(20): var mask = 1 << i var masked_n = layers & mask var bit_value = masked_n >> i layer_array.append(bool(bit_value)) return layer_array pass

Basically copied from this answer: https://stackoverflow.com/questions/2249731/how-do-i-get-bit-by-bit-data-from-an-integer-value-in-c

4 years later