[UE4] Multiplayer Online Tutorial (Key Notes)

Effect

1. Create room and search room functions

2. Join the game according to the specified IP and port

step

1. Create a new third-person character template project

2. Create a blank level, named here "InitMap"

3. Create a new control blueprint, named here "UMG_ConnectMenu"

Display the control blueprint in the level blueprint

Open "UMG_ConnectMenu" and add the following controls

First add the click event of the create room button

        A session is created when this button is clicked. The PublicConnections parameter in the Create Session node can help you control the number of client connections accepted by the server. When a new client tries to connect to the game session, if the current number of connections in the game session (the number of connected clients) is less than the value specified by the PublicConnections parameter, the new client connection will be accepted and added to the game session. . If the maximum number of connections specified by PublicConnections has been reached, new client connections will be rejected. For example, if you set the PublicConnections parameter to 4, your game session allows up to 4 players to connect simultaneously.

        The Use Lan parameter indicates whether to use the LAN

 Change the value of the Boolean variable "IsUseLAN" when the checkbox state changes

Open a new level after creating a room

Run at this time, click the Create Room button, you can see the output log, the server uses port 7777 by default:

 Continue to add click events for searching rooms:

Among them, the MaxResult pin of the search session node indicates the upper limit of the number of search results. When the search for session results is successful, we promote the Results return value to a variable. If it fails, the failure message is printed.

We plan to display the searched room information in the following areas:

4. Create a new control blueprint, named here "UMG_SessionSlot", to display room information

Open "UMG_SessionSlot" and add the following controls:

 Add an event dispatcher to the chart

Open "InitInfo" and add 4 inputs

Set text control content in event chart

Add join button click event

 The variable "Search Result" type is the blueprint session result. Check the editable instance and make it public when generated.

5. Return to the control blueprint "UMG_ConnectMenu" and continue to complete the search room logic

 Add the change event of the IP and Port input boxes

 Store the input IP and port as text type variables

 Add click event for enter button

Add input console command and execution button click event

 6. Set up multiplayer options

 The running effect is shown at the beginning of the article.

Guess you like

Origin blog.csdn.net/ChaoChao66666/article/details/131983766