As you can see, they use different inputs. but what about UMaterial? there is simply no clue in the document. I even checked the engine source code, I can’t find anything!
how to assign texture map to a material is also a mystery.
sigh, I also found the function SetTextureParameterValue from an old example, but it is deprecated I think. I did global search in 4.8.1 source code, can’t find it anymore.
after looking at the engine’s source code, it seems to be allowed to use any name for texture. by default, it just replaces the diffuse color.
my problem was calling the function in the constructor of my AActor class. the program crashes because when the AActor constructor gets called, there is no GEngine object .
I moved stuff to the BeginPlay function, and it won’t crash anymore.
A good rule of thumb is to never call anything in the constructor that might fail. Generally in UE4 I just use the constructor to assign default values. I do most of the work in other functions like:
Regarding the texture names - in your material you create parameters that have names - like a texture parameter for example. That is the name you use when assigning parameters from c++.
Materials don’t work this way. This sounds like a lack of familiarity with the Material Editor / Material system in general. Basically you have to create a Material asset first, in the engine. You then need to create a texture sample parameter within that material, give it a name (e.g. TextureMap).
You then need to create a DynamicMaterialInstance of that material in code, and tell the mesh to use that DMI. Then, you can change the texture using SetTextureParameterValue. In order to get the texture, you could either create a UPROPERTY of type UTexture2D* (I think that’s the class) and specify it in a Blueprint, or you can use ConstructorHelpers::FObjectFinder instead if you have a static asset somewhere. Alternatively if you’ve created a procedural texture or a render target, you can just make it use that too.
UPROPERTY(EditDefaultsOnly, Category = "My Texture")
UTexture2D* TextureToUse;
MyClass.cpp
UDynamicMaterialInstance* MyDMI = SphereVisual->CreateAndSetMaterialInstanceDynamic(0)
if (MyDMI)
{
MyDMI->SetTextureParameterValue(TEXT("TextureMap"), TextureToUse);
}
The FName in SetTextureParameterValue isn’t the name of a texture, its the name of the parameter you want to assign that texture too.
EDIT:
Additionally, I would not use BeginPlay() to set initial properties. Instead there is a virtual function for all Actors that you can Override, called PostInitializeComponents() This function runs immediately after the construction script and after all default parameters have been set (including any overrides from Blueprint), meaning it’s safe to do most things there like DynamicMaterials etc. Just don’t forget to call Super::PostInitializeComponents(); if you do override it.
The reason to use that over BeginPlay(), is because BeginPlay is ONLY called by the GameMode when the game begins - which isn’t necessarily/always straight away.