I have the following material:
I would like to change the value of IsSelected from 0 to 1 when an actor is clicked on.
I think I’m close. Here’s what I currently have:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "GameFramework/Actor.h"
#include "VehicalSocketComponent.h"
#include "VehiclePart.generated.h"
UCLASS(Blueprintable)
class EDITOR3_API AVehiclePart : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AVehiclePart();
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Called every frame
virtual void Tick(float DeltaSeconds) override;
// Get energy drain
virtual int32 GetEnergyContribution();
// Get health regen
virtual int32 GetHealthRegeneration();
protected:
UPROPERTY(BlueprintReadOnly, EditDefaultsOnly, Category = "Vehicle Part Properties")
FName DisplayName = FName(TEXT("Unnamed Part"));
UPROPERTY(BlueprintReadOnly, EditDefaultsOnly, Category = "Vehicle Part Properties")
int32 BlockCostToSpawn = 0;
UPROPERTY(BlueprintReadOnly, EditDefaultsOnly, Category = "Vehicle Part Properties")
int32 CreditCostToBuy = 0;
UPROPERTY(BlueprintReadOnly, EditDefaultsOnly, Category = "Vehicle Part Properties")
int32 CheaterCashCostToBuy = 0;
UPROPERTY(BlueprintReadOnly, EditDefaultsOnly, Category = "Vehicle Part Properties")
int32 HealthPoints = 0;
UPROPERTY(BlueprintReadOnly, EditDefaultsOnly, Category = "Vehicle Part Properties")
int32 DurabilityPoints = 0;
UPROPERTY(BlueprintReadWrite, VisibleAnywhere, Category = "Mesh")
UStaticMeshComponent * StaticMeshComponent;
private:
UMaterial * Material;
UMaterialInstanceDynamic * DynamicMaterial = nullptr;
void OnConstruction(const FTransform &) override;
void NotifyActorBeginCursorOver() override;
void NotifyActorEndCursorOver() override;
void NotifyActorOnClicked() override;
};
#include "Editor3.h"
#include "VehiclePart.h"
AVehiclePart::AVehiclePart()
{
// Set this actor to call Tick() every frame
PrimaryActorTick.bCanEverTick = true;
// Create and attach the scene component to root
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("Root Component"));
// Create and attach the static mesh component to our scene component
StaticMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Static Mesh Component"));
StaticMeshComponent->AttachTo(RootComponent);
static ConstructorHelpers::FObjectFinder<UMaterial> MaterialAsset(TEXT("Material'/Game/Materials/FancyPlastic.FancyPlastic'"));
if (MaterialAsset.Succeeded()) Material = (UMaterial *) MaterialAsset.Object;
}
void AVehiclePart::OnConstruction(const FTransform & Transform)
{
Super::OnConstruction(Transform);
// Setup the dynamic material
if (DynamicMaterial)
{
DynamicMaterial = UMaterialInstanceDynamic::Create(Material, this);
StaticMeshComponent->SetMaterial(0, DynamicMaterial);
StaticMeshComponent->SetMaterial(1, DynamicMaterial);
StaticMeshComponent->SetMaterial(2, DynamicMaterial);
}
}
void AVehiclePart::NotifyActorBeginCursorOver()
{
Super::NotifyActorBeginCursorOver();
UE_LOG(LogTemp, Warning, TEXT("BEGIN MOUSE OVER"));
}
void AVehiclePart::NotifyActorEndCursorOver()
{
Super::NotifyActorEndCursorOver();
UE_LOG(LogTemp, Warning, TEXT("END MOUSE OVER"));
}
void AVehiclePart::NotifyActorOnClicked()
{
Super::NotifyActorOnClicked();
static bool foo = false;
UE_LOG(LogTemp, Warning, TEXT("CLICK!"));
if (DynamicMaterial)
DynamicMaterial->SetScalarParameterValue(FName(TEXT("IsSelected")), foo);
foo = !foo;
}
// Called when the game starts or when spawned
void AVehiclePart::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AVehiclePart::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
/*for(int32 b = 0; b < Children.Num(); b++)
{
UE_LOG(LogTemp, Warning, TEXT("%s"), *(Children**->GetActorLabel()));
}*/
}
int32 AVehiclePart::GetEnergyContribution()
{
return 0;
}
int32 AVehiclePart::GetHealthRegeneration()
{
return 0;
}
I get the log message for the click but the material isn’t changing.
