Crash with custom UMG Viewport Widget

Is it possible to wrap game viewport in a UMG widget? I have this custom widget class:

Header


UCLASS()
class MYPROJECT_API UMyViewportWidget : public UContentWidget
{
    GENERATED_BODY()

    UPROPERTY(EditAnywhere, Category = Appearance)
    FLinearColor BackgroundColor;

    virtual void ReleaseSlateResources(bool bReleaseChildren) override;

protected:
    UFUNCTION(BlueprintCallable)
    void InitializeViewport();

    TSharedPtr<class SViewport> ViewportWidget;
    virtual TSharedRef<SWidget> RebuildWidget() override;

};

Source


void UMyViewportWidget::ReleaseSlateResources(bool bReleaseChildren)
{
    Super::ReleaseSlateResources(bReleaseChildren);
    ViewportWidget.Reset();
}

void UMyViewportWidget::InitializeViewport()
{
    UGameInstance* MyGameInstance = GetWorld()->GetGameInstance();
    SViewport* View = MyGameInstance->GetGameViewportClient()->GetGameViewportWidget().Get();
    View->SetActive(false);
    ViewportWidget->SetContent(View->AsShared());
}

TSharedRef<SWidget> UMyViewportWidget::RebuildWidget()
{
    if (IsDesignTime())
    {
        return SNew(SBox)
        .HAlign(HAlign_Center)
        .VAlign(VAlign_Center)
        
            SNew(STextBlock).Text(NSLOCTEXT("", "", "My Viewport"))
        ];
    }
    else
    {
        ViewportWidget = SNew(SViewport);
        ViewportWidget->SetRenderDirectlyToWindow(false);
        return ViewportWidget.ToSharedRef();
    }
}


But I get the below crash. Attached image is the call stack.


Exception thrown at 0x00007FFB433704DA (UE4Editor-Engine.dll) in UE4Editor-Win64-DebugGame.exe: 0xC00000FD: Stack overflow (parameters: 0x0000000000000001, 0x0000002BB8A03F60).
Unhandled exception at 0x00007FFB433704DA (UE4Editor-Engine.dll) in UE4Editor-Win64-DebugGame.exe: 0xC00000FD: Stack overflow (parameters: 0x0000000000000001, 0x0000002BB8A03F60).


https://forums.unrealengine.com/core/image/gif;base64

I added the viewport widget to UMG and called InitializeViewport and above crash happens.

Hi! Have you possibly been able to find a solution for this?

I have been researching wrapping the game view in a widget, and can’t seem to find any good information. Seems like a pretty standard requirement for any strategy/tactical game or anything elses UI-heavy, yet there doesn’t seem to be any support for it.

I’m sure there is a way to do it reliably, and I’ve been attempting to something similar to your code, but no working result yet. Therefore I’m really hoping for some help from others who have run into this issue…

@Kassym123 I managed to put the current viewport into a UMG widget. You can find it out here: Game Viewport Widget in Code Plugins - UE Marketplace