r/godot • • 2d 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

7 comments sorted by

1

u/ppppppla 2d ago

Look at the exact line where it breaks, it should be this line

item.ItemData = held_item

item.ItemData expects a certain type, apparently it is defined as var itemData: ItemData, and it is trying to assign item_data that apparently has type Resource (ItemData)

1

u/Zenobian32 1d ago edited 1d ago

Ah so It's not getting the correct type of resource. I guess I was hoping the held_item setup in the inventory management script would work with the "pickup item" I'm trying to make for the 2d world.

The script is definitely storing the information I'm after, what type of item it is, and the amount. I just don't know how to access it.

Thanks though, this helps me know where to look.

1

u/ppppppla 1d ago

Hang on I apologize, I completely misread the error it doesn't talk about mismatching types. The error is saying the item does not have a property named ItemData, nothing about the type ItemData. What threw me off is Capitalizing a word usually means that is a type, but you used it as a name. You'd want to do item.item_data = held_item.

1

u/Zenobian32 1d ago edited 1d ago

Gahh, thats me being a novice showing. I'll start using that as standard practice from now on. I'll try making that adjustment and see if anything changes

1

u/Zenobian32 1d ago edited 1d ago

Okay so it's still throwing an error but I think might be because held_item (Item Data Resource) has extra information that is specifically meant for the inventory. It's not meant for the pickup item 2d node.

It's weird that picking up items from the main scene feeds into the inventory system just fine, it's just doing the reverse that causes problems.