Last Updated: 8/19/2022, 9:45:54 PM

# AccelByte Cloud Peer to Peer for Unreal Engine

# Overview

The AccelByte Cloud Peer-to-Peer (P2P) for Unreal Engine allows players to conveniently establish peer-to-peer connections across private networks through the AccelByte Turn Server using AccelByteNetworkUtilities as an Interactive Connectivity Establishment. P2P needs the AccelByte Online Subsystem (opens new window) to allow it runs using AccelByte Service.

# Prerequisites

# Configuration

# Set Up Turn Server

To enable P2P service on your game, you need to add the Turn Server credentials. Go to Open the Config > DefaultEngine.ini file and add the following code.

NOTE

AccelByte will provide you with the TurnServerSecret credentials.

# Implement the P2P Service

TIP

Because implementing the P2P service may vary for each game, you can give it a try first with variables, structs and functions inside OnlineSessionInterfaceAccelByte.h in the AccelByte OSS plugins Source folder. You can customize all the results and delegates.

  1. Create four widget C++ classes to specify the interactions between session functionalities.

    • AccelByteCustomGames: to create and find all available sessions.
    • AccelByteServerListEntry: to represent one session after finding one available and joining the session.
    • AccelByteServerMenu: to act as an in-game lobby and to destroy a session or start the gameplay.
    • AccelByteInGameMenu: to end the session once the gameplay is finished via pause menu.
  2. Add an OSS header at the top of the classes, to ensure all the session-related functionalities are working as intended.

  3. In AccelByteCustomGames, we will implement two functionalities with their delegates:

    • Create Session By creating sessions, players can play a game with their friends or parties without being interrupted by another player.

    • Find Session

      By finding the available sessions, players can find out about which session they are more comfortable with.

  4. In AccelByteServerListEntry, we will implement the following functionality with the delegates:

    • Join Session

      After finding the right session, players can join it using the SessionData which is generated by finding session.

  5. In AccelByteServerMenu, we will implement two functionalities with their delegates:

    • Destroy Session

      If the session is not in use anymore or the player needs to leave the session, we can call destroy session.

    • Start Session

      When the players are ready to play, they can start the game by using this functionality.

  6. Lastly, in UAccelByteInGameMenu, we will implement one functionality with the delegates:

    • End Session

      If the game is finished, and the players need to go back to a UI menu, we can call the end session.

  7. Notice that each of the functionalities comes with a delegate. It is used to receive the response after the game sends a request. And this response can be modified depending on the game flow you want to create.

# Step-by-Step Guide

# UI Implementation

  1. Create a widget blueprint class named WB_CustomGamesMenu. This widget will list all the available sessions and create a new session. Make sure it contains the following items:

    • Scroll Box to list all the available sessions.
    • Text Box to enter a name and session password.
    • Buttons to:
      • Create a new session by clicking the Create Server button.
      • Refresh the available session by clicking the Refresh List button.
      • Log out from the game by clicking the Log Out button.

    p2p-unreal-engine

  2. Create a widget blueprint class named WB_ServerListEntry. This widget will represent available sessions. Make sure it has the following:

    • Text Box for:
      • Server name
      • Current session capacity
      • Available game mode

    p2p-unreal-engine

  3. Create a widget blueprint class named WB_ServerMenu. This widget acts as a Lobby before the players start their gameplay. Make sure it has the following items:

    • Scroll Box to list all the players who join the current session.
    • Button to:
      • Start the session by clicking the Ready button.
      • Leave the session by clicking the Exit button.

    p2p-unreal-engine

  4. Create a widget blueprint class named WB_ServerPlayerEntry. This widget will represent the players who join the current session. Make sure it has the following:

    • Text Box to display the player’s name and their ready status.

    p2p-unreal-engine

  5. Create a widget blueprint class called WB_Pause. This widget acts as a pause screen after the gameplay has started. Make sure it has the following:

    • Button to:
      • End the session
      • Close the pause menu.

    p2p-unreal-engine

# Code Implementation

# Creating a Session
  1. Create a new C++ class that inherits from UUserWidget named AccelByteCustomGames, and re-parent WB_CustomGamesMenu with our new class.

  2. At the top of C++ class, include OSS header files.

  3. Create a button that will be used to trigger create server in the AccelByteCustomGames.h class.

  4. Create a function called CreateCustomGamesSession() to trigger the create session functionality. You can modify the SessionSettings as you wish. This SessionSettings will act as a session setting and will contain rules, target map, etc.

  5. Add initialization in the NativeConstruct() for the Create Server button to trigger the create session.

  6. Create another function called CreateSessionComplete(). to act as delegate . This will be triggered after a request to create a new session is complete.

  7. Specify the session name and bIsSuccess response which is a default response from Unreal Engine OSS when the create a new session request is complete.

IMPORTANT

Before moving to Find Session, to make sure the P2P works after its creation, we suggest always setting the value of SETTING_ACCELBYTE_ICE_ENABLED to true when you create a session. This is used to turn on the NAT relay.

# Find Session
  1. In the AccelByteCustomGames, create a button that will be used to trigger find session functionality. This button also can be called as a Refresh List Button.

  2. Create a function called FindCustomGamesSession() to find sessions.

  3. Create a pointer inside that function called SessionSearch which will act as a filter and will contain the session find result. Now, modify that pointer as necessary.

  4. Add initialization in the NativeConstruct() for the Refresh List button to trigger find session.

# Join Session
  1. Create a new C++ class that inherits from UUserWidget named AccelByteServerListEntry, and re-parent WB_ServerListEntry with our new class.

  2. At the top of C++ class, include Online Subsystem (OSS) header files.

  3. Let’s create a button called Exit Session button that will be used to trigger the destroy session functionality.

  4. Now, create a function called DestroyCustomGamesSession to trigger the destroy session.

  5. Add initialization in the NativeConstruct() for the Exit Session button to trigger destroy session.

  6. Create another function called DestroyCustomGamesSessionComplete() to act as delegates. This will be triggered after a request to join a session is complete.

  7. Specify the session name and bIsSuccess response which is the default response from Unreal Engine OSS as below.

# Destroy Session
  1. Create a new C++ class that inherits from UUserWidget named AccelByteServerMenu, and re-parent WB_ServerMenu with our new class.

  2. At the top of C++ class, include Online Subsystem (OSS) header files.

  3. Create a button called Exit Session button that will be used to trigger the destroy session functionality.

  4. Create a function called DestroyCustomGamesSession to trigger the destroy session.

  5. Add initialization in the NativeConstruct() for the Exit Session button to trigger destroy session.

  6. Create another function called DestroyCustomGamesSessionComplete() to act as delegates . This will be triggered after a request to destroy a session is complete.

  7. Specify the session name and bIsSuccess response which is the default response from Unreal Engine OSS, as below.

# Start Session
  1. Create a button called Start Session button that will be used to trigger the start session functionality.

  2. Create a function called StartCustomGameSession() that will be used for the start session.

  3. Add initialization in the NativeConstruct() for the Start Session button to trigger the start session.

  4. Create another function called StartCustomGamesSessionComplete(). to act as delegates. This will be triggered after a request to start a session is complete.

  5. Specify the session name and bIsSuccess responses which are the default responses from Unreal Engine OSS, as below.

# End Session
  1. Create a new C++ class that inherits from UUserWidget named UAccelByteInGameMenu, and re-parent WB_Pause with the new class.

  2. At the top of C++ class, include OSS header files.

  3. Create a button called End Session button that will be used to trigger the end session functionality.

  4. Create a function called EndGamesSession() that will be used for the end of the session.

  5. Add initialization in the NativeConstruct() for the End Session button to trigger the end session.

  6. Create another function called EndSessionComplete() to act as delegates . This will be triggered after a request to end a session is complete.

  7. Specify the session name and bIsSuccess response which are default responses from Unreal Engine OSS, as below.