Detailed explanation of UE4/5 multiplayer games (2. Connect to steam through OnlineSubsystem and OnlineSubsystemSteam, such as realizing shift+tab to open the pop-up window of steam in steam games)

Table of contents

IP address

OnlineSubsystem

plug-in open

module loading

Config modification

Cpp

test:


In the previous article, we explained the productions connected to each other under the same WiFi.

But we don't just want to be on the same local connection, what we want is to play games together under different WiFi.

So let's first explain the ip address:

IP address

Now under the same WiFi, the ip addresses of the 3 computers are different in order.

In this way, only player A's computer can connect to player B's and player C's computers through the ip address .

Then in order for your WiFi to connect to the Internet, it will give your WiFi an address from the outside [this way you can think it]

External information will flow from the outside to your WiFi and then to your players.

As shown in the picture:

So before the local connection is so simple.

So how and, if the other player is on the other side of the world, how are you supposed to play the game together?

And there are many platforms we need. If we publish on different platforms, we need to learn some codes of many different platforms, such as steam, Xbox and so on.

The Unreal Engine provides an online subsystem, we only need to decide through this:

He has an abstraction layer that abstracts other platform-specific code away, so we don't have to worry about it.

And this system is called: OnlineSubsystem

OnlineSubsystem

plug-in open

Let's take steam as an example:

We open the project, open the plugin, and search for:

You can see many platforms here, but here we are using this one from steam:

 

module loading

Because this plug-in is opened, now we enter VS to see:

This module needs to add an OnlineSubsystemSteam and OnlineSubsystem, if you don't know it, see:

Then it looks like this:

This way we can access the relevant modules.

Config modification

Find your project file, and open config.

The next thing we open is the DefaultEngine.ini file

We need to add something to this file to configure our project to use steam. For details, see:

Unreal Engine Online Subsystem Steam Interface | Unreal Engine 5.1 Documentation (unrealengine.com)

Inside this we can find:

The first one is to define the network driver as steam's network driver, so that we can connect to steam.

The second is to define the online subsystem as steam, so that the network configuration of steam can be used

The third is the default application development ID, which is 480 by default. If you don’t have one, just use 480.

Now we copy this stuff into our DefaultEngine.ini :

Now we close vs and the editor, open the project file, delete some unnecessary things, and regenerate vs:

Then right-click the Unreal logo, Generate Visual Studio project files :

After double-clicking, it will say that you are missing files, but don't worry, just click Yes, because he will regenerate the Binaries file .

Cpp

In the header file create:

TSharedPtr is a smart pointer to a non-UObject

//向前声明的接口类
	//typedef TSharedPtr<IOnlineSession, ESPMode::ThreadSafe> IOnlineSessionPtr
	//class IOnlineSessionPtr myOnlineSessionInterface;因为是多线程的,所以我们并不能这样使用,当然,如果你将#include "Interfaces/OnlineSessionInterface.h"的头文件放在头文件中,也可以使用这个【class要去除】
	TSharedPtr<class IOnlineSession, ESPMode::ThreadSafe> myOnlineSessionInterface;

Then enter the player's cpp file:

#include "OnlineSubsystem.h"

//你也可以将这个放在头文件中,然后用这个IOnlineSessionPtr myOnlineSessionInterface
#include "Interfaces/OnlineSessionInterface.h"

Then create the pointer in the constructor:

 

and then print:

Here we may make an error when compiling

We don't need to worry about this, it's normal, as before, we exit, delete binaries , saved , intermediate , and then re-update.

test:

After running here, you can see that the pointer NULL is known here . In fact, this does not mean that you have found an empty NULL , but because there is an OnlineSubsystem named NULL in Unreal Engine (this can be used to test the connection in the LAN).

Then you will find that no matter how you set it in the editor, it is always NULL , not steam .

Next, you only need to pack it , and then open it, and you will find that the upper left corner is connected to steam .

Here you can do some steam operations (such as the window where the evil shift+tab appears) 

Guess you like

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