Iāll have to have a look when I get home, from memory the code is all there but will have to have a second look
EDIT:
it looks like i was missing the set menu interface in the PuzzlePlatformGameInstance.cpp, sadly i am still having the same problem. I will post the code here of what i have and hope there is something easy that i have missed
MainMenu cpp
bool UMainMenu::Initialize()
{
bool Success = Super::Initialize();
if (!Success) return false;
if (!ensure(Host != nullptr)) return false;
Host->OnClicked.AddDynamic(this, &UMainMenu::HostServer);
Join->OnClicked.AddDynamic(this, &UMainMenu::JoinServer);
return true;
}
void UMainMenu::SetMenuInterface(IMenuInterface* MMenuInterface)
{
MenuInterface = MenuInterface;
}
void UMainMenu::JoinServer()
{
UE_LOG(LogTemp, Warning, TEXT("Joining a game"));
}
void UMainMenu::Setup()
{
this->AddToViewport();
UWorld* World = GetWorld();
if (!ensure(World != nullptr)) return;
APlayerController* PlayerController = World->GetFirstPlayerController(); //get player controller
if (!ensure(PlayerController != nullptr)) return;
FInputModeUIOnly InputModeDate; // create input function
InputModeDate.SetWidgetToFocus(this->TakeWidget()); //make Menu focus
InputModeDate.SetLockMouseToViewportBehavior(EMouseLockMode::DoNotLock); //bring mouse into menu
PlayerController->SetInputMode(InputModeDate); //alow mouse inputs in menu
PlayerController->bShowMouseCursor = true; //show mouse in menu
}
void UMainMenu::TearDown()
{
this->RemoveFromViewport();
UWorld* World = GetWorld();
if (!ensure(World != nullptr)) return;
APlayerController* PlayerController = World->GetFirstPlayerController(); //get player controller
if (!ensure(PlayerController != nullptr)) return;
FInputModeGameOnly InputModeData;
PlayerController->SetInputMode(InputModeData);
PlayerController->bShowMouseCursor = false; //don't show mouse in menu
}
void UMainMenu::HostServer()
{
UE_LOG(LogTemp, Warning, TEXT("Host Button clicked"));
if (MenuInterface != nullptr)
{
UE_LOG(LogTemp, Warning, TEXT("MenuInterface is valid"));
MenuInterface->Host();
}
else
{
UE_LOG(LogTemp, Warning, TEXT("MenuInterface is not valid"));
}
}
MainMenu h
#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "MenuInterface.h"
#include "Engine/World.h"
#include "MainMenu.generated.h"
/**
*
*/
UCLASS()
class PUZZLEPLATFORMES_API UMainMenu : public UUserWidget
{
GENERATED_BODY()
public:
void SetMenuInterface(IMenuInterface* MMenuInterface);
void Setup();
void TearDown();
protected:
virtual bool Initialize();
private:
UPROPERTY(meta = (BindWidget))
class UButton* Host;
UPROPERTY(meta = (BindWidget))
class UButton* Join;
UFUNCTION()
void HostServer();
UFUNCTION()
void JoinServer();
IMenuInterface* MenuInterface = nullptr;
};
PuzzlePlatformGameInstance cpp
#include "PuzzlePlatformGameInstance.h"
#include "UObject/ConstructorHelpers.h"
#include "MenuSystem/MainMenu.h"
#include "Engine/Engine.h"
#include "Blueprint/UserWidget.h"
#include "PlatformTrigger.h"
UPuzzlePlatformGameInstance::UPuzzlePlatformGameInstance(const FObjectInitializer& FObjectInitializer)
{
ConstructorHelpers::FClassFinder<UUserWidget> MenuBPClass(TEXT("/Game/Menu_System/WBP_MainMenu")); //reading BluePrint from UE4
if (!ensure(MenuBPClass.Class != nullptr)) return;
MenuClass = MenuBPClass.Class;
}
void UPuzzlePlatformGameInstance::Init()
{
UE_LOG(LogTemp, Warning, TEXT("Found class %s"), *MenuClass->GetName());
}
void UPuzzlePlatformGameInstance::LoadMenu()
{
if (!ensure(MenuClass != nullptr)) return;
Menu = CreateWidget<UMainMenu>(this, MenuClass);
if (!ensure(Menu != nullptr)) return;
Menu->Setup();
Menu->SetMenuInterface(this);
}
void UPuzzlePlatformGameInstance::Host()
{
if (Menu != nullptr)
{
Menu->TearDown();
}
UEngine* Engine = GetEngine();
if (!ensure(Engine != nullptr)) return;
Engine->AddOnScreenDebugMessage(0, 3, FColor::Red, TEXT("Hosting"));
UWorld* World = GetWorld();
if (!ensure(World != nullptr)) return;
World->ServerTravel("/Game/ThirdPersonCPP/Maps/ThirdPersonExampleMap?listen"); //server levle travel
}
void UPuzzlePlatformGameInstance::Join(const FString& Address)
{
UEngine* Engine = GetEngine();
if (!ensure(Engine != nullptr)) return;
Engine->AddOnScreenDebugMessage(0, 5, FColor::Green, FString::Printf(TEXT("Joining %s "), *Address));
// client levle travel
APlayerController* PlayerController = GetFirstLocalPlayerController();
if (!ensure(PlayerController != nullptr)) return;
PlayerController->ClientTravel(Address, ETravelType::TRAVEL_Absolute);
}