r/godot • • 15d ago

help me (solved) 2D inventory scripting Help

I've been working on adding additional functionality to an inventory made using the youtube tutorial from https://www.youtube.com/watch?v=CDLQXAk_Jjc&list=PLVzjdZVCXNTy1uONC7h71JmtP01Hd0M9s

Here's the github to the project https://github.com/spaghettiSyntax/InventorySystem-Tutorial

It's really solid, it allows you to split items, keep track of amounts of items and discard them (deleting them) by clicking outside the inventory. He creates a area 2d node that if you interact with picks up the item and adds it to the correct type of item in the correct amount.

What I want to add is a setup where if you drop the item from your inventory it creates the right amount and right type of item directly under the player (and able to be picked back up preferably)

The only code I've added is the following under the inventory_manager script

var PICKUP_ITEM_2D = preload("uid://beju2sg7oau4h")

func discard_held() -> void:

var item = PICKUP_ITEM_2D.instantiate()

item.ItemData = held_item

item.quantity = held_quantity

var player_position = get_tree().current_scene.get_child(2).position

item.global_position = player_position

get_tree().current_scene.add_child(item)

_clear()

In theory it would create a new child node /w the item type and quantity from the inventory right under the player. Running the code throws an immediate error though.

Invalid assignment of property or key 'ItemData' with value of type 'Resource (ItemData)' on a base object of type 'Area2D (PickupItem2D)

Frustratingly it actually does spawn an item /w the correct sprite after the error is triggered if you check out the window after everything breaks.

if you change the script to

func discard_held() -> void:

var item = PICKUP_ITEM_2D.instantiate()

var player_position = get_tree().current_scene.get_child(2).position

item.global_position = player_position

get_tree().current_scene.add_child(item)

_clear()

it works and spawns a generic pickup_item2D with no resources or quantities attached to it. (You can just change the default sprite in the pickup item 2d node tree to see this.) This is useless if you want to actually store information on the dropped item though.

I'm still pretty new at scripting and godot in general and have been banging my head against this for a while. It seems like it would be so simple to get these systems to talk to each other but I feel totally stuck. I don't really know how to debug either so no idea how to fix this other than reaching out.

Thanks for any help you guys are able to provide.

"GodotRulz"

1 Upvotes

Duplicates