0 votes

i use KinematicBody2D
how to get mask id
example when i collide with mask 1(player) = queue_free(), mask 3(stone) = stop move, mask 5(item) = change color

if i code like this

if move_and_collide(velocity*delta):
  queue_free()

it will apply to all mask.. so how to make it apply to only one mask
enter image description here

in Engine by (272 points)
edited by

2 Answers

+1 vote
Best answer

I don't think you can get a node's collision layer or mask (Never mind, you can. See njamster's answer) but you could use groups to accomplish what you are trying to do.

var collision = move_and_collide(velocity * delta)
if collision:
    var collider = collision.collider

    if collider.is_in_group("Player"):
        queue_free()

    if collider.is_in_group("Rock"):
        pass

    if collider.is_in_group("Item"):
        pass

If there is only one player then you could also use its name instead of using a group with only one node.

if collider.name == "Player":
    queue_free()
by (156 points)
edited by

You can use MATCH instead of using many IF

thank you for your help

@Mk_: No, you cannot use match here as there is no common pattern.

+1 vote

You can do:

get_collision_layer_bit(0)      
get_collision_mask_bit(0)

This will return true if the first bit of the layer/mask is set or false otherwise. To get the second bit instead, just pass 1 instead of 0 as an argument. And so on.

by (10,055 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.

Categories