Godot Mobile deleting Object

Godot Version

v4.5.1

Question

I’m trying to make a game in Godot mobile. There a line should spawn between 2 Fingers touching the screen. This is already working, but if the line is colliding with something, it should be able to delete this object, or the object should delete itself… The problem there’s that it´s only working, if you enter the object with the line really fast. → If you leave the line on the Object, its not deleting itself.

So I already got the Line (CharacterBody2d) with two Childs (CollisionShape2d and Line2d). The code of the Line is the first bit of the Code:

The other object is a Area2d with two Childs (collisionShape2d and ColorRect). the code is the second one in the following code.

I´ve them put both of them in a main scene, which is just a Control Node above these two scenes.

I really hope, someone Is able to find a solution…

extends Node2D
@onready var collision_shape: CollisionShape2D = $CollisionShape2D
@onready var line_2d: Line2D = $Line2d



var rect := RectangleShape2D.new()
var finger_positionen = {}
var breite = 50.0

func _ready():
	line_2d.visible = false
	collision_shape.disabled = true
	
func _input(event):
	if event is InputEventScreenTouch:
		if event.pressed:
			finger_positionen[event.index] = event.position
		else:
			finger_positionen.erase(event.index)
		update_line()
	elif event is InputEventScreenDrag:
		finger_positionen[event.index] = event.position
		update_line()
		
func update_line():
	if finger_positionen.size() == 2:
		line_2d.visible = true
		collision_shape.disabled = false
		
		var keys = finger_positionen.keys()
		keys.sort()

		var a = finger_positionen[keys[0]]
		var b = finger_positionen[keys[1]]
		line_2d.points = PackedVector2Array([a,b])
		

		var length = (b-a).length()
		rect.size = Vector2(length, breite)
		
		collision_shape.shape = rect
		collision_shape.position = (a+b)/2
		collision_shape.rotation = (b-a).angle()

		
extends Area2D



func _physics_process(_delta):
	
	await get_tree().physics_frame
   
	var bodies = get_overlapping_bodies()
	for body in bodies:
		if body.is_in_group("line"):
			queue_free()

My guesses regarding this are the following:

  • you did not show us a part of your code where you set collision_shape.disabled = true. There is a bug there
  • your line and your objects collide in a way that prevents them from moving into each other and therefore overlapping (although I think this is less likely)
    what I would recommend you to do is add some debug statements:
    print out every time you do any modification tho the disabled property of the collisionShape:
    If that doesn’t help print out every overlapping body in your get_oberlapping_bodies() call.

Oh I also just notices you are using ‘get_overlapping_bodies()’ which should not detect areas of any kind. It is really weird your areas get deleted in any situation at all. Do you have some kind of second delete logic somewhere? If you want to detect areas you should use ‘get_overlapping_areas()‘

I did disable the collision_shape: in the function ready of the line. I used overlapping_bodies in my test object because the line is a character_body_2d which is a body get_overlapping_bodies() is checking for.