So I want to create an overlap event with an actor. I’ve followed several tutorials but I still get a few errors. Can someone tell me what I’m doing wrong? Here’s the header file.
#pragma once
#include "GameFramework/Actor.h"
#include "MyActor.generated.h"
UCLASS()
class PROJECT_API AMyActor : public AActor
{
GENERATED_BODY()
UPROPERTY(VisibleAnywhere, Category = "Trigger Capsule")
class UCapsuleComponent* Trigger;
public:
// Sets default values for this actor's properties
AMyActor();
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Called every frame
virtual void Tick( float DeltaSeconds ) override;
float SphereRadius;
UPROPERTY(EditAnywhere)
UStaticMeshComponent* Mesh;
UFUNCTION()
void OnOverlapBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
};
And here’s the cpp file:
// Fill out your copyright notice in the Description page of Project Settings.
#include "Project.h"
#include "MyActor.h"
#include "Components/CapsuleComponent.h"
#include "DrawDebugHelpers.h"
// Sets default values
AMyActor::AMyActor()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
Mesh = CreateDefaultSubobject<UStaticMeshComponent>("VCone1");
// declare trigger capsule
Trigger = CreateDefaultSubobject<UCapsuleComponent>(TEXT("Trigger Capsule"));
Trigger->InitCapsuleSize(55.f, 96.0f);;
Trigger->SetCollisionProfileName(TEXT("Trigger"));
Trigger->SetupAttachment(RootComponent);
Trigger->OnComponentBeginOverlap.AddDynamic(this, &AMyActor::OnOverlapBegin);
}
void AMyActor::OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if (OtherActor && (OtherActor != this) && OtherComp)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Overlap Begin"));
}
}
// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AMyActor::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
}
The first error I’m getting is with Trigger->SetupAttachment(RootComponent);. It keeps saying that UCapsuleComponent has no member named SetupAttachment. The second problem is with the line Trigger->OnComponentBeginOverlap.AddDynamic(this, &AMyActor::OnOverlapBegin);. Does anyone know what the problem is?
This is very odd, I do not see any issues, so I took your code and it compiles for me with no errors. But then I noticed your post says you are using engine version 4.11. If you are, that would definitely be your problem. This is current engine code your using. In engine 4.11 the overlap function most likely has a different code signature. I would recommend updating to 4.20 and this should work. If not, you will need to use the 4.11 version of the signature by finding the delegate inside the UPrimitiveComponent class. (Which it looks like the compiler gave you the answer.)
So change your signature for your OnOverlapBegin function in your header file and cpp file to this:
Indeed, as said Steve, you are using 4.11 and from the log and the 4.11 source the overlap singature is
(AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult);