how to set multiple collision masks in script
currently i can only set 1 mask in my code

phys.collision_mask = 1

  • It's a bit mask, so it's binary. You can save the bit values as binary numbers and then use the bitwise or | to combine them.

    var gun_mask = 0b0001
    var knife_mask = 0b1000
    
    collision_mask = gun_mask | knife_mask

i dont remember exactly, but i think that collision 1, is the mask 0.

if tipo == "turret":
     set_collision_mask_bit(13,true) # <- mask 12
  • DJM replied to this.

    Zelta yes collision 1 is mask 0.
    im trying to add multiple masks thru code

      i want to do something like>

      phys.collision_mask = 1 and 4
      but that obviously doesnt work

      Are you trying to set bits 1 and 4 in the collision mask?

      • DJM replied to this.

        There's some inconsistency in how the bits are numbered.

        In the Inspector, the left-most bit is numbered 1. In the method call set_collision_mask_bit(), that same bit is 0. In either case, that's the LSB (least significant bit) of the integer mask.

        It doesn't matter which numbering scheme you use, so long as you're consistent about it.

        If you set the bits numbered 1 and 4 in the Inspector, that's equivalent to doing it in code by:

        # Set bits 0 and 3. Leave other bits unchanged.
        set_collision_mask_bit(0, true)
        set_collision_mask_bit(3, true)

        or:

        # Set whole mask to specified value. 
        collision_mask = 1 + 8

        or:

        collision_mask = 9

        or:

        collision_mask = 0b1001 # binary
        • DJM replied to this.

          It's a bit mask, so it's binary. You can save the bit values as binary numbers and then use the bitwise or | to combine them.

          var gun_mask = 0b0001
          var knife_mask = 0b1000
          
          collision_mask = gun_mask | knife_mask
          • DJM replied to this.

            DaveTheCoder that doesnt work on my end
            cybereality
            how do i get the bit binary for 1 and 4?
            im trying to use it in your raycast script u helped me with.

            var from = camera.project_ray_origin(wantedpos)
            var to = from + camera.project_ray_normal(wantedpos) * ray_length
            var space = get_world_3d().direct_space_state
            var phys = PhysicsRayQueryParameters3D.new()
            phys.from = from
            phys.to = to
            phys.collision_mask = 1

            cybereality ok got it working, tnx!
            but the binary for mask 4 says 100 on that website, while var mask4 = 0b1000 was the correct number

            100 is the binary for 4, but in Godot the bit mask layer is not the same as the value (bit mask layer 4 is the bit 3, or the 4th one from the right) . If you roll over the bit mask grid in the inspector, it will tell you the "value" which is the binary number (in decimal). For the layer 4 it is value 8, which is 0b1000. But if you are not combining them, it is easy to figure out without conversion as the mask layer is just the digit in the binary number that is set to one.

            Layer 1 = 0b0001
            Layer 3 = 0b0100

            etc.