Here I created AActor instance and add data member USphereComponent and binding dynamic events on what will be called. Also here is OverlappedActors pointer of AActor vectors represents overlapped actors binding dynamically on generated overlap events.
Header File
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Runtime/Engine/Classes/Components/SphereComponent.h"
#include "SphereActor.generated.h"
UCLASS(Blueprintable)
class TESTCODE_API ASphereActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ASphereActor();
// Sphere Overlapping Begin Event
UFUNCTION()
void OnBeginOverlap(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
// Sphere Overlapping End Event
UFUNCTION()
void OnEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
private:
// Sphere component data member for holding
UPROPERTY(EditAnywhere, Category = "Gameplay Collision")
USphereComponent* SphereComponent;
// Holding Actors that overlapped on SphereComponent
UPROPERTY(EditAnywhere)
TArray<AActor*> OverlappedActors;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
Source File
// Fill out your copyright notice in the Description page of Project Settings.
#include "SphereActor.h"
#include "Runtime/Core/Public/Math/UnrealMathUtility.h"
// Sets default values
ASphereActor::ASphereActor()
{
// 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;
SphereComponent = CreateDefaultSubobject<USphereComponent>("Collision Sphere");
SphereComponent->SetSphereRadius(120.0f);
SphereComponent->OnComponentBeginOverlap.AddDynamic(this, &ASphereActor::OnBeginOverlap);
SphereComponent->OnComponentEndOverlap.AddDynamic(this, &ASphereActor::OnEndOverlap);
}
// Called when the game starts or when spawned
void ASphereActor::BeginPlay()
{
Super::BeginPlay();
}
void ASphereActor::OnBeginOverlap(UPrimitiveComponent * OverlappedComp, AActor * OtherActor, UPrimitiveComponent * OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)
{
// Assume you calculated precision, FMath::RoundToFloat makes you more predictable
auto y = FMath::RoundToFloat(1.1);
// Add element to vector
if(OtherActor) OverlappedActors.Add(OtherActor);
}
void ASphereActor::OnEndOverlap(UPrimitiveComponent * OverlappedComp, AActor * OtherActor, UPrimitiveComponent * OtherComp, int32 OtherBodyIndex)
{
// Remove actor.
if (OtherActor) OverlappedActors.Remove(OtherActor);
}
// Called every frame
void ASphereActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
for (auto SomeActor : OverlappedActors)
{
auto f = FVector::Dist(SphereComponent->GetComponentLocation(), SomeActor->GetActorLocation());
UE_LOG(LogTemp, Warning, TEXT("distance is %f"), f);
}
}
You may check for more using FNumberFormattingOptions struct that exists to create more precised float conversion explained by Rama on wiki: Here