How to create overlap events with C++ with BaseCharacter CapsuleComponent ?

Hello,

I’m new to C++ (and have like 2 days of experience using it)
I would like to recreate something as simple as overlapping an actor with the base character’s CapsuleComponent.


The overlapped actor only has a StaticMesh as root component.
And the Character doesn’t have any extra components.

It’s easy in BP, but in C++ I tried to use this in the Base Character class.

.h

public:
UFUNCTION()
void OnPickupOverlap(UPrimitiveComponent* OnComponentBeginOverlap, UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);

.cpp

//constructor
 	GetCapsuleComponent()->OnComponentBeginOverlap.AddDynamic(this, &ThisClass::OnPickupOverlap);
void ACPPtrainCharacter::OnPickupOverlap(UPrimitiveComponent* OnComponentBeginOverlap, UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	if (AMyCPPActor* MyActorPtr = Cast<AMyCPPActor>(OtherActor))
	{
		MyActorPtr->Destroy();
	}
}

MyCPPActor is set with

.cpp

MeshComp = CreateDefaultSubobject<UStaticMeshComponent>;
RootComponent = MeshComp ;
MeshComp->SetCollisionProfileName("OverlapAll", true);
MeshComp->SetGenerateOverlapEvents(true);

.h

public:
UPROPERTY(EditAnywhere, BlueprintReadWrite)
UStaticMeshComponent* MeshComp;

And I can’t get it to work at all… the overlap events never trigger.
I think I need help with this. :sweat_smile:

personally I like to bind the Overlap events in

public: virtual void PostInitializeComponents() override;

when you bind the delegate in the constructor it is possible the components has not been initialized to bind too, so the binding never actually happens. though I will presume that in your ACPPtrainCharacter you have done

GetCapsuleComponent()->SetGenerateOverlapEvents(true);

all colliders that are to interact with overlaps need to be set to Generate Overlap Events.

does the overlap even trigger at all? (trying putting a UE_LOG() before your if statement in the overlap check)

(typically in C++ we look at the .h first then the .cpp

are you wrapping these manually in a blueprint, or are you having the Editor perform the fake blueprint wrap on these? if you instantiate a raw C++ Actor it will be wrapped in a fake blueprint, so it would be more efficient, and consistent to do that wrapping yourself.

if the these Colliders are exposed with UPROPERTY(EditAnywhere) you should be able to select the relevant Actor in the Level Hierarchy then inspect the collider during PIE and look at the Collision Section to see if “Generate Overlap Events” is ticked =true.

1 Like

Ty for your reply.

does the overlap even trigger at all? (trying putting a UE_LOG() before your if statement in the overlap check)

No I tried to print a UE_LOG before the “if” statement but nothing returns.
All actors have SetGenerateOverlapEvents(true);
I’m trying to do everything in C++ without need for blueprints, directly dragging the C++ Actor from content browser into the level.


Everything is correctly set for an overlap but the event simply never triggers…

I also forgot to mention that the AddDynamic keyword returns an error when I try to initialize the method. I’m thinking it’s just intellisense not working properly on that one?

I think this might be a problem with your Overlap function signature:
you have

void OnPickupOverlap("UPrimitiveComponent* OnComponentBeginOverlap," UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);

the expected signature by the engine is:

void OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);

so that isn’t intelisense not understanding the macro (if you are using VS 2022 update 17.2 or later it does understand most all the macros, it still doesn’t suggest the correct enum values for UFUNCTION() or UPROPERTY(), but it does know what those macros are and what they are expecting) it is the signature not matching the expectation.

for the “just pulling out raw C++ into the level” I would strongly encourage to manually wrap them in a blueprint (the engine will wrap them in a fake blueprint anyways but). By at least wrapping them manually you get the blueprint graph which some things are actually more efficient/consistent to do in the blueprint graph:

  • assigning references to things in the Content Browser/Drawer otherwise you will need to assign them on each instance in the level hierarchy, or use Runtime-Resolved-Hard-References (similar to the issue you are having here with you delegate not being bound because the signature is not what the engine is expecting, but instead your Tank object doesn’t appear to load because some one moved/renamed the mesh)
  • adding functionality to BlueprintNativeEvent and BlueprintImplementableEvent
  • allowing proper instancing with editor default values when instantiating in different levels
  • a designer doesn’t need to learn your C++ code base to add functionality
  • for rapid testing you can mock up in the classes blueprint to pull into C++ in the same class

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