Overlap not triggered

I’m trying to setup overlapping through c++, but it is not getting fired. I am using the component overlapping event, and in Blueprint it is working, my PrintString node is getting triggered. But when I tried to bind a callback to the OnComponentBeginOverlap (same component) event, it is not firing.

I am binding the callback in BeginPlay(), and I have a null check before binding as well. The code also does compile, so I’m completely out of ideas. Any suggestion would be greatly appreciated.

Are they overlapping already in begin play? :slight_smile:

Nope

Can you show your code?

AApple::AApple()
{
 	// 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;

	Tags.Add("Apple");

	BoxComponent = CreateDefaultSubobject<UBoxComponent>(TEXT("BoxComp"));
	BoxComponent->InitBoxExtent(FVector(50));
	BoxComponent->SetCollisionProfileName("OverlapAll");
	BoxComponent->SetGenerateOverlapEvents(true);
	RootComponent = BoxComponent;

	MeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMesh"));
	MeshComponent->SetCollisionProfileName("NoCollision");
	MeshComponent->SetupAttachment(RootComponent);
}

// Called when the game starts or when spawned
void AApple::BeginPlay()
{
	Super::BeginPlay();

	if (BoxComponent)
	{
		GEngine->AddOnScreenDebugMessage(-1, 60, FColor::Cyan, "Add");

		BoxComponent->OnComponentBeginOverlap.AddDynamic(this, &AApple::OnOverlap);
	}

	GridSubsystem = GetWorld()->GetSubsystem<UGridSubsystem>();

	if (GridSubsystem)
	{
		FIntVector GridSize = GridSubsystem->GetMatrix();
		FIntVector RandomGridPos;

		do
		{
			RandomGridPos = FIntVector(FMath::RandRange(0, GridSize.X - 1), FMath::RandRange(0, GridSize.Y - 1), FMath::RandRange(0, GridSize.Z - 1));
		} while (CurrentGridPosition == RandomGridPos || GridSubsystem->GetTileState(RandomGridPos) != TileState::Unoccupied);

		FVector NewLocation;
		GridSubsystem->GetTilePosition(RandomGridPos, NewLocation);
		CurrentGridPosition = RandomGridPos;
		SetActorLocation(NewLocation);
	}
}

void AApple::OnOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	GEngine->AddOnScreenDebugMessage(-1, 60, FColor::Cyan, "Overlap");

	if (!OtherActor->ActorHasTag("Head")) return;


	if (GridSubsystem->GetOccupiedCount() == GridSubsystem->GetCount()) SetActorLocation(FVector(-500, -500, 0));

	FIntVector GridSize = GridSubsystem->GetMatrix();
	FIntVector RandomGridPos;

	do
	{
		RandomGridPos = FIntVector(FMath::RandRange(0, GridSize.X - 1), FMath::RandRange(0, GridSize.Y - 1), FMath::RandRange(0, GridSize.Z - 1));
	} while (CurrentGridPosition == RandomGridPos || GridSubsystem->GetTileState(RandomGridPos) != TileState::Unoccupied);

	FVector NewLocation;
	GridSubsystem->GetTilePosition(RandomGridPos, NewLocation);
	CurrentGridPosition = RandomGridPos;
	SetActorLocation(NewLocation);
}

Here it is.

The thing is the AddOnScreenDebugMessage at the beginning of OnOverlap is not getting called at all.

Allright I just get rid of the other logic and focus on overlaps on your code and test it. Please make sure that you have appropiate header declerations on your functions on overlap so it gets triggered. Like putting UFunction to Overlap. That is the culprit I suspect.

Let us know if thats the case.

Apple.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Components/BoxComponent.h"
#include "Apple.generated.h"

UCLASS()
class SYSTEMCOLORSDEMO_API AApple : public AActor
{
	GENERATED_BODY()

public:
	// Sets default values for this actor's properties
	AApple();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

	UFUNCTION()
	void OnOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp,
	               int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);

public:
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Mesh, meta = (AllowPrivateAccess = "true"))
	UBoxComponent* BoxComponent;

	UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Mesh, meta = (AllowPrivateAccess = "true"))
	UStaticMeshComponent* MeshComponent;
	
};

apple.cpp

// Fill out your copyright notice in the Description page of Project Settings.


#include "Apple.h"

AApple::AApple()
{
 	// 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;

	Tags.Add("Apple");

	BoxComponent = CreateDefaultSubobject<UBoxComponent>(TEXT("BoxComp"));
	BoxComponent->InitBoxExtent(FVector(50));
	BoxComponent->SetCollisionProfileName("OverlapAll");
	BoxComponent->SetGenerateOverlapEvents(true);
	RootComponent = BoxComponent;
	
	MeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMesh"));
	MeshComponent->SetCollisionProfileName("NoCollision");
	MeshComponent->SetupAttachment(BoxComponent);
}

// Called when the game starts or when spawned
void AApple::BeginPlay()
{
	Super::BeginPlay();

	if (BoxComponent)
	{
		GEngine->AddOnScreenDebugMessage(-1, 60, FColor::Cyan, "Add");

		BoxComponent->OnComponentBeginOverlap.AddDynamic(this, &AApple::OnOverlap);
	}
}

void AApple::OnOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	GEngine->AddOnScreenDebugMessage(-1, 60, FColor::Cyan, "Overlap");
	
}

void AApple::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}

Cause without it basically Unable to bind delegate to ‘OnOverlap’ (function might not be marked as a UFUNCTION or object may be pending kill)

123

Right, I forgot about the header file, but sadly it the OnOverlap function does have the UFUNCTION macro in front of it. :smiling_face_with_tear:

Ok, I compiled the engine from IDE, and everything seems to work now… I guess some file just didn’t update correctly in the background.

1 Like

Not sure than, it just works on my machine, everything seems to be fine.

Not sure but can be also SetRootComponent(BoxComponent); maybe there is a event on begin etc. Long shot but can try.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.