Rule of thumb:
- If an object inherits
Reference, it will be automatically managed.
- If an object does not, it needs to be freed manually, or freed by its parent.
Scripts and classes declared using the
class keyword will inherit
Reference if they have no
So when you do
Abc.Ab.new(), it will return an instance of
Ab, which is a reference, so it will be automatically freed as soon as it gets out of scope. You should not use
free() on them.
onready var Abc = load("res://script/Abc.gd").new()
.new() here. So this will create an instance of
Abc.gd, and this one
extends Node2D. Nodes don't inherit
Reference, so they are not automatically freed. If you don't add them to the scene tree, you have to delete them using
free() once you are done with them.
However, if you add them to the tree as child of another node, they will be freed automatically when their parent is freed. Alternatively, nodes can be freed using
Note about your current case:
If all you wanted was to create an instance of
Ab, you don't need to create an instance of
Abc.gd. A script and an instance of that script are two different things.
All you need to do is this:
# Loads the script without creating an instance of it.
var Abc = load("res://script/Abc.gd")
# This also works if you don't have cyclic dependencies,
# with the advantage of providing better completion
const Abc = preload("res://script/Abc.gd")
And this will still work:
var new_ab = Abc.Ab.new(x,y)
You also won't need to free
Abc because scripts are resources, and