Reparent works and then doesn't..? Help please!

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 HeadSlot

Inventory: You tried equipping Steel Helmet
Item: Steel Helmet on slot: HeadSlot
Unequipping Steel Helmet on slot HeadSlot

Inventory: 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 :weary:

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.

I think the problem is in if equipment_slots[i].get_child(i) == null:. The i index in get_child() is probably not correct.

1 Like

Oh snap. Yeah for that if statement i should not be used in get_child. Thank you!

The “error message” is now no longer taunting me (and more importantly would mean reparent isn’t at fault here but most likely how I’m managing the logic).

The darn sword still is not able to be unequipped like the helmet is able to though. They both are derived/intsanced from the same script & class, so it’s still a mystery for now how one is working and the other isn’t. For now, I’ll try making more item types and see how they behave as I look things over.

For posterity.

What @soapspangledgames said was true about that particular if statement that would throw out that print line. But I was still not getting the expected behavior out of the function. I looked at my code for the nth time and realized I was staring at like nine spots where I had also used get_child(i) and I tried replacing all of those with get_child(0) and like magic the sword dances from inventory to equipped slots & back.

:relieved:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.