Reusable code problem: How to create class_name functions that can use queue_free() ?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By GrandNecro

I’m trying to create reusable code for objects that can be carried. I’m doing this by putting the reusable code in a script and loading that script to whatever object that needs the reusable code. However, I can’t figure out how to get the object that the script was loaded in so that I can queue_free() it.

  • In the code below, I created a class named action_t that contains any reusable functions that might occur when the action_key is pressed.
  • I then loaded that class in testbox scene script so that I can reuse the carry() function.
    I’d like the testbox to be queue_freed using the carry() function in the action_t script but cannot figure out how to retrieve the testbox object.
  • Any ideas on how to retrieve the object that the class was instantiated in so that it can be queue_freed?

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.

player.gd

extends CharacterBody2D
func _process()
  ...
  var collided_object = $RayCast2D.get_collider()
  if collided_object:
    if(Input.is_action_just_pressed("action_key")):
      carry_object(collided_object)
 
func carry_object(collided_object : Node2D):
  collided_object.action.carry(self)

testbox.gd

extends StaticBody2D
@onready var action = action_t.new()   # loading reusable code
...

action_t.gd

extends Node2D
class_name action_t

func carry(player : Node2D):
  print("carried!")
  self.queue_free()     # gives error. how to get object that loaded this?
...
:bust_in_silhouette: Reply From: umaruru

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 action_t is a child of testbox, freeing testbox will also free action_t.