Making Line2D trail disappear ONLY once the projectile has collided

Godot Version

Godot 4.7.1

Question

How would I go about making a trail that disappears from the back only once the projectile has collided with the ceiling. I have added my attempt at doing this below.

extends CharacterBody2D

var hasCollided = false

func _ready():
	$Line2D.global_position = global_position

func _physics_process(_delta: float) -> void:
	if not hasCollided:
		$Line2D.add_point(position, 0)
	move_and_slide()

func _on_projectile_physics_handler_colliding_with_ceiling() -> void:
	while $Line2D.points.size() > 0:
		$Line2D.remove_point(0)
		print($Line2D.points)

The Line2D points get removed all at once, instead of back to front.

your while loop executes all at once, it doesn’t wait for any given amount of time, not even a frame.

oh lol idk how I didn’t think of that

i fixed the issue by making the signal assign a variable hasCollided to true, then in the physics_process function while true, removing the first point in the Line2D array

so now the points only get deleted every delta seconds

func _physics_process(_delta: float) -> void:
	if not hasCollided:
		$Line2D.add_point(position)
	else:
		$Line2D.remove_point(0)

	move_and_slide()

func _on_projectile_physics_handler_colliding_with_ceiling() -> void:
	hasCollided = true

To clarify running this in _physics_process happens about 60 times per second, depending on your project’s physics settings and framerate. If you ran it in _process it would be once per frame. Neither is strictly per-second so if you want a consistent effect it may be best to use a short timer node to add and remove points.