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(""));
= 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();
// 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;
};
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.
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.