I’ve been reading about this all over the forums. Subobjects do not overlap if initialized in the constructor. The stated workaround is to initialize the the overlap in BeginPlay, which works. The thing is, the documentation says it should work in the constructor and I’ve seen tutorials showing that it does. But following said tutorials does not produce the same results. I find this to be a curious thing.
Here’s my setup:
ARocket(const FObjectInitializer &objectInitializer);
UFUNCTION()
void OnComponentBeginOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
// Sets default values
ARocket::ARocket(const FObjectInitializer &objectInitializer):Super(objectInitializer)
{
// Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
// Rocket
//static ConstructionHelpers::FObjectFinder<UStaticMesh>;
RocketMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Rocket Mesh"));
RocketMesh->SetupAttachment(RootComponent);
RootComponent = RocketMesh;
// Camera
SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("Spring Arm"));
SpringArm->SetupAttachment(RocketMesh);
SpringArm->bInheritPitch = false;
SpringArm->bInheritRoll = false;
SpringArm->bInheritYaw = false;
DefaultArmLength = 300.0f; // defined in header
SpringArm->TargetArmLength = DefaultArmLength;
SpringArm->bEnableCameraLag = true;
Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
Camera->SetupAttachment(SpringArm);
// Collision bodies
RocketCapsuleLower = CreateDefaultSubobject<UCapsuleComponent>(TEXT("Rocket Capsule Lower"));
RocketCapsuleLower->SetupAttachment(RocketMesh);
RocketCapsuleLower->SetRelativeLocation(FVector(0.0f, 0.0f, -17.0f));
RocketCapsuleLower->SetCapsuleHalfHeight(30.0f);
RocketCapsuleLower->SetCapsuleRadius(25.0f);
RocketCapsuleLower->SetGenerateOverlapEvents(true);
RocketCapsuleLower->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
RocketCapsuleLower->SetCollisionResponseToChannel(ECollisionChannel::ECC_Visibility, ECollisionResponse::ECR_Overlap);
RocketCapsuleLower->OnComponentBeginOverlap.AddDynamic(this, &ARocket::OnComponentBeginOverlap);
// Testing Collision Bodies
void ARocket::OnComponentBeginOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
GEngine->AddOnScreenDebugMessage(-1, 0.5f, FColor::Red, TEXT("You Hit Something!"));
}
As mentioned above, setting the line
RocketCapsuleLower->OnComponentBeginOverlap.AddDynamic(this, &ARocket::OnComponentBeginOverlap);
in BeginPlay does work. The question is, why doesn’t it work from the constructor? I thought adding FObjectInitializer would have solved it but it didn’t and seems unnecessary.