Here I am trying to make a cpp conversion of the blueprint tut.
//RadarComponent.h
#pragma once
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "RadarComponent.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class CPPRADARTUTORIAL_API URadarComponent : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
URadarComponent();
protected:
// Called when the game starts
virtual void BeginPlay() override;
public:
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason);
};
//RadarComp.cpp
#include "RadarComponent.h"
#include "CppRadarHUD.h"
#include "Kismet/GameplayStatics.h"
// Sets default values for this component's properties
URadarComponent::URadarComponent()
{
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don't need them.
PrimaryComponentTick.bCanEverTick = true;
// ...
}
// Called when the game starts
void URadarComponent::BeginPlay()
{
Super::BeginPlay();
if (ACppRadarTutorialHUD* Radar = Cast<ACppRadarTutorialHUD>(UGameplayStatics::GetPlayerController(GetWorld(), 0)->GetHUD()))
{
Radar->AddToRadar(GetOwner());
}
// ...
}
// Called every frame
void URadarComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
// ...
}
void URadarComponent::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
if (ACppRadarTutorialHUD* Radar = Cast<ACppRadarTutorialHUD>(UGameplayStatics::GetPlayerController(GetWorld(), 0)->GetHUD()))
{
Radar->AddToRadar(GetOwner());
}
}
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/HUD.h"
#include "CppRadarHUD.generated.h"
UCLASS()
class ACppRadarHUD : public AHUD
{
GENERATED_BODY()
public:
ACppRadarHUD();
/** Primary draw call for the HUD */
virtual void DrawHUD() override;
UFUNCTION(BlueprintCallable)
void AddToRadar(AActor* Actor);
UFUNCTION(BlueprintCallable)
void ClearFromRadar(AActor* Actors);
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "RadarArray")
TArray<AActor*> RadarActors;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "2DVector")
FVector2D ScreenSize;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "2DVector")
FVector2D RadarStartLocation;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Floats")
float ObjectDistanceScale = 25.0f;
UFUNCTION()
float GetRadarCenterPosition();
UFUNCTION()
float GetDotPosition(AActor* Actor);
void ReceiveDrawHUD(int32 SizeX, int32 SizeY);
class AActor* ActorRef;
private:
/** Crosshair asset pointer */
class UTexture2D* CrosshairTex;
};
#include "CppRadarHUD.h"
#include "Engine/Canvas.h"
#include "Engine/Texture2D.h"
#include "TextureResource.h"
#include "CanvasItem.h"
#include "UObject/ConstructorHelpers.h"
#include "Kismet/GameplayStatics.h"
#include "Kismet/KismetMathLibrary.h"
#include "RadarComponent.h"
#include "Components/ActorComponent.h"
ACppRadarHUD::ACppRadarHUD()
{
// Set the crosshair texture
static ConstructorHelpers::FObjectFinder<UTexture2D> CrosshairTexObj(TEXT("/Game/FirstPerson/Textures/FirstPersonCrosshair"));
CrosshairTex = CrosshairTexObj.Object;
}
void ACppRadarHUD::DrawHUD()
{
Super::DrawHUD();
// Draw very simple crosshair
// find center of the Canvas
const FVector2D Center(Canvas->ClipX * 0.5f, Canvas->ClipY * 0.5f);
// offset by half the texture's dimensions so that the center of the texture aligns with the center of the Canvas
const FVector2D CrosshairDrawPosition( (Center.X),
(Center.Y + 20.0f));
// draw the crosshair
FCanvasTileItem TileItem( CrosshairDrawPosition, CrosshairTex->Resource, FLinearColor::White);
TileItem.BlendMode = SE_BLEND_Translucent;
Canvas->DrawItem( TileItem );
}
void ACppRadarHUD::AddToRadar(AActor* Actor)
{
RadarActors.Add(Actor);
}
void ACppRadarHUD::ClearFromRadar(AActor* Actors)
{
RadarActors.Remove(Actors);
}
float ACppRadarHUD::GetRadarCenterPosition()
{
float resultScreenSizeAndRadarX;
float resultScreenSizeAndRadarY;
resultScreenSizeAndRadarX = ScreenSize.X * RadarStartLocation.X;
resultScreenSizeAndRadarY = ScreenSize.Y * RadarStartLocation.Y;
return resultScreenSizeAndRadarX;
return resultScreenSizeAndRadarY;
}
float ACppRadarHUD::GetDotPosition(AActor* Actor)
{
FVector2D ResultX;
FVector2D ResultY;
ResultX.X = UKismetMathLibrary::InverseTransformLocation(UGameplayStatics::GetPlayerPawn(GetWorld(), 0)->GetActorTransform(), Actor->GetActorLocation()).X / ObjectDistanceScale;
ResultY.Y = UKismetMathLibrary::InverseTransformLocation(UGameplayStatics::GetPlayerPawn(GetWorld(), 0)->GetActorTransform(), Actor->GetActorLocation()).Y / ObjectDistanceScale;
return ResultX.X;
return ResultY.Y;
}
void ACppRadarHUD::ReceiveDrawHUD(int32 SizeX, int32 SizeY)
{
ScreenSize.X = SizeX;
ScreenSize.Y = SizeY;
GetOwningPlayerController()->GetPawn()->FindComponentByClass<ARadarComponent>();
for ( )
{
}
}