Hello! Good morning, afternoon and/or evening!
I’ve been trying to bind a function to the OnComponentBeginOverlap through AddDynamic, but so far there hasn’t been a proper callback from the function.
I remember doing this same process for a school project back in UE4, but I’m currently working in UE5, and so far I haven’t seen if there is something that has changed from 4 to 5 regarding this binding.
Edit: to be specific, it’s the Version: 5.2.1-26001984+++UE5+Release-5.2
This is what I’ve put on the code so far, starting with the .h file:
#include "CoreMinimal.h"
#include "Components/BoxComponent.h"
#include "C_PlayerSlider.generated.h"
UCLASS()
class FANTASY_ARCADE_API AC_PlayerSlider : public APawn
{
GENERATED_BODY()
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Brick_Properties")
UBoxComponent* BrickBoxColl;
// Constructor
AC_PlayerSlider();
// Function to bind to the Overlap event
UFUNCTION()
void OnOverlapPlayer(class UPrimitiveComponent* ThisComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
// Function called after all Components in Pawn are Initialized
virtual void PostInitializeComponents() override;
}
Now with the .cpp file:
// Constructor of the Pawn
AC_PlayerSlider::AC_PlayerSlider()
{
// Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
BrickBoxColl = CreateDefaultSubobject<UBoxComponent>(TEXT("Brick_Box"));
BrickBoxColl->AttachToComponent(BrickMeshComponent, FAttachmentTransformRules::KeepRelativeTransform);
BrickBoxColl->SetBoxExtent(FVector(64.0, 48.0, 32.0));
}
// Function to be called when triggering overlapping
void AC_PlayerSlider::OnOverlapPlayer(class UPrimitiveComponent* ThisComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
// Used just to verify functionality of the binding
if (GEngine)
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Blue, TEXT("LETS GOOOO"));
}
// Called when Components in Pawn have been initialized
void AC_PlayerSlider::PostInitializeComponents()
{
Super::PostInitializeComponents();
// Wanted to make sure the Actor itself can have collisions
SetActorEnableCollision(true);
// All of these lines of codes are in to verify that all this block is working as intended
if (BrickBoxColl != NULL)
{
// This works
if (GEngine)
GEngine->AddOnScreenDebugMessage(-1, 2.0f, FColor::Blue, TEXT("BrickBoxColl found"));
// This also works
BrickBoxColl->SetVisibility(true);
// This as well
BrickBoxColl->SetHiddenInGame(false);
// And just to be sure, it does work.
BrickBoxColl->SetLineThickness(10.0);
// And yet, there is no response from this bind in runtime.
BrickBoxColl->OnComponentBeginOverlap.AddDynamic(this, &AC_PlayerSlider::OnOverlapPlayer);
}
The print messages from certain lines of code work as intended.
But there is no response at all when overlapping, and I even did a Blueprint to test out my intended function (just like Brick Breaker, its intended to push the ball forward).