OnActorBeginOverlap in C++

Hello.

I have a C++ Class called Health that creates a StaticMeshComponent and sets it as Root Component

PickupMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("PickupMesh"));
	RootComponent = PickupMesh;	

After, through blueprints, I add to the scene a new Static Mesh, a PointLight and a Box Collider.

Then, in the event graph I did this:

I want to move all the OnActorBeginOverlap event to Visual Studio, as C++ Code.

How?
How do I obtain the OnActorBeginOverlap event/delegate in Visual Studio?

Thanks!

I tried doing this way:

#include "MyProjectCPP.h"
#include "Pickup.h"
#include "Engine.h"

// Sets default values
APickup::APickup()
{	
	PrimaryActorTick.bCanEverTick = true;

	PickupMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("PickupMesh"));
	RootComponent = PickupMesh;	

	RotationSpeed = 1.5f;
	bIsActive = true;

	PickupMesh->OnComponentBeginOverlap.AddDynamic(this, &APickup::OnPickup);
}

void APickup::OnPickup()
{
	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("PICKED UP!")));
}

But it gives me errors…

Actually, I achieved my purpose… but I DON’T KNOW HOW???

UFUNCTION(BlueprintCallable, Category = "OnPickup")
void OnPickup();

and then the implementation

void AHealth::OnPickup()
{
	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("HEALTH PICKED!")));
}

I don’t know how, but know everytime I touch the Health Mesh in the world, “HEALTH PICKED” gets printed out.

How? I don’t say anywhere to execute THAT function any time I overlap the Health… it’s weird!

Help!

You are on right track with the AddDynamic(…). However, parameters of the function passed in matter, thus getting it to work would require OnPickup() to be something like:

Using OnComponentBeginOverlap:

// In constructor
PickupMesh->OnComponentBeginOverlap.AddDynamic(this, &APickup::OnPickup);

// The method itself (Note the parameters)
void APickup::OnPickup(AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
 {
     GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("PICKED UP!")));
 }

Or using OnActorBeginOverlap

// In constructor:
OnActorBeginOverlap.AddDynamic(this, &AHealth::OnPickup);

// Method itself (Note the parameters)
void AHealth::OnPickup(class AActor* OtherActor)
{
     GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("PICKED UP!")));
 }

What kinda errors? Crashes, compilation errors?

Wait, are you using OnActorBeginOverlap or OnComponentBeginOverlap? If you use OnActorBeginOverlap then the function you add to this event should have signature

void function(class AActor* OtherActor) 

but if you use OnComponentBeginOverlap then it’ll be

void function(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)

Yeah but look at the class code: http://pastebin.com/Ytebd9V0

As you can see, there’s only one function called OnPickup() that when it’s called prints out a string.

Ok, but WHY does the string get printed out when I overlap the Health EVEN THOUGH there’s no link between the OnActorBeginOverlap event and the OnPickup()

Do you understand? OnPickup() is a simple function that isn’t supposed to be called from anywhere! Instead, it gets called when I overlap the actor in the world… why? O.o

Also, in the blueprint of that class, I detached any link from the event.

Ummm… Seems weird… Have you tried re-creating the blueprint? I’ve had issues with blueprints not updating their event delegate lists properly when events were added from C++ code. Like for some reason the function was still being bound to the event even though I had removed the line from constructor. Only way I could resolve those issues was to delete the derived blueprint and re-create it.

Ok I re-created the blueprints and its behavior is as I expected to be. Anyway, I added OnActorBeginOverlap.AddDynamic(this, &AHealth::OnPickup); in the constructor and

void AHealth::OnPickup(AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("on actor begin overlap works!")));
}

In the function OnPickup().

Now, I get 2 errors:

Unreal Engine:

Visual Studio:

Oh, my bad. If you use the OnActorBeginOverlap then correct function parameters are as simple as:

void AHealth::OnPickup(class AActor* OtherActor)

The parameters I gave in my answer would work if event was bound using:

PickupMesh->OnComponentBeginOverlap.AddDynamic(this, &APickup::OnPickup);

EDIT: Updated the answer

Man it works!

But first of all will you tell me the difference between OnActorBeginOverlap and OnComponentBeginOverlap?

Second, how do I use a CastToFirstPersonCharacter inside the OnPickup() function in C++?

With Blueprints it’s just a node, but in C++ how do I achieve that?

I’m not 100% sure, but I think that OnActorBeginOverlap is called when something overlaps the whole actor (any of its components), while OnComponentBeginOverlap is called only when something overlaps the specific component.

What comes to casting, AFirstPersonCharacter* OtherCharacter = Cast(OtherActor); should be sufficient. However, note that if OtherActor is not AFirstPersonCharacter, then cast returns NULL so you’ll have to take that in account.

EDIT: I forgot to specify the class for the cast, this should now be correct.

Mhm, i get errors.

I posted a new question since it’s another issue: Cast to First Person Character C++ - Character & Animation - Unreal Engine Forums

Whoops, I forgot template type arguments from my comment… I’ll write a answer to the new question

Everything over here here is fine, except the

void function(class AActor* OtherActor)  // signature now changed to 
 
void function(class AActor* ThisActor, AActor* OtherActor)