1 min read

[17/50] Auto assign Unreal materials in a declarative manner

Stable Diffusion returns this image for the prompt: "An artwork for unreal materials."
Stable Diffusion returns this image for the prompt: "An artwork for unreal materials."

Continuing with our code samples on how we pursue data-driven game play with Unreal Engine. Below is a sample explaining our setup to auto-assign materials to defined slots in a declarative manner.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "ParameterizedMaterialSlot.generated.h"

USTRUCT(BlueprintType)
struct FSlotMaterial
{

	GENERATED_USTRUCT_BODY()

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	FName Tag;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	int SlotId { 0 };

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	UMaterialInterface* Material = nullptr;
};

UCLASS()
class GAME_API AParameterizedMaterialSlot : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AParameterizedMaterialSlot();

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	bool MatchGrandChildren = false;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	TArray<FSlotMaterial> MaterialTagMap;

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

};