Animations get randomly freed

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By strive_and_conjure

There is a bullet scene in my game. Sometimes, when I shoot, the animatedSprite textures get randomly freed before they are supposed to. They don’t collide with anything, because only the sprite disappears, and if an enemy is still in its path, its invisible collision still occurs and the enemy still takes damage. Its an Area2D with a body_entered function and a group check to perform collision and damage.

 onready var bullet = get_node("AnimatedSprite");
 func _on_player_bullet_body_entered(body):
    	if body.is_in_group("enemies"):
    		if body.is_coll_enabled:
    			$collision.set_deferred("disabled", true);
    			if(weakref(bullet).get_ref() and bullet is AnimatedSprite):
    				bullet.play("collide");
    			vel = Vector2(0, 0);
    			body.damage(damage);
    	elif body.is_in_group("wall"):
    		$collision.set_deferred("disabled", true);
    		if(weakref(bullet).get_ref() and bullet is AnimatedSprite):
    			bullet.play("collide");
    		vel = Vector2(0, 0); 

I have had to add weird checkers like the weakref for the sprite and whether the variable is still a sprite type, because it legitimately shows an error for that. Whenever it randomly frees the sprite, the bullet variable starts referencing the collision node for the bullet ( i am baffled as to how this can occur).

func _on_AnimatedSprite_animation_finished():
	if bullet.get_animation() == "collide":
		queue_free();

It can’t queue_free the bullet before the animation plays either, so when I turn on collision visibility in the output, sure enough, there are a bunch of bullets around stuck on walls since they were never freed. They can’t damage anyone, since I disable the collision if they collide, but it still looks very unnatural that the bullet disappears randomly and the damage still happens. Moreover, it happens reasonably frequently. 1/5 times I think.