Hi, I have some problems with c++. I tried to copy overlap events tutourials from various sources, but none of them worked. I copied this video 1:1: UE4 c++ OnComponentOverlap and OnComponentHit syntax - YouTube. The problem is this line of code: _collision->OnComponentBeginOverlap.AddDynamic(this, &AMyActor3::OnDelegateExampleOverlap);
and the “.” between BeginOverlap and AddDynamic is red underlined. Did something change between the versions or why doesn’t it work? Here is my whole code:
HeaderFile:
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyActor3.generated.h"
UCLASS()
class YOUTUBE_API AMyActor3 : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMyActor3(const FObjectInitializer& objectInitializer);
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
class USphereComponent* _collision;
void OnDelegateExampleOverlap(AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bfromSweep, const FHitResult& SweepResult);
};
cpp file:
#include "Youtube.h"
#include "Components/SphereComponent.h"
#include "MyActor3.h"
// Sets default values
AMyActor3::AMyActor3(const FObjectInitializer& objectInitializer)
:Super(objectInitializer)
{
// 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;
_collision = CreateDefaultSubobject<USphereComponent>(TEXT("RootCollision"));
_collision->SetSphereRadius(250.f);
_collision->SetHiddenInGame(false);
_collision->OnComponentBeginOverlap.AddDynamic(this, &AMyActor3::OnDelegateExampleOverlap);
RootComponent = _collision;
}
// Called when the game starts or when spawned
void AMyActor3::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AMyActor3::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void AMyActor3::OnDelegateExampleOverlap(AActor * OtherActor, UPrimitiveComponent * OtherComp, int32 OtherBodyIndex, bool bfromSweep, const FHitResult & SweepResult)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, "Something overlapped");
}
And I have another question I dont understand: I always have to include " #include “Components/SphereComponent.h”" to my cpp file to use sphere/box/capsule collisions. In the tutourials, the never have to implement this. Why?