I am trying to create a system where the player can pickup boxes and release them in a similar way to Half-Life 2. My player pawn consists of three components. A sphere component (RootComponent) for collision, a camera component, and a second sphere component that is slightly in front of the collision sphere which I plan to use to check for overlaps with objects. Being New to UE4 and programming in general I have run into some confusion with using a TArray resulting from UPrimitiveComponent::GetOverlappingActors()
I am planning to implement the overlap check within an action mapped event. The following is the code i have written so far:
void AMainPawn::OnBeginPickup()
{
if (bIsHolding == false)
{
TArray<AActor*> OverlappingActorsArray;
PickupCheckerSphere->GetOverlappingActors(OverlappingActorsArray);
}
}
void AMainPawn::OnEndPickup()
{
}
If my understanding of the documentation is correct I should now have a TArray of AActor pointers called OverlappingActorsArray that contains pointers to all the specific instances of AActors that have the ‘Generate Overlap Events’ flag checked which are overlapping with the pickupchecker sphere component (the bIsHolding variable is set to false in the constructor).
What I would like to do now is essentially grab the first index in this array and attach the object associated with the pointer to the PickupCheckerSphere component (so that it will move with this component until it is released).
Can someone please explain how I might go about doing this? I have looked at TArray functions in the documentation however I still cant work out how to properly use the functions. I suspect that It will involve the use of Find() or something similar, however I am still confused about how to handle the actor pointers.
As for attaching the box to the sphere component once I am able to reference the box, I was planning on using UPrimitiveComponent::WeldTo()
Would this be a correct approach?