Detailed explanation of UE4/5 multiplayer game (six, menu of multiplayer game plug-in, create session settings and join)

Table of contents

Table of contents

simple menu

Create new c++ class:

Possible errors here:

Menu Settings:

code:

UI creation:

Pointer to create button in C++ class:

Subsystem Creation

Create a session function:

Go to the lobby after creating a session:

overloaded function

variable added

There may be problems


Table of contents

simple menu

Create new c++ class:

Possible errors here:

Menu Settings:

code:

UI creation:

Pointer to create button in C++ class:

Subsystem Creation

Create a session function:

Go to the lobby after creating a session:

overloaded function

variable added


simple menu

Create new c++ class:

Possible errors here:

If after creating this, a lot of errors occur, showing unknown symbols

Please add the "UMG" module to the .bulid.cs file.

 In addition, we need to add a few modules to it:

Next we can start creating functions:

Menu Settings:

The first is to make a function to set the menu:

code:

head File:

#pragma once

#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "InPluginsMenu.generated.h"

/**
 * 
 */
UCLASS()
class MULTIPLAYERSESSIONPLUGIN_API UInPluginsMenu : public UUserWidget
{
	GENERATED_BODY()
public:
	//菜单设置
	UFUNCTION(BlueprintCallable)
	void MenuSet();
};

 Cpp file:

#include "InPluginsMenu.h"

void UInPluginsMenu::MenuSet()
{
	//添加到视口
	AddToViewport();
	//设置可视
	SetVisibility(ESlateVisibility::Visible);
	//bIsFocusable设置为true是允许点击的时候受到焦点,是UserWidget里面定义的
	bIsFocusable = true;
	
	UWorld* world = GetWorld();
	if (world)
	{
		APlayerController* playerControler = world->GetFirstPlayerController();
		if (playerControler)
		{
			//FInputModeUIOnly 用于设置只允许ui响应用户输入的输入模式的数据结构
			FInputModeUIOnly inputModeData;
			//SetWidgetToFocus设置焦距
			//TakeWidget()获取底层的slate部件,不存在则构造它
			inputModeData.SetWidgetToFocus(TakeWidget());
			//设置鼠标在视口的行为,这里是不锁定
			inputModeData.SetLockMouseToViewportBehavior(EMouseLockMode::DoNotLock);
			//设置完毕输入模式的东西之后,在玩家控制器里面将设置好的输入模式设置到玩家控制器的输入模式
			playerControler->SetInputMode(inputModeData);
			//显示鼠标光标
			playerControler->SetShowMouseCursor(true);
		}
	}
}

UI creation:

I won't talk about the creation of user controls, everyone knows it, it's very simple.

 

Remember to replace its parent class with your own c++ class:

 

Then you can set the test effect in the level blueprint, of course you don’t need to test it, it doesn’t affect it.

Pointer to create button in C++ class:

Remember to add the header file in the cpp file:

#include "Components/Button.h"

Of course, if you want, you can add printing in the following two functions to see if it is linked.

Subsystem Creation

Create a pointer to the subsystem that handles all sessions (in the menu's private):

Now that this class has been created, we need to put the header file of this class in the cpp file if we want to call it in cpp:

#include "MultiPlayerSessionGISubsystem.h"

After that, we get an instance of this class inside the constructor:

The subsystem is created in this way, and the next step is to implement other functions.

Create a session function:

 Things to add before then:

 

 

Add the creation function to this click function, and then we need to implement each different function:

The first one is the code of create session, because there are too many pictures, the pictures will not be displayed, so go directly to the code:

void UMultiPlayerSessionGISubsystem::CreateSession(int32 playerConnectNum, FString MatchType)
{
	//判断会话系统是否有效
	if (!mySessionInterface.IsValid())
	{
		return;
	}
	//有会话则获取现有的会话
	auto ExistingSession = mySessionInterface->GetNamedSession(NAME_GameSession);
	//如果现有的会话并不是空的
	if (ExistingSession != nullptr)
	{
		//删除游戏会话
		mySessionInterface->DestroySession(NAME_GameSession);
	}
	//在会话创建请求完成时触发委托CreateSessionCompleteDelegate,返回句柄到CreateSessionCompleteDelegateHandle,方便之后删除
	CreateSessionCompleteDelegateHandle = mySessionInterface->AddOnCreateSessionCompleteDelegate_Handle(CreateSessionCompleteDelegate);
	//
	LastSessionSettings = MakeShareable(new FOnlineSessionSettings());
	//判断现在的接口是不是空的,是则返回true,不是则返回false【此游戏将仅限局域网,外部玩家无法看到,是true还是false】
	//直接在这里判断你是局域网还不是
	LastSessionSettings->bIsLANMatch = IOnlineSubsystem::Get()->GetSubsystemName()=="NULL"?true:false;
	//可用的连接数量是多少
	LastSessionSettings->NumPublicConnections = playerConnectNum;
	//是否允许加入线程
	LastSessionSettings->bAllowJoinInProgress = true;
	//是否允许通过玩家存在加入
	LastSessionSettings->bAllowJoinViaPresence = true;
	//该比赛是否在在线服务上面公开广告
	LastSessionSettings->bShouldAdvertise = true;
	//是否显示用户状态信息
	LastSessionSettings->bUsesPresence = true;
	//不同key和value的匹配,从现有会话设置中定义会话设置
	LastSessionSettings->Set(FName("MatchType"), MatchType, EOnlineDataAdvertisementType::ViaOnlineServiceAndPing);

	//获取本地的第一个玩家控制器
	const ULocalPlayer* localPlayer = GetWorld()->GetFirstLocalPlayerFromController();
	//*localPlayer->GetPreferredUniqueNetId() 首选唯一网格ID
	//判断创建会话是否成功
	if (!mySessionInterface->CreateSession(*localPlayer->GetPreferredUniqueNetId(), NAME_GameSession, *LastSessionSettings))
	{
		//创建失败
		//委托列表中删除委托,传入的是一个句柄(这个CreateSessionCompleteDelegateHandle在上面获取过)
		mySessionInterface->ClearOnCreateSessionCompleteDelegate_Handle(CreateSessionCompleteDelegateHandle);
	}
}

Go to the lobby after creating a session:

Now back to our menu:

 We implement the create session function so that we can create a session after clicking the button:

 Here's where we should go after creating a session:

But if we test it, we will find that after we come to the new map, we are still in a state of being unable to act.

The reason is naturally very simple, because our input mode is still in a state that only allows ui input

For this, we need to make a function that specifically removes the control

Where to call it afterwards?

overloaded function

For this we need to overload a function:

 Then:

variable added

In the header file of the menu, we need to add some variables, such as the number of people connected, which are prepared for better adjustments later.

 Then do the substitution in the function of the menu settings:

 Then change the create session here to your own private variable:

 test:

 

 

There may be problems

The packaging test found that the session cannot be created when steam is connected

For this case we open:

Change here:

Guess you like

Origin blog.csdn.net/q244645787/article/details/130263575