How to use OnActorBeginOverlap c++ code?

Hi;

My player pawn is extended from Character class. In C++, I use GetCapsuleComponent() to get collision component , and then I use “OnComponentBeginOverlap” to call my function ,when my pawn overlaps other actors (in my case, Pickup actor).
Based on UE4 Documents I have to use codes something like this:

GetCapsuleComponent()->OnComponentBeginOverlap.AddDynamic(this, &AMyCharacter::OnOverlapBegin); 

Problem is , there is no AddDynamic() in “OnComponentBeginOverlap” ! and I can’t find out the problem…!

Thanks.
MSD.

2 Likes

You need to define the function you are going to use for the OnOverlapBegins.

Here is an example:

[h]



    // Overlap
    UFUNCTION( )
    void BeginOverlap(UPrimitiveComponent* OverlappedComponent, 
                      AActor* OtherActor, 
                      UPrimitiveComponent* OtherComp, 
                      int32 OtherBodyIndex, 
                      bool bFromSweep, 
                      const FHitResult &SweepResult );


[cpp]



AMyActor::AMyActor()
{
     Collider->OnComponentBeginOverlap.AddDynamic( this, &AMyActor::BeginOverlap );
}

void AMyActor::BeginOverlap(UPrimitiveComponent* OverlappedComponent, 
                                AActor* OtherActor, 
                                UPrimitiveComponent* OtherComp, 
                                int32 OtherBodyIndex, 
                                bool bFromSweep, 
                                const FHitResult &SweepResult )
{
   // Overlap
}


6 Likes

Hi and thank you for reply.
I know that I have to define a function for OnOverlapBegins (and I did) , But my problem is as I explained. When I type GetCapsuleComponent()->OnComponentBeginOverlap the visual studio intellisense does not show AddDynamic() and it just contains “Add” , “AddUnique” , “__Internal_AddDynamic” and “__Internal_AddUnique” …! and there is not any AddDynamic() …

Please check out this image :

&stc=1

That is just because Intellisense isn’t always correct or robust enough to show everything.

To clarify, just because Intellisense doesn’t function, doesn’t mean the code is wrong. There are many instances where Intellisense gives false information because Visual Studio can be quite slow or plain wrong.

Thank you guys , You’re right , It solved …

Yeah need that 100 core i7.

2 Likes

I’m guessing you’re using Visual Studio for your project. You could always try adding Jetbrains ReSharper C++ plugin, and that should improve the intellisense outside of some macros.

Just as my experience, for some UE4 versions you need to move this binding code to BeginPlay otherwise event will not trigger.


Collider->OnComponentBeginOverlap.AddDynamic( this, &AMyActor::BeginOverlap );

8 Likes

Are there any other gotchas that can cause this to fail?

  • binding must be done in BeingPlay() and not in Ctor()
  • the bound callback function must be marked UFUNCTION() in the header
  • both colliding objects must have GenerateOverlapEvents() set to true
    All this is set and it still does not work. :frowning:
1 Like

In UE 4.24 the signature of overlap delegate seems to be


void OnOwnerBeginOverlap(AActor* owner, AActor* otherActor);

1 Like

Vahid, I’d buy you a beer if I could. This has been driving me crazy all night and isn’t documented anywhere. Thank you so much.

The reason you want to put this in BeginPlay() is that UE4 uses class default objects, which have different behavior based on how they are constructed. There’s a thread on this that explains a lot. Make sure that you have NO runtime code in your constructor; for example, if you try to use a value that is a UPROPERTY() in the constructor, that variable value won’t be set by a Blueprint child class at that time, so you will get the default value.

What is CDO? - Programming & Scripting - Unreal Engine Forums.

3 Likes

In case someone else wonder - the real reason is that delegates has no “AddDynamic” method at all, hence intellisence provides no such option for you. “AddDynamic” is a helper macro which expands into __Internal_AddDynamic() call and make it easier to format parameters for you.

I thought this was the case but for me the opposite was true. I had it in BeginPlay() and events would not fire but when I moved it to constructor the overlap events worked.