Help Dealing With Stray Nodes

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

Hi All,

I have recently gone on a hunt refactoring my code to get rid of all the stray nodes it was creating. There is one instance that keeps popping up in print_stray_nodes() that I can’t get rid of, however. It is the “explode” effect on my player projectile. It does not become stray every time I instance a projectile, only after several minutes of run time. Here is the code for the projectile. I instance the explosion tscn in the delete_missile() function.

onready var explosiontcsn = load("res://Effects/Player_MissileExplode.tscn")
var toggle = true
var alive = true

func _physics_process(delta):
	if alive:
		self.global_position += velocity.rotated(rotation) * SPEED * delta
	elif !alive:
		delete_missile()


func _on_Area2D_area_entered(_area):
	alive = false

func _on_VisibilityNotifier2D_screen_exited():
	alive = false

func delete_missile():
if toggle:
	toggle = false
	trail.destroy()
	self.remove_child(trail)
	get_parent().add_child(trail)
	var explosion = explosiontcsn.instance()
	get_parent().add_child(explosion)
	explosion.global_position = global_position
	trail.global_position = global_position
	explosion.rotate(rotation)
	if Globals.missile_counter > 0:
		Globals.missile_counter -= 1
	queue_free()

Any ideas on what I can do to fix this to prevent the explode scene from becoming a stray? I spawn these projectiles pretty often as you can imagine, and pretty rapidly often. Thanks in advance.

EDIT: I just realized it is happening consistently at 22 seconds of runtime whether instance a missile or not???

:bust_in_silhouette: Reply From: RaebaldKashban

Is there anywhere else in your code where you change the value of “toggle”? Your delete_missile() function only deletes the node if toggle is true. I don’t know how the rest of your project is structured, but if toggle gets set to false somehow in another part of the code, then queue_free() may never get called.

As an experiment, you could try removing the conditional statement and seeing if that fixes the issue. Would at least help you narrow down possible problems.

Hey thanks for the reply,

I couldn’t figure out how to delete the question but I figured it out. It was because I was doing var X = preload(explode_tscn).instance() in another unrelated scene! This was what originally set me on this stray node hunt because I did this on all of my scenes!

scrubswithnosleeves | 2020-09-26 16:50