Search code examples
c++unreal-engine4unrealscript

how does OnOverlapBegin_Implementation means and OnOverlapBegin differs?


I am confused how the

UFUNCTION(BlueprintNativeEvent, Category = Collision) void OnOverlapBegin(UPrimitiveComponent* Comp, AActor* otherActor, UPrimitiveComponent* otherComp, int32 otherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);

and

virtual void OnOverlapBegin_Implementation(UPrimitiveComponent* Comp, AActor* otherActor, UPrimitiveComponent* otherComp, int32 otherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)

works.


Solution

  • Essentially, this is a question about the BlueprintNativeEvent function specifier. So the top function definition you have up there would be in a class' header file.

    UFUNCTION(BlueprintNativeEvent, Category = Collision) 
    void OnOverlapBegin(UPrimitiveComponent* Comp, AActor* otherActor, UPrimitiveComponent* otherComp, int32 otherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
    

    And because of that function specifier, the function is meant to be overriden by a blueprint class that is a subclass of whatever C++ class this function is defined in. Now, the keyword here is meant. Therefore, it is technically optional for a blueprint to override this function. However, the function may still be used by the blueprint or elsewhere in the C++ class. So if there isn't an overridden version of the function above, then the default implementation of the function is used instead. And you would define the default method in the header file and implement the method in the C++ file using the signature in the bottom half of your question.

    virtual void OnOverlapBegin_Implementation(UPrimitiveComponent* Comp, AActor* otherActor, UPrimitiveComponent* otherComp, int32 otherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
    

    Although, I believe that you don't even need this function definition in your header file to actually implement the function. So you can just go straight to your C++ class and implement the default OnOverlapBegin function like so,

    void RandomClass::OnOverlapBegin_Implementation(UPrimitiveComponent* Comp, AActor* otherActor, UPrimitiveComponent* otherComp, int32 otherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) 
    {
        // called if not overridden by blueprint or subclass
    }
    

    Click here for more information on interfaces in Unreal Engine.

    Click here, here, and here for more information on the BlueprintNativeEvent function specifier.