Hi! I’m currently trying to create an item that can be picked up. I want to do this using composition instead of inheritance. But right now, I’m getting duplicate instances when
When I enter the “pickup” zone, my PickUpArea signals that the item has been picked up. My Item (Sword, HealingPotion), to which the PickUpArea is attached, listens for this signal and the inventory signal itself, passing the Resource—item_data and quantity—and then destroys itself.
So it turns out that every new Item will have to implement a function that handles the signal from PickUpArea, doing literally the same thing: signaling to the inventory and destroying itself.
How can this problem be solved using composition? I understand how to solve it using inheritance.
You could have a Node that does the pickup logic, and you add that Node to each item that have the ability to be picked up.
If you add a few exports to your script you can also have some flexibility.
With this system, you can just slap the “Interactable” Node (I now realize the typo, oops) on any scene, setup an area and an other node to “activate”.
This way, I can implement a new “Activator” Node that uses player detection or anything really, and activate the same “Effector”. Or use the same “Activator” to trigger a number or “Effectors”.
That’s how I did interactions in a prototype I’m working on, but I’m not sure it’s the best way tho…
I feel I should use signals to do this in some way but it still eludes me a little bit
(please put code between two ``` to make it a code block)
func like_this()
pass
From what I understand, you have an Item (here a Sword), root Node contains a ItemData instance and in the Scene hierarchy of this Item, you have a PickUpArea Node with this script attached.
If that’s the case, you could change your script to have an @export pointing to the Node that contains the ItemData and get it from there… of course that means you need all items using this to have that.
@export var item_root_node: Node2D
@export var quantity: int = 1
func _ready():
EventBus.item_picked_up.connect(_on_item_picked_up)
func _on_item_picked_up():
EventBus.item_received.emit(item_root_node.item_data, quantity)
queue_free()
You could also have a generic Item class that implements a dud for data, that you use on the root Node, and that cleans up the code
@export var item: Item
@export var quantity: int = 1
func _ready():
EventBus.item_picked_up.connect(_on_item_picked_up)
func _on_item_picked_up():
EventBus.item_received.emit(item.data, quantity)
queue_free()
Or am I misunderstanding something?



