This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

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?
...
Godot version Godot v4.0 Stable
in Engine by (64 points)
edited by

1 Answer

+1 vote
Best answer

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.

by (82 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.