How to get Parameter Index of a Dynamic Material?

So there’s a function SetScalarParameterByIndex(int32 ParameterIndex, float Value) and my question is: How can I get the correct index to get this function running if I have many parameters? I tried the GetAllScalarParameterInfo, but after I print the index out of that, it’s always -1

Here’s the code:

				UMaterialInstanceDynamic* HealthBarMat = HealthBar->GetDynamicMaterial();
				HealthBarMat->SetScalarParameterByIndex(6, 0.9f);

				TArray<FMaterialParameterInfo> Info1;

				TArray<FGuid> Info2;

				HealthBarMat->GetAllScalarParameterInfo(Info1, Info2);
				for (int i = 0; i < Info1.Num(); i++)
				{
					UE_LOG(LogTemp, Warning, TEXT("Name = %s = %d"), *Info1[6].Name.ToString(), Info1[6].Index);
				}

Hi LordaxPL,

Why do you need the index? The comment on the docs for it states “Use the cached value of OutParameterIndex above to set the vector parameter ONLY on the exact same MID.” - I don’t think it’s a generic function you can use like that.

1 Like

You aren’t actually logging the info of “allscalarparameters”. You just log whatever info is at index 6 multiple times (and hope that the index exists). Not sure why that would be -1 though. The cache may not be initialized or it may only be initialized in editor environments.

1 Like

Hi, I thought it would be a bit faster to set the parameter by its index as it would just compare two int32 values (unlike in the SetScalarParameterValue function which takes the name of the parameter and compares it). I think comparing strings is more expensive than comparing int values, right? So, just to sum up, I can’t use this function? I’ve played with it a bit and couldn’t figure out the right index so I eventually settled for the function which takes the name. Thanks for the reply btw! :slight_smile:

1 Like

Oh, yeah, I forgot to change that, this was just for debugging. In the original code it was Info1[i] and the results were the same, it was printing out -1 as the index of all scalar parameters. So is there a way to actually get the index? Or SetScalarParameterValue which takes the name of the parameter isn’t that bad and I should use this one instead? Thanks!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.