I have a widget that owns a const FSlateBrush* IconBrush;
I load the brush in the widget constructor
void SKInventoryListItem::Construct(const FArguments& InArgs)
{
UID = InArgs._UID;
...
IconBrush = CreateSlateBrush(UKObjectManager::GetPickupObjectTexture(UID), InArgs._ItemHeight, InArgs._ItemHeight);
....
When I need to paint it, the object is invalid.
int32 SKInventoryListItem::OnPaint(const FPaintArgs& Args, const FGeometry& AllottedGeometry, const FSlateRect& MyClippingRect, FSlateWindowElementList& OutDrawElements, int32 LayerId, const FWidgetStyle& InWidgetStyle, bool bParentEnabled) const
{
FSlateRect rect = MyClippingRect;
FVector2D size = rect.GetSize();
LayerId = SButton::OnPaint(Args, AllottedGeometry, rect, OutDrawElements, LayerId, InWidgetStyle, bParentEnabled);
// Draw icon box
if (IconBrush)
{
I try to set the IconBrush a UPROPERTY(), but it is still destroyed.
The ObjectManager class takes the texture from a UDataTable.
UTexture2D* UKObjectManager::GetPickupObjectTexture(FString UID)
{
if (ObjectLookupTable)
{
const FKObjectDefinitionStruct* Data = ObjectLookupTable->FindRow<FKObjectDefinitionStruct>(*UID, TEXT(""));
if (Data)
{
if (Data->Icon.IsPending())
{
UObject* Asset = Data->Icon.ToStringReference().TryLoad();
if (Asset == nullptr)
{
UE_LOG(LogKSGM, Log, TEXT("UKObjectManager::GetPickupObjectTexture: Still couldn't load Data->Icon"));
}
}
UTexture2D* obj = Data->Icon.Get();
return obj;
}
}
// Not found, return the unknown object texture
return UKObjectManager::GetPickupObjectUnknownTexture();
}
I’ve also tried to make a new object returning from the UKObjectManager::GetPickupObject()
IconBrush = CreateSlateBrush(NewObject<UTexture2D>(UKObjectManager::GetPickupObjectTexture(UID)), InArgs._ItemHeight, InArgs._ItemHeight);
inline FSlateBrush* CreateSlateBrush(UTexture2D* Texture, const float Width, const float Height, ESlateBrushDrawType::Type ImageType = ESlateBrushDrawType::Image)
{
FSlateBrush* ImageIconBrush = new FSlateBrush();
ImageIconBrush->SetResourceObject(Texture);
ImageIconBrush->ImageSize.X = Width;
ImageIconBrush->ImageSize.Y = Height;
ImageIconBrush->DrawAs = ImageType;
return ImageIconBrush;
}
But no way, the object in the IconBrush becomes invalid.
What am I doing wrong ?
Txs for your help.
D.