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"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

Setting dynamic material instance color C++

Hello, I am trying to change material color of standard UE4 Mannequin skeletal mesh but there is nothing hapenning both in viewport and in game.

    AEnemyBase::AEnemyBase()
     {
         mesh_ = GetMesh();
         mesh_->SetupAttachment(GetCapsuleComponent());    
     
         TArray<class UMaterialInterface*> materials = GetMesh()->GetMaterials();
         for (int i = 0; i < materials.Num(); i++)
         {
             UMaterialInstanceDynamic* matInstance_ = UMaterialInstanceDynamic::Create(materials[i], this);
     
             matInstance_->SetVectorParameterValue(FName(TEXT("_BodyColor")), characterColor);
             GetMesh()->SetMaterial(i, matInstance_);
         }
     }

Product Version: UE 4.17
Tags:
more ▼

asked Jan 06 '18 at 08:35 PM in C++ Programming

avatar image

komilll
3 1 2 4

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

1 answer: sort voted first

Hey there, first of all you dont need the FName(TEXT, just do "_BodyColor", what you are doing there is kind of the same as doing 3 + 1 - 1, instead of using just 3. Second, i would create the dynamic material in the begin play and using this command:

 UMaterialInterface * Material = GetMesh()->GetMaterial(i);
 UMaterialInstanceDynamic* matInstance = GetMesh()->CreateDynamicMaterialInstance(i, Material);

 if(matInstance != nullptr)
      matInstance->SetVectorParameterValue("_BodyColor", characterColor);

See if that works.

more ▼

answered Jan 06 '18 at 08:46 PM

avatar image

xlar8or
6k 161 129 304

avatar image komilll Jan 06 '18 at 09:07 PM

Hey, thanks for your answer, it works perfectly as it should.

But do you have any idea why code that you've sent works in BeginPlay but doesn't work in class contructor?

avatar image xlar8or Jan 08 '18 at 09:01 AM

Hey good to know, the inner code of unreal probably has some verification that prevents it from running on the contructor.

(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