Add onbeginoverlap an onendoverlap event to instanced static mesh.

Hi

I am new to c++ in Unreal engine, so sorry for my noobness.

I am trying to get an overlap/hit event from instanced static meshes in an actor component. I searched a lot, but I only seen the

“mesh->OnComponentBeginOverlap.AddDynamic” form, what is not exist in instance mesh, only “instancedmesh->OnComponentBeginOverlap.Add” (or __Internal_AddDynamic)

but I have no idea how to link it to my OverlapBegin function this way.

Here is how I declared it the header:

UPROPERTY(VisibleAnywhere)
		UInstancedStaticMeshComponent * Instancedchainlink_side;

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

UFUNCTION()
		void OverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);

And here is How I tried to set up the instance mesh:

	Instancedchainlink_side = CreateDefaultSubobject<UInstancedStaticMeshComponent>(TEXT("Instancedchainlink_side"));
	//SetRootComponent(InstancedStaticMeshComponent);
	//InstancedStaticMeshComponent->AttachTo(this->rootcom);
	Instancedchainlink_side->SetMobility(EComponentMobility::Movable);
	//Instancedchainlink_side->SetCollisionProfileName(UCollisionProfile::NoCollision_ProfileName);
	Instancedchainlink_side->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
	Instancedchainlink_side->SetGenerateOverlapEvents(true);
	//Instancedchainlink_side->SetCollisionResponseToAllChannels(ECR_Overlap);
	Instancedchainlink_side->SetEnableGravity(true);
	Instancedchainlink_side->SetSimulatePhysics(true);
	

	Instancedchainlink_side->OnComponentBeginOverlap.Add(&OverlapBegin); 

Othervise the instanced mesh works perfectly, I have collision with it, and it appear and work correctly.

I think I created this question too soon, because I found a solution:

FScriptDelegate ScriptDelegate;
ScriptDelegate.BindUFunction(this, FName("OverlapBegin"));
Instancedchainlink_side->OnComponentHit.Add(ScriptDelegate);

Also, if someone want to use this solution, and want to get the index too, you can get it like this:

void UCerletChain::OverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	UE_LOG(LogTemp, Warning, TEXT("name: > %s, index > %d"), *OverlappedComponent->GetName(), SweepResult.ElementIndex);
	
}

I hope it ill help someone.

Of course if there is a better/more elegant solution, I would gladly hear about it.

Hi @Badytheprogram

Welcome to the Unreal Engine Forums.

TDLR:
Use AddDynamic, you were binding to the wrong delegate, and the instance index is SweepResult.Item not ElementIndex

OnComponentBeginOverlap is a FComponentBeginOverlapSignature (a dynamic multicast delegate). AddDynamic is a macro defined on every dynamic multicast. So it essentual just wraps the FScriptDelegate + BindUFunctionyou did manually. InteliSense sometimes doesn’t show it as it’s a macro which is why it says it doesn’t exist. Helpful I know :slight_smile:

Try it like this:

Instancedchainlink_side->OnComponentBeginOverlap.AddDynamic(this, &UCerletChain::OverlapBegin);
Instancedchainlink_side->OnComponentEndOverlap.AddDynamic(this, &UCerletChain::OverlapEnd);

Also, SweepResult is only populated when bFromSweep == true and I think for SweepResult.ElementIndex it’s wrong for the instance index. The instance index on an ISMC hit/sweep lives in FHitResult::Item, not ElementIndex. Try do it like this and see if it works better:

void UCerletChain::OverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
const int32 InstanceIndex = bFromSweep ? SweepResult.Item : INDEX_NONE;
UE_LOG(LogTemp, Warning, TEXT(“ISMC %s, instance %d, other actor %s”),
*OverlappedComponent->GetName(), InstanceIndex, *GetNameSafe(OtherActor));
}

Hi. Thanks for the welcoming.

This is my original problem, the Instancedchainlink_side->OnComponentBeginOverlap

don’t have AddDynamic entry, only Add and AddUnique for me.

That’s why I created this question. Sorry, if it was hardly understandable, my English is not the best.

Edit:

Never mind, I am an idiot. for somehow, I skipped the “InteliSense sometimes doesn’t show it as it’s a macro which is why it says it doesn’t exist.” part.

Thanks for the correction.

No problem :slight_smile: Glad you got it. Hope it’s all working now :smiley:. Guessing you are using Visual Studio? One of the reason I really like Jetbrains Rider.