How many collision layers/groups?

In the GUI there are 20:


But in python you can only set up to a 16 bit integer or you receive an error:

AttributeError: gameOb.collisionGroup = int: KX_GameObject, expected a int bit field, 0 < group < 65536

(65536 = 2**16)

Has anyone used the last 4 collision layers to know if they work?

Um, not sure how you’re counting 20 there. There are 16 collision groups and 16 collision layers in the UI.

Keep in mind that the group and mask fields are bit flags. This means their integer value isn’t what’s important: it’s the value of each individual bit. When setting them in Python, you could use powers of 2 or bit shifting, but usually the simplest and clearest way is to use a literal binary value. For example, to only enable the last two collision groups on an object:


# using powers of 2
obj.collisionGroup = 2**15 + 2**14

# using bit shifting
obj.collisionGroup = (1<<15) + (1<<14)
# same thing with bitwise OR
obj.collisionGroup = 1<<15 | 1<<14

# using a binary literal
obj.collisionGroup = 0b1100000000000000

EDIT:
It can also be useful to use bitwise operators on the fields. For example, if you wanted to ensure objA is in all the same collision groups as objB and objC, you can use a bitwise OR:

objA.collisionGroup = objB.colligionGroup | objC.collisionGroup

Or, if you want objA to only be in collision groups that objB and objC share, you can use a bitwise AND:

objA.collisionGroup = objB.colligionGroup & objC.collisionGroup