I am trying to make simple grid system for my turn-based game, and I have decided to use decals to render tiles on the floor. One of specific functions my tile actor has is highlighting tile, when its color changes when selected/when accessible/etc. As for now, I have implemented highlighting like this:
// DaldosBasicGridTile.h
#pragma once
#include "CoreMinimal.h"
#include "DaldosGridTile.h"
#include "DaldosCoreTypes.h"
#include "DaldosBasicGridTile.generated.h"
UCLASS()
class DALDOS_API ADaldosBasicGridTile : public ADaldosGridTile
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ADaldosBasicGridTile();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category="Colors")
TMap<EHighlightMode, UMaterialInterface*> HighlightMaterials;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category="Colors")
UMaterialInterface* DefaultMaterial;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category="Decal")
UMaterial* DecalMaterial;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category="Decal")
FVector DecalSize;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category="Components")
UDecalComponent* Decal;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
virtual void SetHighlightMode(EHighlightMode HighlightMode) override;
};
// DaldosBasicGridTile.cpp
#include "Grid/DaldosBasicGridTile.h"
#include "Components/DecalComponent.h"
#include "Kismet/GameplayStatics.h"
// Sets default values
ADaldosBasicGridTile::ADaldosBasicGridTile()
{
// 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;
Decal = CreateDefaultSubobject<UDecalComponent>(TEXT("Decal"));
Decal->SetDecalMaterial(DecalMaterial);
Decal->SetRelativeRotation(FRotator(-90, 0, 0));
Decal->DecalSize = FVector(64.0f, 128.0f, 128.0f);
Decal->bDestroyOwnerAfterFade = true;
RootComponent = Decal;
// SetActorRotation(FRotator(-90, 0, 0));
}
// Called when the game starts or when spawned
void ADaldosBasicGridTile::BeginPlay()
{
Super::BeginPlay();
SetHighlightMode(EHighlightMode::SELECTED); // Just for debug purposes, to know, that SetHighlightMode actually works
}
// Called every frame
void ADaldosBasicGridTile::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void ADaldosBasicGridTile::SetHighlightMode(EHighlightMode HighlightMode)
{
Super::SetHighlightMode(HighlightMode);
UMaterialInterface* NewHighlightMaterial = HighlightMaterials.Contains(HighlightMode) ? HighlightMaterials[HighlightMode] : DefaultMaterial;
Decal->SetDecalMaterial(NewHighlightMaterial);
It works great and tile change its color on BeginPlay. However, when I try to change highlight mode during the gameplay (as a reaction to user’s input), tile doesn’t change its color. Its material is displayed correctly in debugger, but visually it is still the same one, that was applied on BeginPlay.
What could be the reason for that and how can I fix it?
Thank you in advance!