Hello,
I am new to c++ and instead of using blueprints I decided to use c++ for my in-game widgets as a challenge. In public SCompoundWidget, I declared dynamic materials as.
UMaterialInterface* BaseMaterial;
UMaterialInstanceDynamic* DynamicMaterial;
TSharedPtr<FSlateMaterialBrush> InteractionBrush;
and in constructor
BaseMaterial = Cast<UMaterialInterface>(StaticLoadObject(UMaterialInterface::StaticClass(), nullptr, *MaterialName));
DynamicMaterial = UMaterialInstanceDynamic::Create(BaseMaterial, nullptr);
InteractionBrush = MakeShared<FSlateMaterialBrush>(*DynamicMaterial, FVector2D(50.0f, 50.0f));
DynamicMaterial->SetScalarParameterValue(InnerCircle, 0.499f);
DynamicMaterial->SetScalarParameterValue(OuterCircle, 0.501f);
DynamicMaterial->SetScalarParameterValue(MaterialOpacity, 0.0f);
But if I use this code dynamic material keep deleted by garbage collector and then Unreal engine crashes. Then I tried to add dynamic material to root
DynamicMaterial->AddToRoot();
This solved the problem but there must be a more professional solution to this. What is the proper way to create dynamic materials in SCompoundWidget? How can I prevent garbage collector delete it.
Also after player leaves interaction area this widget will be deleted so there must a automatic way to handles its life time. Thank you so much. As a new beginner I need help.