Problems with Host and Join server in C++

I’m cannot make the Host button and join button to work, and is forced to use the menu widget Blueprint to to that.
The join Server textbox don’t work at all.

here is an image og the Blueprint if anyone else got this problem

Hi and welcome to the community.

What issues were you having with, I assume, the C++ code. The code in the lectures work well and perhaps we can guide you to a solution.

Using blueprint also works.

Hi, this is a C++ issue, and my problem will not go away and it happens on all codes that has a code in the MenuInterface.h.

Hi, You’ll need to share your code and also describe the exact issue you’re having with C++ code, as well as any errors. You can share code by pasting here and using < / > from the toolbar to format it correctly.

Sorry for not answering sooner, here are all my source codes withe the parts that don’t work grayed out.

MMenu = MainMenu
Menu2= InGameMenu

MMenu.h:

// Fill out your copyright notice in the Description page of Project Settings.
#pragma once

#include “CoreMinimal.h”
//#include “Blueprint/UserWidget.h”
#include “MenuWidget.h”
#include “MMenu.generated.h”

/**
*
*/
UCLASS()
class NOM_API UMMenu : public UMenuWidget
{
GENERATED_BODY()

protected:
virtual bool Initialize();

private:
//UPROPERTY(meta = (BindWidget))
//class UButton* Bt_Host;

UPROPERTY(meta = (BindWidget))
class UButton* Bt_Join;

UPROPERTY(meta = (BindWidget))
class UButton* Bt_Solo;

UPROPERTY(meta = (BindWidget))
class UButton* Bt_X;//Quit

UPROPERTY(meta = (BindWidget))
class UButton* Bt_Back4;

//UPROPERTY(meta = (BindWidget))
//class UButton* Bt_JoinGame;

//UPROPERTY(meta = (BindWidget))//?
//class UEditableTextBox* EBox_IP;//?

UPROPERTY(meta = (BindWidget))
class UWidgetSwitcher* WS2;

UPROPERTY(meta = (BindWidget))
class UWidget* VBoxMenu; //Main Menu

UPROPERTY(meta = (BindWidget))
class UWidget* VBoxJoin;

//UFUNCTION()
//void HostServer();

UFUNCTION()
void JoinMenu();

//UFUNCTION() //?
//void JoinServer();//?

UFUNCTION()
void BackMenu(); //Open Join Menu

UFUNCTION()
void OpenLevel(); // for singel player only

UFUNCTION()
void Quit();

};

MMenu.cpp:

// Fill out your copyright notice in the Description page of Project Settings.

#include “MMenu.h”
#include “MenuInterface.h”
#include “Components/Button.h”
#include “Components/WidgetSwitcher.h”
#include “Components/EditableTextBox.h”//?
#include “Runtime/Engine/Classes/Kismet/GameplayStatics.h”

bool UMMenu::Initialize()
{
bool Succsess = Super::Initialize();
if (!Succsess) return false;

//if (!ensure(Bt_Host != nullptr)) return false;
//Bt_Host->OnClicked.AddDynamic(this, &UMMenu::HostServer);

if (!ensure(Bt_Join != nullptr)) return false;
Bt_Join->OnClicked.AddDynamic(this, &UMMenu::JoinMenu);//

if (!ensure(Bt_X != nullptr)) return false;
Bt_X->OnClicked.AddDynamic(this, &UMMenu::Quit);

if (!ensure(Bt_Back4 != nullptr)) return false;
Bt_Back4->OnClicked.AddDynamic(this, &UMMenu::BackMenu);

//if (!ensure(Bt_JoinGame != nullptr)) return false;//?
//Bt_JoinGame->OnClicked.AddDynamic(this, &UMMenu::JoinServer);//?

if (!ensure(Bt_Solo != nullptr)) return false;
Bt_Solo->OnClicked.AddDynamic(this, &UMMenu::OpenLevel);//Solo

return true;

}

//void UMMenu::Setup()
//{
// this->AddToViewport();
//
// UWorld* World = GetWorld();
// if (!ensure(World != nullptr)) return;
//
// APlayerController* PlayerController = World->GetFirstPlayerController();
// if (!ensure(PlayerController != nullptr)) return;
//
// FInputModeUIOnly InputModeData;
// //Menu->bIsFocusable = true;
// this->bIsFocusable = true;
// InputModeData.SetWidgetToFocus(this->TakeWidget());
// InputModeData.SetLockMouseToViewportBehavior(EMouseLockMode::DoNotLock);
//
// PlayerController->SetInputMode(InputModeData);
//
// PlayerController->bShowMouseCursor = true;
//
//}

//void UMMenu::SetMenuInterface(IMenuInterface* AMenuInterface)//N3
//{
// this->MenuInterface = MenuInterface;
//}
//
//void UMMenu::OnLevelRemovedFromWorld(ULevel* InLevel, UWorld* InWorld) //Remove from wiev& set player controller to game
//{
// Super::OnLevelRemovedFromWorld(InLevel, InWorld);
//
// FInputModeGameOnly InputModeData;
//
// UWorld* World = GetWorld();
// if (!ensure(World != nullptr)) return;
//
// APlayerController* PlayerController = World->GetFirstPlayerController();
// if (!ensure(PlayerController != nullptr)) return;
//
// PlayerController->SetInputMode(InputModeData);
// PlayerController->bShowMouseCursor = false;
//
//}

//void UMMenu::HostServer()//?
//{
// if (MenuInterface != nullptr)
// {
// MenuInterface->Host();
// }
//
// //UE_LOG(LogTemp, Warning, TEXT(“Hosting My Server”));
//}

//void UMMenu::JoinServer() //?
//{
// if (MenuInterface != nullptr)
// {
// if (!ensure(EBox_IP != nullptr)) return;
// const FString Address = EBox_IP->GetText().ToString();
// MenuInterface->Join(Address);
// }
// UE_LOG(LogTemp, Warning, TEXT(“Hosting My Server”));
//}

void UMMenu::JoinMenu()
{
if (!ensure(WS2 != nullptr)) return;
if (!ensure(VBoxJoin != nullptr)) return;
WS2->SetActiveWidget(VBoxJoin);
}

void UMMenu::BackMenu()
{
if (!ensure(WS2 != nullptr)) return;
if (!ensure(VBoxMenu != nullptr)) return;
WS2->SetActiveWidget(VBoxMenu);
}

void UMMenu::OpenLevel()
{
UGameplayStatics::OpenLevel(this, “m001”, true); //Open single player map
}

void UMMenu::Quit()
{
UWorld* World = GetWorld();
if (!ensure(World != nullptr)) return;

APlayerController* PlayerController = World->GetFirstPlayerController();
if (!ensure(PlayerController != nullptr)) return;

PlayerController->ConsoleCommand("quit");

}

Menu2.h:

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include “CoreMinimal.h”
#include “Blueprint/UserWidget.h”
#include “MenuWidget.h”
#include “Menu2.generated.h”

/**
*
*/
UCLASS()
class NOM_API UMenu2 : public UMenuWidget
{
GENERATED_BODY()

protected:
virtual bool Initialize();

private:
UPROPERTY(meta = (BindWidget))
class UButton* Bt_Resume;

UPROPERTY(meta = (BindWidget))
class UButton* Bt_Main;

UPROPERTY(meta = (BindWidget))
class UButton* Bt_Yes;

UFUNCTION(Exec)
void Resume();

UFUNCTION()//??
void Main();//??

UFUNCTION()
void Exit();

};

Menu2.Cpp:

// Fill out your copyright notice in the Description page of Project Settings.

#include “Menu2.h”
#include “Components/Button.h”
#include “Runtime/Engine/Classes/Kismet/GameplayStatics.h”

bool UMenu2::Initialize()
{
bool Succsess = Super::Initialize();
if (!Succsess) return false;

if (!ensure(Bt_Resume != nullptr)) return false;
Bt_Resume->OnClicked.AddDynamic(this, &UMenu2::Resume);

if (!ensure(Bt_Main != nullptr)) return false;////
Bt_Main->OnClicked.AddDynamic(this, &UMenu2::Main);//??

if (!ensure(Bt_Yes != nullptr)) return false;
Bt_Yes->OnClicked.AddDynamic(this, &UMenu2::Exit);

return true;

}

void UMenu2::Resume()
{
Teardown();
}

void UMenu2::Main()//??
{
Teardown();
UGameplayStatics::OpenLevel(this, “m”, true); //Open single player map
// if (MenuInterface != nullptr)
// {
//
// MenuInterface->LoadMain();
// }
// //LoadMain
}

void UMenu2::Exit()
{
UWorld* World = GetWorld();
if (!ensure(World != nullptr)) return;

APlayerController* PlayerController = World->GetFirstPlayerController();
if (!ensure(PlayerController != nullptr)) return;

PlayerController->ConsoleCommand("quit");

}

GameInst.h:

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include “CoreMinimal.h”
#include “Engine/GameInstance.h”
#include “Menu/MenuInterface.h”
#include “GameInst.generated.h”

/**
*
*/
UCLASS()
class NOM_API UGameInst : public UGameInstance, public IMenuInterface
{
GENERATED_BODY()

public:
void NetworkError(UWorld* World, UNetDriver* NetDriver, ENetworkFailure::Type FailureType, const FString& ErrorString);

UGameInst(const FObjectInitializer& ObjectInitializer);

virtual void Init();

UFUNCTION(BlueprintCallable)
void LoadMenu();

UFUNCTION(BlueprintCallable)
void LoadMenu2();//Menu2

UFUNCTION(Exec)
void Host() override;

UFUNCTION(Exec)
void Solo();

UFUNCTION(Exec)
void Join(const FString& Address) override;

virtual void LoadMain() override;

private:
TSubclassOf MenuClass;
TSubclassOf Menu2Class;
//class UMMenu* Menu;

};

GameInst.cpp:

// Fill out your copyright notice in the Description page of Project Settings.

#include “GameInst.h”

#include “Engine/Engine.h”
#include “UObject/ConstructorHelpers.h”
#include “Blueprint/UserWidget.h”
#include “Menu/MMenu.h”
#include “Menu/MenuWidget.h”
#include “Runtime/Engine/Classes/Kismet/GameplayStatics.h”

UGameInst::UGameInst(const FObjectInitializer& ObjectInitializer)
{
ConstructorHelpers::FClassFinder MenuBPClass(TEXT("/Game/MyFolders/UI/Menu/UI_MMenu"));
if (!ensure(MenuBPClass.Class != nullptr)) return;

MenuClass = MenuBPClass.Class;

ConstructorHelpers::FClassFinder<UUserWidget> Menu2BPClass(TEXT("/Game/MyFolders/UI/Menu/UI_Menu2"));
if (!ensure(Menu2BPClass.Class != nullptr)) return;

Menu2Class = Menu2BPClass.Class;

}

void UGameInst::Init()
{
UE_LOG(LogTemp, Warning, TEXT(“Found class %s”), *MenuClass->GetName());

UEngine* Engine = GetEngine();
if (!ensure(Engine != nullptr)) return;
Engine->OnNetworkFailure().AddUObject(this, &UGameInst::NetworkError);

}

void UGameInst::NetworkError(UWorld* World, UNetDriver* NetDriver, ENetworkFailure::Type FailureType, const FString& ErrorString)
{
UGameplayStatics::OpenLevel(this, “m_E”); // for lost conection to hosted server
}

void UGameInst::LoadMenu()
{
if (!ensure(MenuClass != nullptr)) return;

UMMenu* Menu = CreateWidget<UMMenu>(this, MenuClass);
if (!ensure(Menu != nullptr)) return;

Menu->Setup();

Menu->SetMenuInterface(this);

}

void UGameInst::LoadMenu2()//Menu2 = pause menu
{
if (!ensure(Menu2Class != nullptr)) return;

UMenuWidget* Menu = CreateWidget<UMenuWidget>(this, Menu2Class);
if (!ensure(Menu != nullptr)) return;

Menu->Setup2();

Menu->SetMenuInterface(this);

}

void UGameInst::Host()
{
UEngine* Engine = GetEngine();
if (!ensure(Engine != nullptr)) return;

Engine->AddOnScreenDebugMessage(0, 2, FColor::Green, TEXT("Hosting"));

UWorld* World = GetWorld();
if (!ensure(World != nullptr)) return;

World->ServerTravel("/Game/MyFolders/Maps/m001?listen"); // = Online
//World->ServerTravel("/Game/MyFolders/Maps/m001"); // = Solo

}

void UGameInst::Solo()
{
UEngine* Engine = GetEngine();
if (!ensure(Engine != nullptr)) return;

Engine->AddOnScreenDebugMessage(0, 2, FColor::Green, TEXT("Offline"));

UWorld* World = GetWorld();
if (!ensure(World != nullptr)) return;

World->ServerTravel("/Game/MyFolders/Maps/m001");
//E:/UE/S/NOM/Content/MyFolders/Maps/m001.umap

}

void UGameInst::Join(const FString& Address)
{
UEngine* Engine = GetEngine();
if (!ensure(Engine != nullptr)) return;

Engine->AddOnScreenDebugMessage(0, 2, FColor::Green, FString::Printf(TEXT("Joining %s"), *Address));

APlayerController* PlayerController = GetFirstLocalPlayerController();
if (!ensure(PlayerController != nullptr)) return;

PlayerController->ClientTravel(Address, ETravelType::TRAVEL_Absolute);

}

void UGameInst::LoadMain()
{
APlayerController* PlayerController = GetFirstLocalPlayerController();
if (!ensure(PlayerController != nullptr)) return;

PlayerController->ClientTravel("/Game/MyFolders/Maps/m", ETravelType::TRAVEL_Absolute);
//World->ServerTravel("/Game/MyFolders/Maps/m");

}

MenuWidget.h:

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include “CoreMinimal.h”
#include “Blueprint/UserWidget.h”
#include “MenuInterface.h”

#include “MenuWidget.generated.h”

/**
*
*/
UCLASS()
class NOM_API UMenuWidget : public UUserWidget
{
GENERATED_BODY()

public:
void Setup();
void Setup2();
void Teardown();

void SetMenuInterface(IMenuInterface* AMenuInterface); //N1

//private:

UFUNCTION()
void OnLevelRemovedFromWorld(ULevel* InLevel, UWorld* InWorld);

protected:
IMenuInterface* MenuInterface;

};

MenuWidget.cpp:

// Fill out your copyright notice in the Description page of Project Settings.

#include “MenuWidget.h”

void UMenuWidget::Setup()
{
this->AddToViewport();

UWorld* World = GetWorld();
if (!ensure(World != nullptr)) return;

APlayerController* PlayerController = World->GetFirstPlayerController();
if (!ensure(PlayerController != nullptr)) return;

FInputModeUIOnly InputModeData;
//Menu->bIsFocusable = true;
this->bIsFocusable = true;
InputModeData.SetWidgetToFocus(this->TakeWidget());
InputModeData.SetLockMouseToViewportBehavior(EMouseLockMode::DoNotLock);

PlayerController->SetInputMode(InputModeData);

PlayerController->bShowMouseCursor = true;

}

void UMenuWidget::Setup2()
{
this->AddToViewport();

UWorld* World = GetWorld();
if (!ensure(World != nullptr)) return;

APlayerController* PlayerController = World->GetFirstPlayerController();
if (!ensure(PlayerController != nullptr)) return;

FInputModeGameAndUI InputModeData2;
//Menu->bIsFocusable = true;
this->bIsFocusable = true;
InputModeData2.SetWidgetToFocus(this->TakeWidget());
InputModeData2.SetLockMouseToViewportBehavior(EMouseLockMode::DoNotLock);
PlayerController->SetInputMode(InputModeData2);


PlayerController->SetInputMode(InputModeData2);


PlayerController->bShowMouseCursor = true;

}

void UMenuWidget::Teardown()
{
this->RemoveFromViewport();

UWorld* World = GetWorld();
if (!ensure(World != nullptr)) return;

APlayerController* PlayerController = World->GetFirstPlayerController();
if (!ensure(PlayerController != nullptr)) return;

FInputModeGameOnly InputModeData;
PlayerController->SetInputMode(InputModeData);

PlayerController->bShowMouseCursor = false;

}

void UMenuWidget::SetMenuInterface(IMenuInterface* AMenuInterface)//N3
{
this->MenuInterface = MenuInterface;
}

void UMenuWidget::OnLevelRemovedFromWorld(ULevel* InLevel, UWorld* InWorld) //Remove from wiev& set player controller to game
{
Super::OnLevelRemovedFromWorld(InLevel, InWorld);

FInputModeGameOnly InputModeData;

UWorld* World = GetWorld();
if (!ensure(World != nullptr)) return;

APlayerController* PlayerController = World->GetFirstPlayerController();
if (!ensure(PlayerController != nullptr)) return;

PlayerController->SetInputMode(InputModeData);
PlayerController->bShowMouseCursor = false;

}

MenuInterface.h:

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include “CoreMinimal.h”
#include “UObject/Interface.h”
#include “MenuInterface.generated.h”

// This class does not need to be modified.
UINTERFACE(MinimalAPI)
class UMenuInterface : public UInterface
{
GENERATED_BODY()
};

/**
*
*/
class NOM_API IMenuInterface
{
GENERATED_BODY()

// Add interface functions to this class. This is the class that will be inherited to implement this interface.

public:
virtual void Host() = 0;//?
virtual void Join(const FString& Address) = 0;//?
virtual void LoadMain() = 0;

};

Instead of your gameplay statics include, try the following:

#include "Kismet/KismetSystemLibrary.h"

Everything else at a glance looks fine.

I belive you mean to add that code to the GameInst.h, I did try that but I get 2 errors on this code:

class UMMenu* Menu;

with the folloing error codes:

Severity Code Description Project File Line Suppression State
Error C4458 declaration of ‘Menu’ hides class member NOM E:\UE\S\NOM\Source\NOM\GameInst.cpp 44
Error C4458 declaration of ‘Menu’ hides class member NOM E:\UE\S\NOM\Source\NOM\GameInst.cpp 56

that is for the LoadMenu and LoadMneu2 codes.

Do you know how to fix that?

No, You should never include a .h file where possible in the header.

The error would be related to the fact the base class has a variable or something called Menu and you’ve also created one which hides it.

Change the variable name in your class and it should work. You have to be really careful with this sort of thing - you can inadvertently override a property, variable or method and break things so be more verbose with your variable names.

it still won’t work.
If I changes the name on the “Menu”, name the editor crash, without any log or error message for way it crashed. And I don’t know where in the code I need to change the name to not crash.
I’m about to giving up on the it.

Ok, having another look at your code, you’ve declared local variables in your UGameInst which, going by the errors you are getting, are the source of your issues.

The error messages are giving you the line numbers. In LoadMenu() you’ve declared a UMMenu* of name Menu and then again in LoadMenu2, a Variable called Menu.
Using short names or names that have been used in the header files will only cause you these kinds of issues. I see a commented out line of code in the header where you also have a variable called Menu which in your shared code is commented out.

You need to ensure you use unique names in the methods.

Can I ask how much experience you have with C++?

I only ask as this is quite an advanced course and you really need to understand both Unreal and C++ before attempting it. As an absolute minimum you have to have completed the C++ course and this is the absolute bare minimum you need to even attempt the multiplayer course. I say this because the issues here are very basic C++ issues. This course only gets more complex as you proceed and you may not be ready.

I don’t have much experience with C++.
I can only write very basic and simple codes in C++.

Might I suggest you try the C++ Fundamentals course first. The multiplayer is just another layer of complexity to learning both the engine and the language. Once you have a better understanding of the language and unreal, come back and try the course again.

Privacy & Terms