Objects aren't being destroyed by code

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Cobra!
:warning: Old Version Published before Godot 3 was released.

I’m trying to make come objects get destroyed by code, so certain levels will only have the objects it needs, and doesn’t need to compute objects it doesn’t.

However, after triple checking I got the nodepaths correct, the objects just aren’t getting destroyed.

Here’s the (relevant) code:

export(Array) var itemsToDelete
 
func _ready():
    if itemsToDelete.size() > 0:
        for item in itemsToDelete:
            print(item)
            get_node(item).free()

What do I do? I’m using Godot 2.1, by the way.

use queue_free() instead

using free can cause errors unless you REALLY know what you are doing and queue_free() makes the object disappear in the end of its usage(aka end of scene)

rustyStriker | 2018-01-09 15:29

:bust_in_silhouette: Reply From: SingingApple

Use queue_free() instead of free()

Something like this:

export(Array) var itemsToDelete

func _ready():
    if itemsToDelete.size() > 0:
        for item in itemsToDelete:
            print(item)
            get_node(item).queue_free()