x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Dynamic Materials doesn't work after calling SetMaterial()

Hello!

Dynamic materials work perfectly as long as I create them in PostInitializeComponents or in the constructor. But if I at some point try to execute the same bit of code during gameplay it stops working.

Here's what I have:

 void AWBaseCharacter::PostInitializeComponents()
 {
     Super::PostInitializeComponents();
     ReCreateDynamicMaterials();
 }
 
 void AWBaseCharacter::ReCreateDynamicMaterials()
 {
     USkeletalMeshComponent* mesh = GetMesh();
     for (int32 i = 0; i < mesh->GetNumMaterials(); ++i)
     {
         mesh->CreateAndSetMaterialInstanceDynamic(i);
     }
 }
 void AWBaseCharacter::SetCharacterSkin_Multi_Implementation(const FString& aSkin)
 {
     const static FString ContextString("GENERAL");
     auto rowHandle = mCharacterSkinTable->FindRow<FCharacterSkinLookupTable>(*aSkin, ContextString);
     if (!rowHandle)
     {
         UE_LOG(WLog, Log, TEXT("No character skin matching lookup: %s"), *aSkin);
         return;
     }
 
     auto animBluePrint = rowHandle->AnimationBlueprint;
     auto skelMesh = rowHandle->SkeletalMesh;
     auto bodyMat = rowHandle->MaterialBody;
     auto headMat = rowHandle->MaterialHead;
 
     GetMesh()->SetSkeletalMesh(skelMesh);
     GetMesh()->SetAnimInstanceClass(animBluePrint->GetAnimBlueprintGeneratedClass());
     GetMesh()->SetMaterial(0, headMat);
     GetMesh()->SetMaterial(1, bodyMat);
     ReCreateDynamicMaterials();
 }

 void AWBaseCharacter::SetCharacterColor_Multi_Implementation(int32 aTeam)
 {
     USkeletalMeshComponent* mesh = GetMesh();
     for (int32 i = 0; i < mesh->GetNumMaterials(); ++i)
     {
         if (auto mat = Cast<UMaterialInstanceDynamic>(mesh->GetMaterial(i)))
         {
             mat->SetVectorParameterValue(TEXT("playerclr"), AWGameState::GetTeamColor(aTeam));
         }
     }
 }

if I omitt the "GetMesh()->SetMaterial()" it works fine as it did before, but I desperately need to be able to set the materials. Any idea how to fix this?

PS: This isn't the entire code as it would bloat the entire page, but I call SetCharacterColor_Multi elsewhere, which works fine as long as I haven't called SetMaterial beforehand.

Product Version: Not Selected
Tags:
more ▼

asked Mar 04 '15 at 10:07 PM in C++ Programming

avatar image

Omberone
314 16 17 33

avatar image MickD777 Mar 05 '15 at 01:37 PM

What is the behavior when you do that code? A little more info could help!

BTW, auto keyword can be very useful but it also is harder to understand code. I'd say not to use it like you do, but that's only my opinion ;)

avatar image Omberone Mar 05 '15 at 02:20 PM

The code I posted were only touching the relevant bits, I'll update with the SetVectorParameter I use as well. Basically I'm trying to switch mesh, animation blueprint and materials on a character during runtime.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

Solved it.

 if (ParameterValue->ParameterValue != Value)
 {
     ParameterValue->ParameterValue = Value;
     // Update the material instance data in the rendering thread.
     GameThread_UpdateMIParameter(this, *ParameterValue);
     CacheMaterialInstanceUniformExpressions(this);
 }

this tidbit of code inside MaterialInstance.cpp made sure that my new parameter was never getting set. It compared my old value to my new one, and found no differences. I now first change the value to a random one, and then reset it to what I want it to be - this forces the material to update its' cached parameter references.

more ▼

answered Mar 05 '15 at 03:16 PM

avatar image

Omberone
314 16 17 33

avatar image MickD777 Mar 05 '15 at 03:29 PM

Nice one! Will keep that in my if it ever happens to me.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question