Godot Version
4.2.2 stable
Question
With an inventory system I’m working on, I’m using reparent to handle the moving/equipping of items and what confuses me is that it works as intended for certain items but other items it does not (apparently). I tried swapping reparent for a set of remove parent/add child functions but it still leads to the exact same set of outcomes. Could someone help me understand what’s happening and what I’m doing wrong here?
The specific code segment in question is:
item.reparent(equipment_slots[i])
print("The parent of ", item.item_name, " is now ", item.get_parent().name)
if equipment_slots[i].get_child(i) == null:
print("Slot ", equipment_slots[i].name, " does not contain an item")
In the above code, I later added the print lines to attempt to debug it…
Testing things in game yields the following output:
Inventory: You tried equipping Steel Helmet
The parent of Steel Helmet is now HeadSlotInventory: You tried equipping Steel Helmet
Item: Steel Helmet on slot: HeadSlot
Unequipping Steel Helmet on slot HeadSlotInventory: You tried equipping Steel Sword
The parent of Steel Sword is now RHandSlot
Slot RHandSlot does not contain an item
In game, I test it by equipping and unequipping the helmet which works as I want it to. Yet what doesn’t make sense is how on first equip of the sword, it immediately goes into that latter print line saying the equipment slot does not have a child when, a code of line above it, the child was literally set and confirmed?? *Edit* But I should note that in the game/UI the sword does go to the correct equipment slot from the inventory. It just won’t LEAVE
Other details are that, before this function, I have already scanned the node tree and have an array of nodes that are the equipment slots (equipment_slots[i] in the code segment provided), the items and item slots are typed and I have double checked in code and debugging that they are reading and matching correctly, before equipping items they are instanced into an inventory that has a number of separate item slots, and if it matters the “HeadSlot” is the first node in the equip slot array.
I’m still fairly new to working with godot and programming in general (I have a few years experience but have never actually produced anything of note), so any constructive advice or tips would be appreciated. Thank you in advance.