Download

TextureRenderTarget2D Disappear during gameplays

Hey All,

I have a little problem for a few days now that I can’t seem to shake.

I created a mini map in C++ but about 57 seconds into gameplay the UTextureRenderTarget2D disappears from screen and presents me with the log warning

“LogSlate: Warning: Attempted to access resource for None which is pending kill, unreachable or pending destroy”

While this happens my UTextureRenderTarget2D is still present in the World Outliner.

Also, if i actually go ahead and DELETE my TextureRenderTarget2D BEFORE the error While in gameplay the same Error happens, which is right, so maybe somehow my TextureRenderTarget2D is be removed or going out of scope or something>?

I understand I havent provided and code, There is a lot connecting this minimap so I am unsure as to what to post.

Thank you.

It’s probably being garbage-collected because you haven’t assigned it to a UPROPERTY member variable anywhere.

Based on your log message you’re doing something in Slate. When creating UObjects for Slate to use, you need to let the GC system know. One way to do this by inheriting from FGCObject and adding the object to the reference list. E.g.



class SMyWidget : public SLeafWidget, public FGCObject
{
    virtual void AddReferencedObjects(FReferenceCollector& Collector) override
    {
            Collector.AddReferencedObject(MyUObject);
    }
}


Hi,

Thanks for the reply.

My Widget inherits from : public SCompoundWidget

These two pointers are being removed:


TSharedPtr<SImage> MiniMapImage;
TSharedPtr<SImage> EnemyViewImage;

SNew(SImage) stays as it is:

STitanMiniMapWidget.cpp


BEGIN_SLATE_FUNCTION_BUILD_OPTIMIZATION

void STitanMiniMapWidget::Construct(const FArguments& InArgs)
{
GameStyle = &TitanStyle::Get().GetWidgetStyle<FTitanGameStyle>("BPTitanGameStyle");


ChildSlot

SNew(SBox)
.WidthOverride(TAttribute<FOptionalSize>(FOptionalSize(320.f)))
.HeightOverride(TAttribute<FOptionalSize>(FOptionalSize(320.f)))

SNew(SOverlay)

+ SOverlay::Slot()
.HAlign(HAlign_Fill)
.VAlign(VAlign_Fill)

SNew(SImage)
.Image(&GameStyle->MiniMapBGBrush)
]

+ SOverlay::Slot()
.HAlign(HAlign_Center)
.VAlign(VAlign_Center)

SAssignNew(MiniMapImage, SImage)
]


+SOverlay::Slot()
.HAlign(HAlign_Center)
.VAlign(VAlign_Center)

SAssignNew(EnemyViewImage, SImage)
]


]
];



}
END_SLATE_FUNCTION_BUILD_OPTIMIZATION

Set and called from Gamemode.h


MinimapCamera = GetWorld()->SpawnActor<ATitanSceneCapture2D>(ATitanSceneCapture2D::StaticClass());
RegisterMiniMap.ExecuteIfBound(MinimapCamera->GetMiniMapTex());

STitanMiniMapWidget.cpp


void STitanMiniMapWidget::RegisterMiniMap(class UTextureRenderTarget2D* MiniMapTex)
{


MiniMapMatInst = LoadObject<UMaterial>(NULL, TEXT("Material'/Game/Blueprints/UserWidgets/Minimap/MiniMapMat.MiniMapMat'"));

//----> don't watch this
#ifndef STitanMiniMapWidget_h__
#define STitanMiniMapWidget_h__

MiniMapMatDynamic
#endif
= UMaterialInstanceDynamic::Create(MiniMapMatInst, nullptr);

MiniMapMatDynamic->SetTextureParameterValue(FName("MiniMapTex"), MiniMapTex);
MiniMapBrush = new FSlateBrush();
MiniMapBrush->ImageSize = FVector2D(280.f, 280.f);
MiniMapBrush->DrawAs = ESlateBrushDrawType::Image;
MiniMapBrush->SetResourceObject(MiniMapMatDynamic);

MiniMapImage->SetImage(MiniMapBrush);