I have an understanding that the action_t.gd is just a script and not a node, so it doesn't have a node path, but I'm just wondering if there's a way to get the node that instantiated the class script.
action_t
extends Node2D
, so when you create it with action_t.new()
, it's essentially creating a new node, but not adding it to the tree. When a node is not needed anymore, it should be freed, otherwise it will stay on the memory indefinitely.
Since action_t
is a node, you could add it to the tree and it would be very easy to get the node that created it. You can add it as child of the testbox with add_child()
. Then you can use get_parent()
to get the reference.
testbox.gd
extends StaticBody2D
@onready var action = action_t.new() # loading reusable code
...
func _ready():
add_child(action) # adds 'action' as child
action_t.gd
extends Node2D
class_name action_t
func carry(player : Node2D):
print("carried!")
var testbox = get_parent() # gets reference to the testbox
...
testbox.queue_free() # frees testbox and its children with it
...
Since actiont is a child of testbox, freeing testbox will also free actiont.