I have this code:
// Fill out your copyright notice in the Description page of Project Settings.
#include "GroundTile.h"
#include "GameFramework/PlayerController.h"
// Sets default values
AGroundTile::AGroundTile()
{
// Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
// Create the TileMesh component and set it as the root component
TileMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("TileMesh"));
RootComponent = TileMesh;
CollisionBox = CreateDefaultSubobject<UBoxComponent>(TEXT("CollisionBox"));
CollisionBox->SetupAttachment(TileMesh);
// Enable mouse over events
TileMesh->SetGenerateOverlapEvents(true);
// TODO: Enable mouse click events
// Bind the mouse over events
CollisionBox->OnComponentBeginOverlap.AddDynamic(this, &AGroundTile::OnMouseEnter);
CollisionBox->OnComponentEndOverlap.AddDynamic(this, &AGroundTile::OnMouseLeave);
}
// Called when the game starts or when spawned
void AGroundTile::BeginPlay()
{
Super::BeginPlay();
TileMesh->SetMaterial(0, DefaultMaterial);
APlayerController* PlayerController = GetWorld()->GetFirstPlayerController();
if (PlayerController)
{
PlayerController->bShowMouseCursor = true;
PlayerController->bEnableClickEvents = true;
PlayerController->bEnableMouseOverEvents = true;
}
}
// Called every frame
void AGroundTile::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void AGroundTile::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
}
void AGroundTile::OnMouseEnter(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if (OtherActor && (OtherActor != this) && OtherComp)
{
TileMesh->SetMaterial(0, HighlightedMaterial);
}
}
void AGroundTile::OnMouseLeave(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
if (OtherActor && (OtherActor != this) && OtherComp)
{
TileMesh->SetMaterial(0, DefaultMaterial);
}
}