Devlog #2 — UE5 Debugging: The CDO Problem I Didn't Know Existed!
The Issue
I was working on a pickup & drop system in Unreal Engine 5, and I ran into a weird problem.
My component was able to hold an item, but when I called the DropHeldItem() function, the item was not dropped. I set
breakpoints in both the Hold and Drop functions and found that all the pointers I set in the TryHold() function
(like CurrentHeldItem) are now null inside the DropHeldItem() function, and I am sure they were not set to null anywhere!
The component responsible for holding and dropping items is UItemHolderComponent.
TryHold()WorkedDropHeldItem()Didn’t Work:1
ItemHolderComponent > Drop: CurrentHeldItem is null
Everything signals that it should normally work.
- The component was created correctly.
- All pointers were marked with UPROPERTY().
- No code path was clearing the data.
But the state was gone!
The Cause
After detailed debugging, I discovered that the DropHeldItem() function was executed on the Class Default Object, instead
of the runtime component.
1
2
Hold -> owner=BP_MainCharacter_C_0
Drop -> owner=BPGC_ARCH_FOR_CDO_MainCharacter_3
The Fix
To quickly fix this, I didn’t directly use the cached component pointer, but instead got the component at runtime.
1
2
3
4
5
6
7
8
9
10
11
void AMainCharacter::DropItemInHand()
{
if (UItemHolderComponent* Holder = GetComponentByClass<UItemHolderComponent>())
{
Holder->DropHeldItem();
}
else
{
UE_LOG(LogTemp, Error, TEXT("ItemHolderComponent not found!"));
}
}
Conclusion
I won’t assume it’s fixed! I will debug it more when I have time. I need to
understand why ItemHolderComponent was pointing to the CDO instead of the runtime component.
Let me know if you had this issue before, or if you know why it happens.