Announcement

Collapse
No announcement yet.

Struggling to adjust a material parameter via c++

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Struggling to adjust a material parameter via c++

    So in my Character header file i have these lines:
    Code:
            float testfloat;
    
    	void UpdateTeamColors(UMaterialInstanceDynamic* UseMID);
    
    	UPROPERTY(Transient)
    	TArray<UMaterialInstanceDynamic*> MeshMIDs;
    
    	void TeamColorVerify();
    in the Character cpp file i have this:
    Code:
    //in constructor
    testfloat = 0.f;
    
    //and these functions
    
    void ASmashyCharacter::UpdateTeamColors(UMaterialInstanceDynamic* UseMID)
    {
    	if (UseMID)
    	{
    		ASmashyPlayerState* MyPlayerState = Cast<ASmashyPlayerState>(PlayerState);
    		if (MyPlayerState)
    		{
    			float MaterialParam = (float)MyPlayerState->GetTeamNum();
    			UseMID->SetScalarParameterValue(TEXT("TeamColorIndex"), MaterialParam);
    		}
    	}
    }
    
    void ASmashyCharacter::UpdateTeamColorsAllMIDs()
    {
    	for (int32 i = 0; i < MeshMIDs.Num(); ++i)
    	{
    		UpdateTeamColors(MeshMIDs[i]);
    	}
    }
    
    void ASmashyCharacter::TeamColorVerify()
    {
    	ASmashyPlayerState* MyPlayerState = Cast<ASmashyPlayerState>(PlayerState);
    	for (int32 i = 0; i < MeshMIDs.Num(); ++i)
    	{
    		UpdateTeamColors(MeshMIDs[i]);
    	
    	testfloat += 1.f;
    	UMaterialInstanceDynamic* MI = UMaterialInstanceDynamic::Create(Mesh->GetMaterial(0), this);
    	MI->SetScalarParameterValue(TEXT("TeamColorIndex"), testfloat);
    
    	//float MaterialParam = (float)MyPlayerState->GetTeamNum();
    	//MeshMIDs[i]->SetScalarParameterValue(TEXT("TeamColorIndex"), testfloat);
    	}
    }
    Here's the problem. if i create a breakpoint at the start of UpdateTeamColorsAllMIDs(), it breaks, so the function is being run. it also breaks if i place a point at the start of the for loop.

    If i place a break point inside the for loop (UpdateTeamColors(MeshMIDs[i]), this break point is never triggered.

    So then i set up the final function just to see if i could debug this somewhat. TeamColorVerify() is triggered by a button press (e). placing breakpoints within the function confirms that it's being triggered fine. EXCEPT for within the for loop, the same line fails to trigger the break point as in the previous function.

    Also, annoyingly... TeamColorVerify() doesn't seem to adjust the TeamColorIndex parameter within the material that's applied to the character mesh.

    This means i have two problems. The first is that for some reason the function within the for loop is never triggered (super frustrating). And also the parameter isn't changed either. This is still the case if i uncomment the bottom two lines in TeamColorVerify().

    Any ideas?
    Lee Devonald
    Character Technical Artist.
    Portfolio: http://crazyferretstudios.com

    #2
    After creating your MID, you have to overwrite the original material with it:

    Code:
    UMaterialInstanceDynamic* MI = UMaterialInstanceDynamic::Create(Mesh->GetMaterial(0), this);
    Mesh->SetMaterial(0, MI)
    MI->SetScalarParameterValue(TEXT("TeamColorIndex"), testfloat);
    Alternatively, UPrimitiveComponent::CreateAndSetMaterialInstanceDynamic will handle both creating and replacing the material for you.

    Comment


      #3
      okay mark me down as a *****... forgot to actually fill the MeshMID's array with data -_-
      Lee Devonald
      Character Technical Artist.
      Portfolio: http://crazyferretstudios.com

      Comment


        #4
        Code:
        Fatal error: [File:D:\BuildFarm\buildmachine_++depot+UE4-Releases+4.5\Engine\Source\Runtime\CoreUObject\Private\Templates\Casts.cpp] [Line: 11] 
        Cast of GameUserSettings /Engine/Transient.GameUserSettings_0 to SmashyGameUserSettings failed
        this is destroying me, i've been trying to fix this for two hours but i have no idea how to fix it.
        Last edited by Almighty_gir; 11-27-2014, 10:09 PM.
        Lee Devonald
        Character Technical Artist.
        Portfolio: http://crazyferretstudios.com

        Comment


          #5
          A CastChecked is failing, most likely because the instance is null. You should only use CastChecked if something is guaranteed to not be null.

          Comment


            #6
            Yeah i've changed that to Cast, but it's still breaking. I've posted more info here:
            https://answers.unrealengine.com/que...to-cast-1.html
            Lee Devonald
            Character Technical Artist.
            Portfolio: http://crazyferretstudios.com

            Comment


              #7
              Ah, my bad, it's not null, but rather of the wrong type. The path /Engine/Transient.GameUserSettings_0 sounds like a GameUserSettings instance, not a SmashyGameUserSettings instance. Have you updated the GameUserSettingsClassName config in your project settings / DefaultEngine?

              Comment


                #8
                i had, two days ago... but i made a typo and didn't find it until last night. =/
                Lee Devonald
                Character Technical Artist.
                Portfolio: http://crazyferretstudios.com

                Comment

                Working...
                X