Is StarMaterialInstanceDynamic defined anywhere? It seems like you haven’t created and initialized the right variables yet.
I’ve done what you’re trying to do, lemme paste some example code. In this instance “Material” is a pre-created base material named “GameMaterial” with parameters in it.
In your class definition in the header file:
UPROPERTY(EditAnywhere) UMaterial * Material;
UPROPERTY(EditAnywhere) UMaterialInstanceDynamic * MaterialInstance;
In your cpp file:
AGameActor::AGameActor(const class FObjectInitializer& FObjectInitializer) : Super(FObjectInitializer)
{
GameMesh = FObjectInitializer.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("GameMesh"));
RootComponent = GameMesh;
ConstructorHelpers::FObjectFinder<UMaterial> MatFinder(TEXT("Material'/Game/Game/GameMaterial'"));
if (MatFinder.Succeeded()) {
Material = MatFinder.Object;
MaterialInstance = UMaterialInstanceDynamic::Create(Material, Package);
}
}
When you want to update a parameter:
void AGameActor::SetGameMaterialParameter(FName ParameterName, UTexture * Texture) {
UE_LOG(LogTemp, Warning, TEXT("[Game] Setting material for %s"), *ParameterName.ToString());
MaterialInstance->SetTextureParameterValue(ParameterName, Texture);
}
I’ve left out the code for assigning the material to the mesh as I don’t have it on hand (I’m using UMaterialInstanceConstant for a different reason, which does everything differently. Go figure.) This code also assumes you’re assigning a staticmesh via code, so you can chop that bit out if you like too.
Anyway, I’m sure you know about writing to object properties in C++, eg. mygameobject->color = FColor::Red;
The UPROPERTY() part above means several things:
- The type after it inherits from the UObject class at some point
- It’s exposed to the user interface in the editor (if you click a spawned actor of this type, this member will appear in the list in the details panel)
- It’s memory-managed by UE after that, anything referenced by that UPROPERTY() won’t be destroyed until the actor doing the referencing no longer exists. This is a Very Good Thing.
- It can be accessible by blueprints (with the right arguments in UPROPERTY())
So it’s not JUST for blueprints but most of what it does is to do with blueprints and the UI, but it has its place even if you’re just doing C++. I recently solved a stack overflow by inheriting my classes from UObject and letting them be managed by UE.
You can do all of the above in blueprints too, by getting an actor, getting its mesh, getting its material, creating a dynamic material instance and hooking it up then setting parameter values. In fact, I did this in blueprints first to work it out and then moved it to C++.