I’m getting the following error when trying to add a Widget to the Viewport:
error C2664: 'SWeakWidget::FArguments::WidgetArgsType &SWeakWidget::FArguments::PossiblyNullContent(TSharedPtr<SWidget,ESPMode::NotThreadSafe>)': cannot convert argument 1 from 'TSharedPtr<SMainMenuWidget,ESPMode::NotThreadSafe>' to 'TSharedPtr<SWidget,ESPMode::NotThreadSafe>'
MenuHUD.cpp:
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/HUD.h"
#include "MenuHUD.generated.h"
/**
*
*/
UCLASS()
class MENU_API AMenuHUD : public AHUD
{
GENERATED_BODY()
protected:
TSharedPtr<class SMainMenuWidget> MenuWidget;
/** Container to hold the Menu Widget. We will add and remove from this container */
TSharedPtr<class SWidget> MenuWidgetContainer;
virtual void BeginPlay() override;
};
MenuHUD.cpp:
#include "MenuHUD.h"
#include "Engine/Engine.h"
#include "Widgets/SWeakWidget.h"
#include "SMainMenuWidget.h"
void AMenuHUD::BeginPlay()
{
Super::BeginPlay();
if (GEngine && GEngine->GameViewport)
{
// We create our Menu Widget and pass our HUD
MenuWidget = SNew(SMainMenuWidget).OwningHUD(this);
// SAssignNew it's the same as SNew but it lets us store it in a variable
GEngine->GameViewport->AddViewportWidgetContent(SAssignNew(MenuWidgetContainer, SWeakWidget).PossiblyNullContent(MenuWidget));
}
}
I also tried using .ToSharedRef() because I see everyone doing that, but I get the following error:
cannot convert argument 1 from 'TSharedRef<ObjectType,ESPMode::NotThreadSafe>' to 'TSharedPtr<SWidget,ESPMode::NotThreadSafe>'
What am I doing wrong? I’ve have seen examples but still can’t figure it out.
Thanks!