I got a class in my game that hold holds a mesh refernece and some other values, speed, hp, target etc..

for y in mob_list:
	mob_list.erase(y)
	y.free()
	y = null

I wish to know what is the proper means of removing a Class object and the list item pointing towards it? Im making a clearmap() function and and same seem to remove the class items.

I check it by : tmpCNT=0 for mob in mob_list: tmpCNT = tmpCNT + 1 b.msg(tmpCNT)

calling this, and it always shows I still have object in the class. The next level starts and i get flagged for referencing a base nill that was deleted.

I suppose a better way to ask this is

If I call

var mob_list = []	

to establish my object list..

if i free this list with

mob_list = null

How do I re-establish it so I can use it further down the line. I keep getting a "Unable to iterate on object of type 'Nil'." error.

GDScript uses reference counting to manage memory. Under the hood the scripting engine keeps track of how many references currently exist for every object. When number of references reaches zero, the object is automatically deleted.

However, only built-in container types and objects that derive from Reference class are counted in this way. Custom objects that don't have Reference class in their upstream inheritance chain are not reference counted. They need to be explicitly deleted using free(). This includes all node types. Check class' inheritance chain in the docs to determine if its instances are reference counted or not.

So if you have an array of ref counted objects, and they are only referred to from that array, assigning null to the array variable will delete everything. Letting the array variable go out of scope will do the same.

Conversely, if you have an array of nodes you'll need to iterate and free() each node manually.

If your class is a child of Node you may want to use queue_free(). You might want to consider using Node as the parent of MobEntity (or whatever you have named it) and use a Node as a container as it may let the engine do some tasks for you.

https://docs.godotengine.org/en/stable/classes/class_node.html#class-node-method-queue-free

for y in mob_list:
    mob_list.erase(y)
    y.free()
    y = null <----- this is not necessary

Erasing from the array while iterating through its elements with a for-in loop is a bad idea. You may end up with only half the elements erased. Better to just clear the array:

for mob in mob_list:
	mob.free()
mob_list.clear()

Yeah, in general, you cannot modify an array while looping through it. But I think you always want to use queue_free() not free(). Though there are cases where free() will work fine, queue_free() is safer and, AFAIK, has no downsides, so better to just use that to be on the safe side.

queue_free() is the Node class method. If your objects are not nodes but instead derive directly from Object class, the only way to delete them is free().

Deleting a node object using free() is completely fine if it's not in the scene tree or doesn't have any children. Queue_free() really is all about proper cleanup of node's involvement with its surroundings (tree, children, signals...)

10 months later