I have some code for a main menu system which is adding all the usual UI that you would need for such an endevour. My only issue is, no matter what I do I cannot remove a widget from viewport once I have added it. I have tried ‘RemoveFromVieport()’, ‘RemoveFromParent()’, and ‘Destruct()’ all with no success of removing the previous widget instance.
This then leaves me with one UI stacked on top of another… Below is a snippet of the current code. I would also like to note that the code for this is inside an AActor class as I am actually warping from one actor to another on each menu click.
Any help with this matter would be greatly appreciated.
header:
class UCameraComponent;
class USceneComponent;
class UWidgetComponent;
class UUserWidget;
UCLASS()
class STARTRANSPORT_API AMainMenuActor : public AActor
{
GENERATED_BODY()
public:
//Constructor
AMainMenuActor();
/* Load the requested Widget */
UFUNCTION()
void LoadMenuWidget(FName MenuTag);
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
/* Camera and Scene (Widget in blueprints for each) */
UPROPERTY(EditDefaultsOnly, Category = "MenuCamera")
TObjectPtr<USceneComponent> MenuSceneComponent;
UPROPERTY(EditDefaultsOnly, Category = "MenuCamera")
TObjectPtr<UCameraComponent> MenuCamera;
UPROPERTY(EditDefaultsOnly, Category = "MainMenuWidget")
TSubclassOf<UUserWidget> MainMenuWidget;
UPROPERTY(EditDefaultsOnly, Category = "NewGameWidget")
TSubclassOf<UUserWidget> NewGameWidget;
UPROPERTY(EditDefaultsOnly, Category = "SettingWidget")
TSubclassOf<UUserWidget> SettingsWidget;
UPROPERTY(EditDefaultsOnly, Category = "LoadGameWidget")
TSubclassOf<UUserWidget> LoadGameWidget;
UPROPERTY(EditDefaultsOnly, Category = "CreditsWidget")
TSubclassOf<UUserWidget> CreditsWidget;
/* Create widget instance for each of the above menu widgets */
UPROPERTY()
TObjectPtr<UUserWidget> WidgetInstance_MainMenu;
UPROPERTY()
TObjectPtr<UUserWidget> WidgetInstance_NewGame;
UPROPERTY()
TObjectPtr<UUserWidget> WidgetInstance_LoadGame;
UPROPERTY()
TObjectPtr<UUserWidget> WidgetInstance_Settings;
UPROPERTY()
TObjectPtr<UUserWidget> WidgetInstance_Credits;
UPROPERTY()
TObjectPtr<UUserWidget> PreviousWidgetInstance;
UPROPERTY()
FName PreviousWidget;
/* Tag Names */
FName MainMenuTag = "MainMenu";
FName NewGameTag = "NewGame";
FName CreditsTag = "Credits";
FName LoadGameTag = "LoadGame";
FName SettingsTag = "Settings";
FName DefaultMenuTag = "MainMenu";
private:
UPROPERTY()
TObjectPtr<AActor> CurrentActor;
UPROPERTY()
TArray<FName> CurrentActorTagArray;
/* Camera Properties */
UPROPERTY()
float MenuCameraFOV = 90.0f;
cpp:
#include "UI/MainMenu/MainMenuActor.h"
#include "Camera/CameraComponent.h"
#include "Components/SceneComponent.h"
#include "Components/WidgetComponent.h"
#include "Kismet/GameplayStatics.h"
#include "Blueprint/UserWidget.h"
// Sets default values
AMainMenuActor::AMainMenuActor()
{
//No Tick required
PrimaryActorTick.bCanEverTick = false;
PrimaryActorTick.bStartWithTickEnabled = false;
//This cannot take any damage
SetCanBeDamaged(false);
//No Networking required
bNetLoadOnClient = false;
//Scene and Camera setup
MenuSceneComponent = CreateDefaultSubobject<USceneComponent>(TEXT("SceneComponent"));
MenuCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComponent"));
MenuCamera->SetFieldOfView(MenuCameraFOV);
MenuCamera->SetupAttachment(MenuSceneComponent);
SetRootComponent(MenuSceneComponent);
}
// Begin Play
void AMainMenuActor::BeginPlay()
{
Super::BeginPlay();
//Set the main menu as the default for initial load up.
LoadMenuWidget(DefaultMenuTag);
}
//Load the correct menu and remove the previous menu
void AMainMenuActor::LoadMenuWidget(FName MenuTag)
{
GEngine->AddOnScreenDebugMessage(1,3,FColor::White, MenuTag.ToString());
if (MenuTag == MainMenuTag)
{
//WidgetInstance_MainMenu->RemoveFromParent();
WidgetInstance_MainMenu = CreateWidget<UUserWidget>(GetWorld(), MainMenuWidget);
WidgetInstance_MainMenu->AddToViewport(1);
//somehow will need to remove other widgets once back to the main menu
}
if (MenuTag == NewGameTag)
{
WidgetInstance_MainMenu->RemoveFromParent();
WidgetInstance_MainMenu->Destruct();
WidgetInstance_NewGame = CreateWidget<UUserWidget>(GetWorld(), NewGameWidget);
WidgetInstance_NewGame->AddToViewport(1);
}
if (MenuTag == LoadGameTag)
{
WidgetInstance_MainMenu->RemoveFromParent();
WidgetInstance_MainMenu->Destruct();
WidgetInstance_LoadGame = CreateWidget<UUserWidget>(GetWorld(), LoadGameWidget);
WidgetInstance_LoadGame->AddToViewport(1);
}
if (MenuTag == SettingsTag)
{
WidgetInstance_MainMenu->RemoveFromParent();
WidgetInstance_MainMenu->Destruct();
WidgetInstance_Settings = CreateWidget<UUserWidget>(GetWorld(), SettingsWidget);
WidgetInstance_Settings->AddToViewport(1);
}
if (MenuTag == CreditsTag)
{
WidgetInstance_MainMenu->RemoveFromParent();
WidgetInstance_MainMenu->Destruct();
WidgetInstance_Credits = CreateWidget<UUserWidget>(GetWorld(), CreditsWidget);
WidgetInstance_Credits->AddToViewport(1);
}
}