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

**URL:** <https://forums.unrealengine.com/t/how-to-create-overlap-events-with-c-with-basecharacter-capsulecomponent/1372004>\
**Category:** C++\
**Tags:** overlap-actors, capsule, unreal-engine, CPP, question\
**Created:** [November 7, 2023, 10:53pm UTC](https://forums.unrealengine.com/t/how-to-create-overlap-events-with-c-with-basecharacter-capsulecomponent/1372004 "2023-11-07T22:53:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MR.JAJ](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MR.JAJ](https://forums.unrealengine.com/u/MR.JAJ)\
**Post date:** [November 7, 2023, 10:53pm UTC](https://forums.unrealengine.com/t/how-to-create-overlap-events-with-c-with-basecharacter-capsulecomponent/1372004/1 "2023-11-07T22:53:27Z")

</div>

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.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/1/1/a115f05ca18cc1d3326076f5acd76c2aa9afd0d9.png)  
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

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

```

.cpp

```auto
//constructor
 	GetCapsuleComponent()->OnComponentBeginOverlap.AddDynamic(this, &ThisClass::OnPickupOverlap);

```

```auto
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

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

```

.h

```auto
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. 😅

---

<div class="post-metadata">

**Author:** ![gardian206](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@gardian206](https://forums.unrealengine.com/u/gardian206)\
**Post date:** [November 7, 2023, 11:17pm UTC](https://forums.unrealengine.com/t/how-to-create-overlap-events-with-c-with-basecharacter-capsulecomponent/1372004/2 "2023-11-07T23:17:36Z")

</div>

personally I like to bind the Overlap events in

```auto
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

```auto
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.

---

<div class="post-metadata">

**Author:** ![MR.JAJ](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MR.JAJ](https://forums.unrealengine.com/u/MR.JAJ)\
**Post date:** [November 8, 2023, 10:54am UTC](https://forums.unrealengine.com/t/how-to-create-overlap-events-with-c-with-basecharacter-capsulecomponent/1372004/3 "2023-11-08T10:54:21Z")

</div>

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.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/0/c/7/0c7d10c0a27f4b3e36137ebbac6de95cdd1fd53f.jpeg)  
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?

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/e/b/ceb76cc6a14f1a8a55c800ac4d7a5a7e880207b7.png)

---

<div class="post-metadata">

**Author:** ![gardian206](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@gardian206](https://forums.unrealengine.com/u/gardian206)\
**Post date:** [November 8, 2023, 4:07pm UTC](https://forums.unrealengine.com/t/how-to-create-overlap-events-with-c-with-basecharacter-capsulecomponent/1372004/4 "2023-11-08T16:07:12Z")

</div>

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

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

```

the expected signature by the engine is:

```auto
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

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [December 8, 2023, 4:07pm UTC](https://forums.unrealengine.com/t/how-to-create-overlap-events-with-c-with-basecharacter-capsulecomponent/1372004/5 "2023-12-08T16:07:24Z")

</div>

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