How to delete a colliding object

Godot Version

4.2.2

Question

I’m making a game where if 2 same blocks collide they create a new one. To differenciate between blocks I give them “block_id”. My problem is that I don’t know how to get a name and then id of the colliding object. I also would like to get their position to know whitch block to delete. I know you can get collisions

extends CharacterBody2D

const SPEED = 1000
const BLOCK_64 = preload(“res://block2.png”)
const BLOCK_32 = preload(“res://block32.png”)

@onready var block_32 = $“.”
@onready var move_timer = %move_timer
@onready var wall_collision = %wall_collision
@onready var detect = $detect
@onready var sprite_2d = $Sprite2D
@onready var delete_timer = $delete_timer

var can_move = true
var moving_up = false
var moving_down = false
var moving_right = false
var moving_left = false

var block_32tscn = preload(“res://block32.png”)

func _physics_process(_delta):

# Movement
if Input.is_action_just_pressed("right") and velocity.x == 0 and velocity.y == 0 and can_move:
	velocity.x += SPEED
	move_timer.start()
	can_move = false
	moving_right = true
if Input.is_action_just_pressed("left") and velocity.x == 0 and velocity.y == 0 and can_move:
	velocity.x -= SPEED
	move_timer.start()
	can_move = false
	moving_left = true
if Input.is_action_just_pressed("down") and velocity.x == 0 and velocity.y == 0 and can_move:
	velocity.y += SPEED
	move_timer.start()
	can_move = false
	moving_down = true
if Input.is_action_just_pressed("up") and velocity.x == 0 and velocity.y == 0 and can_move:
	velocity.y -= SPEED
	move_timer.start()
	can_move = false
	moving_up = true



if moving_up and velocity.y == 0:
	velocity.y -= SPEED
if moving_down and velocity.y == 0:
	velocity.y += SPEED
if moving_right and velocity.x == 0:
	velocity.x += SPEED
if moving_left and velocity.x == 0:
	velocity.x -= SPEED


move_and_slide()

func _on_move_timer_timeout():
can_move = true
moving_down = false
moving_up = false
moving_left = false
moving_right = false

func delete():
delete_timer.start()
if moving_left and velocity.x < 0:
queue_free()
if moving_right and velocity.x > 0:
queue_free()
if moving_down and velocity.y > 0:
queue_free()
if moving_up and velocity.y < 0:
queue_free()
sprite_2d.texture = BLOCK_64

func _on_detect_area_entered(area):
if (block_id of the colliding block) == get_detected.block_id:
delete()

You can get nodes that character bodies collide with like this:

if move_and_slide():
	for i in get_slide_collision_count():
		var collider_node = get_slide_collision(i).get_collider()
1 Like

So I just use collider_node.block_id?

Yep, but I think you’d need to check if the collider actually has a blobk_id property, unless every object that can collide in your scene has a block_id property, including the floor…

if move_and_slide():
	for i in get_slide_collision_count():
		var collider_node = get_slide_collision(i).get_collider()
        if "block_id" in collider_node:
            print(collider_node.block_id)
1 Like

Thank you. Everything works as intended

1 Like