Download

Compiling shaders every time when I play test game

Dear experts,

I have a few floor tiles and on BeginPlay() of my Character, I want random tiles to change color for a few seconds and then change back again.

For now my BeginPlay() looks like this:
So far I only pick one random tile and change the color on it by “myMesh->SetMaterial(0,tileCurrent->OnMaterial”
“OnMaterial” is a UMaterial instance and I set it on the Tile BP to a material instance I have.

void AMainCharacter::BeginPlay()
{
	Super::BeginPlay();

	TArray<AActor*> foundTiles;

	UGameplayStatics::GetAllActorsOfClass(GetWorld(), AFloorTile::StaticClass(), foundTiles);
	int32 anzahlTiles = foundTiles.Num() -1;
	
	int32 zufall = FMath::RandRange(1,anzahlTiles);

	int32 index =0;
	AFloorTile* tileCurrent = nullptr;

	tileCurrent = Cast<AFloorTile>(foundTiles[zufall]);

	UStaticMeshComponent* myMesh = Cast<UStaticMeshComponent>(tileCurrent->GetComponentByClass(UStaticMeshComponent::StaticClass()));
	myMesh->SetMaterial(0,tileCurrent->OnMaterial);

}

It works, but every time I test play the game the engine starts compiling shaders of my tiles. I know it is the shaders for the tiles because when I reduce the tiles on the floor less shaders are being compiled.

Why? Is “SetMaterial()” the wrong function to accomplish this? Or do I have to check something on the “OnMaterial” (UMaterial) ?

Thank you for any ideas!

Best regards, Peter

You want to dig into Creating and Using Material Instances | Unreal Engine Documentation

1 Like

“OnMaterial” is a UMaterial instance and I set it on the Tile BP to a material instance I have.

1 Like

thank you both, sorry about my late reply, that makes sense. Using a material instance solved the issue.

So you were lying in the original post about using an instance? :stuck_out_tongue:

Not completely: I have it as UInstance in the code but not in the engine. Hm. I have to double check that :sweat_smile:
EDIT: I checked and switched my code to using “UMaterialInstance”, now the compile issue of the shaders is gone.

1 Like