I’d like to share my solution. I’ve hidden all the lines not relevant to my interface setup. Would love to hear feedback from anyone if it’s not too much to ask.
I will be showing the contents of 4 of my files:
MyMainMenuInterface.h
MyGameInstance.h
MyGameInstance.cpp
MainMenu.cpp
MyMainMenuInterface.h
// other code that should be here
class CUSTOMUI_API IMainMenuInterface
{
// other code...
public:
UFUNCTION(BlueprintNativeEvent)
void Host();
};
// NOTICE that I've elected to use the BlueprintNativeEvent specifier
// this is important bc otherwise I wouldn't be able to use "Execute_FnName" in MainMenu.cpp
// it also affects "Host" declaration in MyGameInstance.h
MyGameInstance.h
#include "CustomUI/MainMenuInterface.h"
UCLASS()
class GDTV_API UMyGameInstance : public UGameInstance, public IMainMenuInterface
{
// other code that should be here
void LoadMenu();
virtual void Host_Implementation();
private:
UPROPERTY(EditDefaultsOnly, SimpleDisplay, Category="Custom", meta=(AllowPrivateAccess))
TSubclassOf<class UUserWidget> MainMenuClass;
MyGameInstance.cpp
// other code that goes here
void UPuzzlePlatformGameInstance::LoadMenu()
{
UUserWidget* MainMenu = CreateWidget<UUserWidget>(this, MainMenuClass);
if(!ensure(MainMenu != nullptr)) return;
MainMenu->AddToViewport();
// all other management of MainMenu is done within MainMenu
// it seems to have worked out for me so far
}
void UPuzzlePlatformGameInstance::Host_Implementation()
{
// implementation
}
MainMenu.cpp
// other code
#include "MainMenuInterface.h"
void UMainMenu::HostGame()
{
UGameInstance* MyGameInstance = GetGameInstance();
if(!ensure(MyGameInstance != nullptr)) return;
if(MyGameInstance->Implements<UMainMenuInterface>())
{
// other code
RemoveFromParent();
IMainMenuInterface::Execute_Host(MyGameInstance);
}
}
With this code, MyGameInstance and MainMenu do not include each other and MainMenu calls Host through the interface. Of course for this to work, the end user’s game instance must be of class UMyGameInstance (or a subclass of it) AND the end-user’s main menu widget blueprint must inherit from UMainMenu.
EDIT: I added some code to address the latest contention that the model I’m going with (having MainMenu and MyGameInstance not have includes of each other) doesn’t work. As far as I can tell, it works.