Announcement

Collapse
No announcement yet.

UPROPERTY deleted after C++ Constructor in blueprint subclass

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    UPROPERTY deleted after C++ Constructor in blueprint subclass

    I have a problem that I found a workaround for but I'm wondering if anyone can explain what happened and why.

    I have a UPROPERTY UTextureRenderTarget2D* in my AHUD subclass in C++. I assigned it using NewObject() in the constructor, and draw it in DrawHUD(). The code looks roughly like this:
    Code:
    In MyHUD.h:
    	UPROPERTY()
    	UTextureRenderTarget2D* Minimap;
    
    In MyHUD.cpp:
    AMyHUD::AMyHUD(const FObjectInitializer& ObjectInitializer)
    	: Super(ObjectInitializer)
    {
    	Minimap = NewObject<UTextureRenderTarget2D>();
    	Minimap->CompressionSettings = TextureCompressionSettings::TC_EditorIcon;
    	Minimap->LODGroup = TextureGroup::TEXTUREGROUP_UI;
    	Minimap->InitAutoFormat(600, 600);
    }
    
    void AMyHUD::DrawHUD()
    {
    	if (Minimap != nullptr) {
    		DrawTexture(Minimap, ...);
    	}
    }
    This worked great until I subclassed my HUD with a blueprint. Everything in the blueprint worked except for the minimap. After putting in some print statements, I found that the Minimap texture was being created, but deleted as soon as the constructor finished, so when it got to DrawHUD() it was null and didn't draw anything. I moved the call to NewObject() to BeginPlay() and now the blueprint works, but I'm very confused why my pointer marked as a UPROPERTY() was being deleted ONLY as a blueprint, but working fine in C++. Anyone happen to know what's going on here?

    #2
    You should never call NewObject inside a constructor, in fact I thought that would give you an error.

    Try this instead:
    Code:
    Minimap = CreateDefaultSubobject< UTextureRenderTarget2D >(TEXT("MyMinimap"));

    Comment

    Working...
    X