Collision works in BP but not in C++

Hi!

I have a parent class called interactive object, and some blueprint parents. In Interactive Object, there is a BeginOverlap Function, but it doesn’t work!

For a test, I added a print to BeginOverlap in the event graph of one of the Blueprint Children of InteractiveObject, and it worked fine, so there must be something wrong with my BeginOverlap Function, what’s the problem with it?

cpp:

    void AInteractiveObject::BeginOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
    {
    	//if (typeid(OtherActor) == typeid(nightguard))
    	//{
    		UE_LOG(LogTemp, Log, TEXT("BeginOverlap"));
    		nightguard = (ARSGameCharacter*) OtherActor;
    		withinrange = true;
    	//}
    }

h:

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

I’m thinking you haven’t subscribed to the event. Subscribing basically sets up a function to call when that event fires a broadcast.

In your .cpp BeginPlay() function, you’ll want a line that reads:

OnActorBeginOverlap.AddDynamic(this, &AMyActor::BeginOverlap);

That way, your function BeginOverlap executes when the actor detects anything overlapping, and passes through those actor pointers as arguments.

Hello! Thanks for your answer…

I’ve tried what you said, but AddDynamic doesn’t seem to exist :confused: the only options are Add and AddUnique, not AddDynamic.

What’s wrong?

Also, how do i set up arguments to BeginPlay?

Good catch , I’ve edited my answer to reflect the need for the class name.

Looking in Actor.h, the declaration on line 26 says it takes two AActor*'s, the actor being overlapped, and the incoming actor. Just use those and it should work out!

Hey TheSporech-

Timothy’s suggestion was mostly correct. Though IntelliSense may complain about AddDynamic, it is the correct way to bind your function to the overlap action. The full syntax for the binding would be OnActorBeginOverlap.AddDynamic(this, &AMyActor::BeginOverlap); where AMyActor is the class where your function is declared.

Another important note is that when binding your function like this, the parameter list of your function has to match the parameter list of OnActorBeginOverlap delegate which can be found in the Actor.h file.

Cheers

Hello!
Thanks so much for explaining…
I am getting an error becuase i must have the wrong parameters. I cant find them in Actor.h, so could you post them?

Thank you!

It is listed in the Delegate signature section near the top of the file. If you find the UPROPERTY for OnActorBeginOverlap, it’s type is FActorBeginOverlapSignature. The delegate signature for FActorBeginOverlapSignature is (AActor* OverlappedActor, AActor* OtherActor).

I’ve got the code compiling correctly, but im still not getting overlap events!

:S

What component of the actor are you attempting to overlap? Is that component as well as the other actor causing the overlap set to generate overlap events? Additionally, is your actor set to not block the other actor?

It’s just the box component. I’ll show you the entire class, just the constructor and the function i am using:

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

#include "RSGame.h"
#include "InteractiveObject.h"
#include <typeinfo>


// Sets default values
AInteractiveObject::AInteractiveObject()
{
 	// 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;

	meshcomp = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Skeletal Mesh"));
	meshcomp->CastShadow = true;
	RootComponent = meshcomp;

	EntryBox = CreateDefaultSubobject<UBoxComponent>(TEXT("Entry Box"));
	EntryBox->bGenerateOverlapEvents = true;

	OnActorBeginOverlap.AddDynamic(this, &AInteractiveObject::BeginOverlap);
}

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

	OnActorBeginOverlap.AddDynamic(this, &AInteractiveObject::BeginOverlap);

	epressed = false;
	withinrange = false;
}

void AInteractiveObject::BeginOverlap(AActor* OverlappedActor, AActor* OtherActor)
{
	//if (typeid(OtherActor) == typeid(nightguard))
	//{
		UE_LOG(LogTemp, Log, TEXT("BeginOverlap"));
		nightguard = (ARSGameCharacter*) OtherActor;
		withinrange = true;
	//}
}

You should be able to remove the OnActorBeginOverlap at line 29 in your BeginPlay() function. Can you also set the box component to show during runtime to verify that you are overlapping with it. If you’re using a blueprint of your class you can uncheck the HiddenInGame checkbox. If you’re using the class directly, it would be EntryBox->SetHiddenInGame(false);. Depending on the behavior of your actor, you may also want to make the box component a child of your root component using EntryBox->SetupAttachment(meshcomp);.

Still not working :frowning:

I have a log in BeginOverlap, but it never actually works :frowning:

Since this appears to be working for me, could you provide the project you’re using for me to test directly?

Hang on, got it working. Not sure what i changed… UE4 is like that for me.
THank you so much!

Your tips have been great :slight_smile: