I am building an open world 2D game where the character is loaded into new scenes frequently. I noticed that previous nodes were still active in the tree after calling get_tree().change_scene("next_scene")
. For example, the _physics_process(delta)
function of a node that is no longer of interest is called continuously even after the game no longer needs the node. I am imagining this builds up over time and seems very process intensive.
To prevent this, I started calling queue_free()
after the scene is changed, but I don't want to remember this every time on the calling node. Instead, I wrote a global function in a singleton that takes in the calling node instance as a parameter and, after loading the player into the next relevant scene, it cleans up the old node (simplified version of the function below):
func change_player_location(next_location: String, call_node: Node):
call_node.queue_free()
return get_tree().change_scene(next_location)
My goal is to not have to remember to clean up nodes. Should I be concerned about this? Is there a better way?