Hello, I want to create a c++ component that, when a player is close to the item shows an icon. I have created the class that inherits from AActor class and that class works fine, but when I converted the class into a component it doesn’t work. Back in the editor it doesn’t even show the debug lines of the USpheareComponent. Anyway here is my code: .h file : // Fill out your copyright notice in the Description page of Project Settings.
#pragma once
include “CoreMinimal.h”
include “Components/ActorComponent.h”
include “InteractionIconComponent.generated.h”
class USphereComponent;
class UBillboardComponent;
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class UNTITLED_HORROR_GAME_API UInteractionIconComponent : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component’s properties
UInteractionIconComponent();
protected:
// Called when the game starts
virtual void BeginPlay() override;
public:
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
private:
UFUNCTION()
void OnOverlapBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
UFUNCTION()
void OnOverlapEnd(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);
private:
USphereComponent* InnerCollision;
USphereComponent* OuterCollision;
UBillboardComponent* InnerSprite;
UBillboardComponent* OuterSprite;
private:
UPROPERTY(EditAnywhere, Category = Radius)
int OuterRadius;
UPROPERTY(EditAnywhere, Category = Radius)
int InnerRadius;
UPROPERTY(EditAnywhere, Category = Textures)
UTexture2D* OuterTexture;
UPROPERTY(EditAnywhere, Category = Textures)
UTexture2D* InnerTexture;
};
and
.cpp file : include “InteractionIconComponent.h”
include “Components/SphereComponent.h”
include “Components/BillboardComponent.h”
include “GameFramework/Character.h”
// Sets default values for this component’s properties
UInteractionIconComponent::UInteractionIconComponent()
{
PrimaryComponentTick.bCanEverTick = false;
// Initialize Components
OuterCollision = CreateDefaultSubobject<USphereComponent>(TEXT("OuterCollision"));
InnerCollision = CreateDefaultSubobject<USphereComponent>(TEXT("InnerCollision"));
OuterSprite = CreateDefaultSubobject<UBillboardComponent>(TEXT("OuterSprite"));
InnerSprite = CreateDefaultSubobject<UBillboardComponent>(TEXT("InnerSprite"));
// Bind Overlap Events
OuterCollision->OnComponentBeginOverlap.AddDynamic(this, &UInteractionIconComponent::OnOverlapBegin);
OuterCollision->OnComponentEndOverlap.AddDynamic(this, &UInteractionIconComponent::OnOverlapEnd);
InnerCollision->OnComponentBeginOverlap.AddDynamic(this, &UInteractionIconComponent::OnOverlapBegin);
InnerCollision->OnComponentEndOverlap.AddDynamic(this, &UInteractionIconComponent::OnOverlapEnd);
OuterCollision->SetSphereRadius(OuterRadius);
InnerCollision->SetSphereRadius(InnerRadius);
InnerSprite->SetSprite(InnerTexture);
OuterSprite->SetSprite(OuterTexture);
InnerSprite->SetVisibility(false);
OuterSprite->SetVisibility(false);
InnerSprite->SetHiddenInGame(false);
OuterSprite->SetHiddenInGame(false);
}
// Called when the game starts
void UInteractionIconComponent::BeginPlay()
{
Super::BeginPlay();
InnerSprite->SetSprite(InnerTexture);
OuterSprite->SetSprite(OuterTexture);
SetIsVisualizationComponent(true);
}
// Called every frame
void UInteractionIconComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
}
void UInteractionIconComponent::OnOverlapBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
ACharacter* PlayerCharacter = Cast(OtherActor);
if (!PlayerCharacter)
return;
if (OverlappedComp == OuterCollision)
{
OuterSprite->SetVisibility(true);
}
else if (OverlappedComp == InnerCollision)
{
OuterSprite->SetVisibility(false);
InnerSprite->SetVisibility(true);
}
UE_LOG(LogTemp, Warning, TEXT("Inside"));
}
void UInteractionIconComponent::OnOverlapEnd(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
ACharacter* PlayerCharacter = Cast(OtherActor);
if (!PlayerCharacter)
return;
if (OverlappedComp == OuterCollision)
{
OuterSprite->SetVisibility(false);
}
else if (OverlappedComp == InnerCollision)
{
OuterSprite->SetVisibility(true);
InnerSprite->SetVisibility(false);
}
UE_LOG(LogTemp, Warning, TEXT("Inside"));
}
Help would be greatly appreciated!