Download

[UMaterialInstanceDynamic] Compile problem

Hello !

I was following this tutorial on eu4 : https://www.youtube.com/channel/UCnO…O1WwKFq-5Pvj0Q
At 13.00m he create a UMaterialInstanceDynamic and he does the SetVectorParametersValue on his material. I’ve try to do it in the basicProjectile.cpp file (OnHit function).
But visual studio tells me I can’t do that. Also, Visual studio auto completion didnt work on MatInst.



void AMaze_V_1_0_0Projectile::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
    // Only add impulse and destroy projectile if we hit a physics
    if ((OtherActor != NULL) && (OtherActor != this) && (OtherComp != NULL) && OtherComp->IsSimulatingPhysics()) {
        OtherComp->AddImpulseAtLocation(GetVelocity() * 100.0f, GetActorLocation());


        UMaterialInstanceDynamic *MatInst = OtherComp->CreateAndSetMaterialInstanceDynamic(0);

        if (MatInst) {

            // Error Here on MatInst ... Dont know why
MatInst->SetVectorParametersValue("Color", FLinearColor::MakeRandomColor);
        }

        Destroy();
    }
}

Sorry for my english and thx for any help.

Hello,

First, you made a typo:

Bad:
MatInst->SetVectorParametersValue(“Color”, FLinearColor::MakeRandomColor);
Good:
MatInst->SetVectorParameterValue(“Color”, FLinearColor::MakeRandomColor());

Then, the reason why visual studio is complaining is the GENERATED_UCLASS_BODY() macro inside MaterialInstanceDynamic.h

To keep it simple, at the end of the macro, there is a “public:”, but visual studio doesn’t know it.
The result: it thinks that the function is private and this is why it doesn’t autocomplete you.

But it should compile no problem.

Cheap fix if you have the engine source and you want to have the autocomplete:
Search for “GENERATED_UCLASS_BODY()” in the entire solution and replace it with “GENERATED_UCLASS_BODY() public:”

Note:
I do this everytime I get a new engine source code and everything works fine, but make sure that it does not break anything for you before doing it.

Thanks a lot ! :slight_smile:

Hum It still doest compile even without the “s”

I’ve also added the public in the header file but the auto complet is still broken.


void AMaze_V_1_0_0Projectile::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
    // Only add impulse and destroy projectile if we hit a physics
    if ((OtherActor != NULL) && (OtherActor != this) && (OtherComp != NULL) && OtherComp->IsSimulatingPhysics()) {
        OtherComp->AddImpulseAtLocation(GetVelocity() * 100.0f, GetActorLocation());


        FVector scale = OtherComp->GetComponentScale();
        scale *= 0.8f;

        if (scale.GetMin() < 0.5) {
            OtherActor->Destroy();
        } else {
            OtherComp->SetWorldScale3D(scale);

        }

        UMaterialInstanceDynamic *MatInst = OtherComp->CreateAndSetMaterialInstanceDynamic(0);

        if (MatInst) {
            MatInst->SetVectorParameterValue("Color", FLinearColor::MakeRandomColor);
        }

        Destroy();
    }
}

What is the compile error message?

Projectile.cpp (54): Note: You must not use the source type or the manufacturer’s overload solution? was ambiguous?
Projectile.cpp (54): error C2664: ‘void UMaterialInstanceDynamic :: SetVectorParameterValue (FName, FLinearColor)’ ?: Unable to convert argument 2 of ‘FLinearColor (__cdecl *) (void)’ in ‘FLinearColor’

Ok I found how to fix it … Sorry I was doing **** … The “()” was missing at the end of MakeRandomColor()

But I still dont understand why I need to include this :


 #include "Runtime/Engine/Classes/Materials/MaterialInstanceDynamic.h" 

To get the auto complet on a MaterialInstanceDynamic Object

Now if I remove this include I have an error on the MatInst but on ue4 side it compile


         UMaterialInstanceDynamic *MatInst = OtherComp->CreateAndSetMaterialInstanceDynamic(0);

        if (MatInst) {
            MatInst->SetVectorParameterValue("Color", FLinearColor::MakeRandomColor());
        }

Oh wow, I didn’t see the missing () either, sorry.

And for the include, it is better to keep it.
Most of the classes are not included by default.

If you don’t know where to find it:
Go into the API and search for it.
Go at the bottom and you will find the include file.

While being there, you can also take a look at the dependency module, if you’re not adding it into your build.cs file, you will probably get a link error during compile.