How to Make a Multiplayer Game in Unreal Engine 4 [Blueprint Tutorial] (Unreal) – Part 1 – by DevAddict

April 27, 2021

Multiplayer

Unreal


Title:
How to Make a Multiplayer Game in Unreal Engine 4 [Blueprint Tutorial]

By:
DevAddict


Youtube – Tutorial

Description:
A tutorial extension to the previous Unreal platformer tutorial that shows multiplayer implementation.

Summary

This tutorial extends the previous tutorial on making a platformer in Unreal found here:

Youtube – Lets Make a Platformer – Unreal Engine 4.26 Beginner Tutorial

The original tutorial follows one created by Unreal with some extra steps added. This tutorial is an expansion made by DevAddict specifically to show how to add multiplayer to this project.

Lesson 1: Introduction to Multiplayer

Play Modes

When going into Play Mode in Unreal, there are many options for testing and debugging multiplayer.

    Play Modes: Net Modes:

  • Play as Offline (Standalone): (Default) You are the server
  • Play as Listen Server: Editor acts as both the Server and the Client
  • Play as Client: Editor acts solely as Client and a Server is started behind the scenes for you to connect to

Testing Multiplayer Settings

Approach #1

  • Set Number of Players to: 2
  • Set Play Mode to: Play as Client

This tests both windows as if they were both individual clients.

Approach #2

  • Set Number of Players to: 2
  • Set Play Mode to: Play as Listen Server

The Editor will act as the Server (host) and the extra windows will act as Clients connected to that Server. This helps point out differences occurring between the Server and Clients for debugging multiplayer actors.

Editing Blueprints

Editing Game Mode

The Game Mode class only exists on the Server. It does NOT exist on any of the Clients.

This fact is why transitioning this tutorial to multiplayer causes many issues, the first of which is fixing the UI so it displays for all players. It originally only displays for the Server player because much of the programming for it is within the Game Mode class. Similarly, the respwan code is also only in the Game Mode.

Use “Event OnPostLogin” node

-> “Cast To BP_PlatformerController” node

-> Client Draw HUD (Event we created in the BP_PlatformerController class)

This tells the Game Mode (Server) that when a new player logs in and has their own Player Controller created, that that specific instance will create its own HUD for that individual Client. Note that they intially tried the “Event Handle Starting New Player” node in place of the “Event OnPostLogin” node, which did create the UI, but it did NOT create the character (so in the Unreal editor you just moved around as a camera). This approach may work with some extra modifications, but it did not direclty work in this instance.

Player Controller

The Player Controller is very powerful in multiplayer because it is replicated on the Server and the Client. They like to keep UI on the Player Controller because it exists throughout the play session. While the character may be destroyed in some instances, the Player Controller generally persists. This makes the Player Controller beneficial for respawning mechanisms as well.

Building a Player Controller:

Right-Click -> Blueprint Class -> Player Controller
Named: BP_PlatformerController

You need to connect the Player Controller and your Game Mode, as they work together to realy information between players and the Server.

In the Game Mode class (ThirdPersonGameMode) Event Graph -> Details -> Classes -> Player Controller Class -> Use dropdown to select new Player Controller (BP_PlatformerController)

Common Error – Event BeginPlay to Initialize Player Controller Blueprint

When initializing their Player Controller class, many may try using the “Event BeginPlay” node. This works for single player, which is why it may be prevalent, but it does not work for a multiplayer project. Instead you want an event that will run on the Client ONLY.

Moving HUD from Game Mode (Server) to Player Controller (Client):

Add Custom Event

Connect Custom Event to start of class

In Details of Custom Event -> Graph -> Replicates: Run on owning Client -> Replicates: Check ON Reliable

via Blogger http://stevelilleyschool.blogspot.com/2021/04/how-to-make-multiplayer-game-in-unreal.html

Online Multiplayer Networking Solution Tutorial Using Unity and Mirror – Tutorial by: Jason Weimann

February 2, 2021

Networking Online Multiplayer

Unity & Mirror

Title:
Let’s build a 4-Player Networked Game LIVE – Online Shooter (with Mirror & Unity)

By:
Jason Weimann


Youtube – Tutorial

Description:
Intro to using Mirror for networking online multiplayer play in Unity development.


Introduction

This tutorial has Jason Weimann implementing online network play into a basic Unity twin-stick shooting game. They use Mirror, which is a Unity asset used for simplifying the online network synchronization process. This is a live implementation where they work through many errors transferring a game from simply working locally to working with a host/client relationship.

Mirror

Mirror – Home Page

Mirror is “a high level Networking API for Unity, supporting different low level Transports” (from Mirror themselves). It is a clean solution for implementing a quick and simple online networking option for Unity projects. The core components breakdown as such (supplied by their site):

  • [Server] / [Client]: tags can be used for the server-only and client-only parts
  • [Command]s: are used for Client -> Server communication
  • [ClientRpc] / [TargetRpc]: for Server -> Client communication
  • [SyncVar]s and SyncLists: are used to automatically synchronize state

Authoritative Server

When creating a networking environment for a project, it is important to determine what aspects of it are determined server-side and what are determined client-side. With games, most information should generally be handled server-side since this helps prevent cheating or hacking. Most games with larger scale player bases will have dedicated servers to handle the online play of the games, and these handle a majority of the data as well as checking data coming in from the clients helps in the efforts to mitigate cheating.

As they go through the tutorial, the only information they end up handling client-side is that specific player’s transform. To help keep the game feeling as clean and smooth as possible for the player, they at least allow this to be determined client-side so any of their movement is quickly shown to them. This information is then sent to the server to be distributed. While this opens an avenue for cheating, the data being sent from the client can be checked before truly being implemented if this is a real concern.

After that almost everything is handled on the server-side. When the client wishes to do something, the server generally runs the actual logic and then sends the data to the client using a Mirror [ClientRpc] attribute. Many of the major mechanic handling scripts then only run ifServer and handle the information coming in to determine what events will actually occur.

List of Client-Side Authorization

  • Player Movement

List of Server-Side Authorization

  • Bullet Spawn
  • Bullet Transform
  • Enemy Spawn
  • Enemy Transform

Transitioning from a Local Project to a Network Project

Removing Duplicated Logic

One of the common issues they ran into in the tutorial while transitioning from a basic local project to a network project using Mirror was that in their efforts to have the server handle most logic, sometimes they would accidentally have the server as well as the client running the same logic. This produced weird results in various cases, from stuttering enemy movement to strange projectile effects. This would happen when adding to functionality of only running something server-side, but then forgetting to remove the logic from occurring just locally. This could cause instances of the same logic running twice effectively.

Basics of Testing Networking in Unity with Mirror

As this can lead to many bugs and errors, testing is critical when dealing with networking elements. There are several ways to go about this, but one of the simplest and what they use in the tutorial is building the project, then using that build as one network user (Client/Host) and the Unity Editor as the other (Host/Client). Mirror allows for a quick GUI implementation providing a Host button and Client button to connect to said Host using an IP address. “LocalHost” can be used in place of the IP address when doing the suggested testing, as this has the client look on its own computer for the game host.

Further details on the Network Manager HUD and using the base network connectivity can be found here on Mirror’s site:

Mirror – Network Manager HUD

Summary

This tutorial seems to show that Mirror is a decent option for quickly implementing an online network multiplayer solution for your simpler Unity projects. This simplified approach to setting up a network project also seems like good practice for getting your feet wet with dealing with networking implementation into Unity projects in general. It still requires client-side and server-side differentiation and managing what data is handled where and passing data between the two, so this appears to me as good practice for understanding these concepts. It also does just appear to work rather easily, so if you just want to get some kind of online multiplayer working for your project this seems like a useable solution.

via Blogger http://stevelilleyschool.blogspot.com/2021/02/online-multiplayer-networking-solution.html