Attaching object A to object B works fine. But when I bring object C into the mix, everything fails.
C as a grandparent
Say I have a bowl. I put an orange in it (eg. I attach the orange to the bowl). However, when I try to pick up this bowl (eg. attach it to my character), the orange stays where it was–the bowl is the only thing that’s picked up. orange->GetAttachParentActor()
is null.
C as a grandchild
Say my character is already carrying the bowl. I attempt to put the orange into the bowl that is being carried. The game crashes with the following messages:
First-chance exception at 0x000007FED41A2637 (UE4Editor-Engine.dll) in UE4Editor.exe: 0xC0000005: Access violation reading location 0x0000000000000000.
Unhandled exception at 0x000007FED41A2637 (UE4Editor-Engine.dll) in UE4Editor.exe: 0xC0000005: Access violation reading location 0x0000000000000000.
At this line:
i->RootComponent->SetSimulatePhysics(true);
For this (line 2469 of FBodyInstance::SetInstanceSimulatePhysics()):
Weld(ChildBI, ChildBI->OwnerComponent->GetSocketTransform(ChildrenLabels[ChildIdx]));
For both these scenarios, I’m using this code to attach the orange to the bowl (where i
is the item to be attached = the orange):
// Disable collision first for attachment purposes
i->SetActorEnableCollision(false);
i->AttachRootComponentToActor(newParent, NAME_None, EAttachLocation::SnapToTarget);
// Now enable both collision and physics so that the orange will land in the bowl as opposed to float in it
i->SetActorEnableCollision(true);
i->RootComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
i->RootComponent->SetSimulatePhysics(true);
i->RootComponent->SetEnableGravity(true);
And this pseudo code to attach the bowl to the player:
bowl->DisablePhysics();
bowl->AttachRootComponentTo(player);
What’s wrong with the way I’m attaching these objects? What is the correct way to do it?