OnComponentBeginOverlap not called

Hello, I’m trying to pick up a weapon and to attach it to my character using OnComponentBeginOverlap and AttachToComponent, but it seems that it is not working, and I’m not getting any errors, though. Here is the .cpp file :

// Fill out your copyright notice in the Description page of Project Settings.

#include "gmpl.h"
#include "MainCharacter.h"
#include "Engine.h"
#include "Gun.h"


// Sets default values
AGun::AGun()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	WeaponRoot = CreateDefaultSubobject<USceneComponent>(TEXT("Weapon Root"));
	RootComponent = WeaponRoot;

	WeaponMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Weapon Mesh"));
	WeaponMesh->AttachToComponent(WeaponRoot, FAttachmentTransformRules::SnapToTargetNotIncludingScale);

	WeaponBox = CreateDefaultSubobject<UBoxComponent>(TEXT("Weapon Sphere"));
	WeaponBox->SetWorldScale3D(FVector(1.0f, 1.0f, 1.0f));
	WeaponBox->bGenerateOverlapEvents = true;
	WeaponBox->OnComponentBeginOverlap.AddDynamic(this, &AGun::OnPlayerEnterBox);
	WeaponBox->AttachToComponent(WeaponRoot, FAttachmentTransformRules::SnapToTargetNotIncludingScale); 
	
}

// Called when the game starts or when spawned
void AGun::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void AGun::Tick( float DeltaTime )
{
	Super::Tick( DeltaTime );

}

void AGun::OnPlayerEnterBox(UPrimitiveComponent * OverlappedComp, AActor * OtherActor, UPrimitiveComponent * OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)
{
	GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::Purple, TEXT("OVERLAP"));

	AMainCharacter *Character = Cast<AMainCharacter>(this->GetOwner());
	PlayerMesh = Character->GetMesh();
	PlayerMesh->GetSocketLocation(TEXT("RightHand"));

	if (Character != nullptr)
	{
		if (Character->GetMesh() != nullptr)
		{
			WeaponMesh->AttachToComponent(Character->GetMesh(), FAttachmentTransformRules::SnapToTargetIncludingScale, "RightHand_Socket");
		}
	}
}

Here is the .h file :

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "GameFramework/Actor.h"
#include "Gun.generated.h"


UCLASS()
class GMPL_API AGun : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AGun();

	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	
	// Called every frame
	virtual void Tick( float DeltaSeconds ) override;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon Mesh")
	class USkeletalMeshComponent *WeaponMesh;
	
	UPROPERTY(EditAnywhere)
	USceneComponent *WeaponRoot;

	UPROPERTY(EditAnywhere)
	UShapeComponent *WeaponBox;

	UPROPERTY()
	USkeletalMeshComponent *PlayerMesh;

	UFUNCTION()
	void OnPlayerEnterBox(UPrimitiveComponent *OverlapComp, AActor *OtherActor, UPrimitiveComponent *OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult);
};

So, if it was working, a debug message should appear, but it doesn’t. Could someone enlighten me on what’s wrong ?

Well, after trying to run the code with the local windows debugger, it seems like it cannot work because Character is a nullptr. How could I fix this ?

When creating a suboject, I always use SetupAttachment rather than AttachToComponent. This was new in 4.12.

Additionally, I’d check your collision settings on the weapon box. In blueprint, if you make a class inheriting from this, you should be able to see all its properties, and from there check what collision settings you have. You can of course set all these in code, and the blueprint isn’t needed, it just may make seeing what you currently have easier.

On line 47, it looks like you set Character by casting this->GerOwner() as AMainCharacter. However the owner wouldn’t be the Actor you’re looking for in this instance.

What I think you’re looking for is OtherActor, the second parameter in the overlap function. That would be the character Actor who triggered the overlap. Replace “this->GetOwner()” with “OtherActor” and it should work for you.

I think I see why. You create the weapon mesh component in the constructor, but you may need to create it on the character in order to have it show up on that actor. I always use NewObject to create a component before calling AttachToComponent on it.

Try something like this:

WeaponMesh = NewObject<USkeletalMeshComponent>(Character, USkeletalMeshComponent::StaticClass()); 
WeaponMesh->AttachToComponent(Character->GetMesh(), FAttachmentTransformRules::SnapToTargetIncludingScale, "RightHand_Socket");

If you’re looking for a visual effect of the gun snapping from its starting position to Character’s hand, create Character’s weapon mesh as a separate pointer and set WeaponMesh to null. That way the gun will appear in Character’s hand at the same time it disappears from the world.

Thank you for this quick answer : ) So, instead of using AttachToComponent, I used SetupAttachment to attach the weapon to the socket. I also made a blueprint class inherited from the c++ class, and I noticed that I am running with the OverlapAllDynamic collision preset. So now, I can enter the box but the editor crashes : /

OK, I’ll try it : )

Well, it stopped crashing and it is overlapping, but I can’t get the weapon to the socket.

Yup, I think it worked, I had to replace WeaponMesh by WeaponRoot too. Some scaling to do with the mesh, and that should do it. Thank you very much ^^

No problem!