Greetings
In my assessment that I am going through to determine if I am going to use UE4, I have a problem trying to get the basic default UE4 cube mesh to change its default material in using C++.
There is no tutorial, no guide no nothing on how to do this except some suggestions on which I have found in this forum of which the below code is the one that looks like it is the best solution, but it crashes the project. Then I have to rebuild or as I did several times copy a backup to recover the project. This code sits in a .cpp file in the class constructor below where the cube mesh is defined as in a previous post.
UMaterial *the_box_Material;
UMaterialInstanceDynamic *box_Material_dyn;
static ConstructorHelpers::FObjectFinder<UMaterial> Material(_T("Material'/Game//StarterContent/Materials/M_Metal_Steel.M_Metal_Steel'"));
if (Material.Succeeded()) {
the_box_Material = (UMaterial*)Material.Object;
if (the_box_Material == NULL)
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("the_box_Material == NULL "));
else {
box_Material_dyn = UMaterialInstanceDynamic::Create(the_box_Material, this);
if (box_Material_dyn == NULL)
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("box_Material_dyn == NULL"));
else
//BoxVisual->SetMaterial(0, the_box_Material);//** is it this**
BoxVisual->SetMaterial(0, box_Material_dyn); // **or as I believe this that should be used **
}
Is this code correct because I have seen variations on this that are claimed to work and I have spent the best part of a day trying out several different things and angles with no better success ? If I cannot get something as basic and what should be trivial as this to work I will have to consider not using UE4. Especially if there are going to be other unexpected incidences or prospect of loosing hours or days of work because it unexpectedly crashes.
Thanks in advance for any help or guidance here.
Regards
DominioN