Dynamic binding

I am reading a book from packt pub the code is trying to teach me how to bind two functions about overlapping.

The following line in the book does not work at all.

//Bind delegates
	//OnActorBeginOverlap->AddDynamic(this, &AHelloSphere::MyOnBeginOverlap);
	//OnActorEndOverlap.AddDynamic(this, &AHelloSphere::MyOnEndOverlap);


void AHelloSphere::MyOnBeginOverlap(AActor* OtherActor)
{
	FString outputString;
	outputString = "Hello " + OtherActor->GetName() + "!";
	TextRenderComponent->SetText(FText::FromString(outputString));
}

void AHelloSphere::MyOnEndOverlap(AActor* OtherActor) 
{
	TextRenderComponent->SetText(NSLOCTEXT("AnyNs", "Any", "HelloWorld"));
}

Hey Revix2k16,

My guess is that the book was written before they changed the delegate for overlaps. The “new” way is as follows:

[OverlapActor.h]

#pragma once

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

UCLASS()
class AH474682_API AOverlapActor : public AActor
{
	GENERATED_BODY()
	
public:	
	AOverlapActor();

	UStaticMeshComponent *Mesh;
	USphereComponent *Collider;

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

[OverlapActor.cpp]

#include "AH474682.h"
#include "OverlapActor.h"

AOverlapActor::AOverlapActor()
{
	PrimaryActorTick.bCanEverTick = false;

	Collider = CreateDefaultSubobject<USphereComponent>( TEXT("Sphere") );
	Collider->SetSphereRadius( 128.f );
	Collider->OnComponentBeginOverlap.AddDynamic( this, &AOverlapActor::OnOverlap );
	SetRootComponent( Collider );

	Mesh = CreateDefaultSubobject<UStaticMeshComponent>( TEXT("Mesh") );
	Mesh->SetupAttachment( RootComponent );

	static ConstructorHelpers::FObjectFinder<UStaticMesh> MeshAsset( TEXT("StaticMesh'/Engine/BasicShapes/Cube.Cube'") );
	if( MeshAsset.Succeeded() )
	{
		Mesh->SetStaticMesh( MeshAsset.Object );
	}	
}

void AOverlapActor::OnOverlap(UPrimitiveComponent* OverlappedComponent,
	AActor* OtherActor,
	UPrimitiveComponent* OtherComp,
	int32 OtherBodyIndex,
	bool bFromSweep,
	const FHitResult & SweepResult)
{
	UE_LOG( LogTemp, Warning, TEXT("Overlapped Actor: %s" ), *OtherActor->GetName( ) );
}

I put your code into what I was doing for the book , the book implies it would work aka walking to sphere change the text.

But it does not this what it all looks like.

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

#include "HelloCode2.h"
#include "HelloSphere.h"


// Sets default values
AHelloSphere::AHelloSphere()
{
	// 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;
	// Our root component will be a sphere component that will inform us of overlaps and collisions
	USphereComponent* SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("RootComponent"));

	RootComponent = SphereComponent;

	// Assign properties to sphere component
	SphereComponent->InitSphereRadius(220.0f);

	SphereComponent->
		SetCollisionProfileName(TEXT("OverlapAllDynamic"));

	// Create and attach a sphere mesh
	UStaticMeshComponent* SphereVisual = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("SphereMesh"));
	SphereVisual->AttachTo(RootComponent);

	// Assign mesh to static mesh component through construction helper
	ConstructorHelpers::FObjectFinder<UStaticMesh> SphereAsset(TEXT("/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere"));


	// Adjust properties of the mesh if mesh asset was found
	if (SphereAsset.Succeeded())
	{
		SphereVisual->SetStaticMesh(SphereAsset.Object);
		SphereVisual->SetRelativeLocation(FVector(0.0f, 0.0f, -50.0f));
	}

	// Create the fire particle system
	UParticleSystemComponent* FireParticles = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("FireParticles"));

	FireParticles->AttachTo(SphereVisual);
	FireParticles->bAutoActivate = true;

	// Assign fire particle system to component
	ConstructorHelpers::FObjectFinder<UParticleSystem> FireVisual(TEXT("/Game/StarterContent/Particles/P_Fire.P_Fire"));

	if (FireVisual.Succeeded())
	{
		FireParticles->SetTemplate(FireVisual.Object);
	}

	// Initialize Text Render component
	TextRenderComponent = CreateDefaultSubobject<UTextRenderComponent>(TEXT("Text"));

	TextRenderComponent->AttachTo(SphereVisual);

	TextRenderComponent->SetRelativeLocation(FVector(0.0f, 0.0f, 110.0f));

	TextRenderComponent->SetHorizontalAlignment(EHTA_Center);
	TextRenderComponent->SetYScale(2.0f);
	TextRenderComponent->SetXScale(2.0f);
	TextRenderComponent->SetVisibility(true);
	TextRenderComponent->SetText(NSLOCTEXT("AnyNs", "Any", "HelloWorld"));


}

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

}

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

}


void AHelloSphere::MyOnBeginOverlap(UPrimitiveComponent* OverlappedComponent,
	AActor* OtherActor,
	UPrimitiveComponent* OtherComp,
	int32 OtherBodyIndex,
	bool bFromSweep,
	const FHitResult &SweepResult)
{
	FString outputString;
	outputString = "Hello " + OtherActor->GetName() + "!";
	TextRenderComponent->SetText(FText::FromString(outputString));
}

void AHelloSphere::MyOnEndOverlap(UPrimitiveComponent* OverlappedComponent,
	AActor* OtherActor,
	UPrimitiveComponent* OtherComp,
	int32 OtherBodyIndex,
	bool bFromSweep,
	const FHitResult &SweepResult)
{
	TextRenderComponent->SetText(NSLOCTEXT("AnyNs", "Any", "HelloWorld"));
}





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

#pragma once

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

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

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

protected:
	// On Overlap implementation
	UFUNCTION()
		void MyOnBeginOverlap(UPrimitiveComponent* OverlappedComponent,
			AActor* OtherActor,
			UPrimitiveComponent* OtherComp,
			int32 OtherBodyIndex,
			bool bFromSweep,
			const FHitResult &SweepResult);

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

	class UTextRenderComponent* TextRenderComponent;
	
};

Hey Revix2k16,

There was a couple issues. The first was that UTextRenderComponent couldn’t be found because the header for it wasn’t included in the HelloSphere.cpp file.

Secondly, there MyOnEndOverlap( ) function had the wrong setup for the delegate; it is different than MyOnBeginOverlap( ).

Lastly, you were missing the “OnComponentBeginOverlap” and “OnComponentEndOverlap” delegate assignments for SphereComponent.

[HelloSphere.h]

#pragma once

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

UCLASS()
class HELLOCODE2_API AHelloSphere : public AActor
{
	GENERATED_BODY()
	
public:
	AHelloSphere();
	virtual void BeginPlay() override;
	virtual void Tick(float DeltaSeconds) override;

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

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

	class UTextRenderComponent* TextRenderComponent;	
};

[HelloSphere.cpp]

#include "HelloCode2.h"
#include "HelloSphere.h"
#include "Runtime/Engine/Classes/Components/TextRenderComponent.h"

AHelloSphere::AHelloSphere()
{
	PrimaryActorTick.bCanEverTick = true;

	USphereComponent* SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("RootComponent"));
	SphereComponent->InitSphereRadius(220.0f);
	SphereComponent->SetCollisionProfileName(TEXT("OverlapAllDynamic"));
	SphereComponent->OnComponentBeginOverlap.AddDynamic( this, &AHelloSphere::MyOnBeginOverlap );
	SphereComponent->OnComponentEndOverlap.AddDynamic( this, &AHelloSphere::MyOnEndOverlap );
	SetRootComponent( SphereComponent );

	UStaticMeshComponent* SphereVisual = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("SphereMesh"));
	SphereVisual->SetupAttachment(RootComponent);

	ConstructorHelpers::FObjectFinder<UStaticMesh> SphereAsset(TEXT("/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere"));
	if( SphereAsset.Succeeded() )
	{
		SphereVisual->SetStaticMesh(SphereAsset.Object);
		SphereVisual->SetRelativeLocation(FVector(0.0f, 0.0f, -50.0f));
	}

	UParticleSystemComponent* FireParticles = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("FireParticles"));
	FireParticles->bAutoActivate = true;
	FireParticles->SetupAttachment(SphereVisual);

	ConstructorHelpers::FObjectFinder<UParticleSystem> FireVisual(TEXT("/Game/StarterContent/Particles/P_Fire.P_Fire"));
	if( FireVisual.Succeeded() )
	{
		FireParticles->SetTemplate(FireVisual.Object);
	}

	TextRenderComponent = CreateDefaultSubobject<UTextRenderComponent>(TEXT("Text"));
	TextRenderComponent->SetRelativeLocation(FVector(0.0f, 0.0f, 110.0f));
	TextRenderComponent->SetHorizontalAlignment(EHTA_Center);
	TextRenderComponent->SetYScale(2.0f);
	TextRenderComponent->SetXScale(2.0f);
	TextRenderComponent->SetVisibility(true);
	TextRenderComponent->SetText(NSLOCTEXT("AnyNs", "Any", "HelloWorld"));
	TextRenderComponent->SetupAttachment(SphereVisual);
}

void AHelloSphere::BeginPlay()
{
	Super::BeginPlay();
}

void AHelloSphere::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}


void AHelloSphere::MyOnBeginOverlap(UPrimitiveComponent* OverlappedComponent,
	AActor* OtherActor,
	UPrimitiveComponent* OtherComp,
	int32 OtherBodyIndex,
	bool bFromSweep,
	const FHitResult &SweepResult)
{
	FString outputString = "Hello " + OtherActor->GetName() + "!";
	if( TextRenderComponent )
	{
		TextRenderComponent->SetText(FText::FromString(outputString));
	}
}

void AHelloSphere::MyOnEndOverlap(UPrimitiveComponent* OverlappedComponent,
	AActor* OtherActor,
	UPrimitiveComponent* OtherComp,
	int32 OtherBodyIndex)
{
	if( TextRenderComponent )
	{
		TextRenderComponent->SetText(NSLOCTEXT("AnyNs", "Any", "HelloWorld"));
	}
}

Thanks man a lot can you tell more about what passes in function for begin overlap?

UPrimitiveComponent OverlappedComponent :*

The Component that is overlapping.

AActor OtherActor,*

The Actor that is overlapping.

UPrimitiveComponent OtherComp,*

The Component child of OtherActor that is overlapping.

int32 OtherBodyIndex,

The body index of the Component that is overlapping.

bool bFromSweep,

If the collision came from sweep; when you move something you have the option to “sweep” from one position to another, which will track its movement from point A to B instead of teleporting it. bFromSweep will tell you if the collision came from the sweep movement.

const FHitResult &SweepResult

FHitResult is a lot of extra data from the collision.