|Attention||Topic was automatically imported from the old Question2Answer platform.|
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_tthat 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_tscript 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.
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)
extends StaticBody2D @onready var action = action_t.new() # loading reusable code ...
extends Node2D class_name action_t func carry(player : Node2D): print("carried!") self.queue_free() # gives error. how to get object that loaded this? ...