Presence is used to determine the online status about players in your game, generating events to deliver information for their status in real time. Presence is an incredibly important feature for gameplay, as it lets your players know the online status of their friends, whether they are online/offline, currently in a match or lobby, their location in the game world, and even what activity they are engaged in.
While this feature is vital in game development, incorporating Presence is easier said than done. To start from scratch, it takes a lot of resources to build, maintain, and scale when your player count increases. Luckily, PubNub has made it easier than ever to inject real-time functionalities into your Unreal Engine game with our real-time, low-latency API platform. We take care of the infrastructure layer of your apps so that you can focus on your application. Whether you're developing for Windows, Mac, Virtual Reality systems such as Oculus and Meta Quest, or going cross-platform, our Unreal SDK has you covered. And the best part is that you can either use Blueprints, C++, or a combination of both to implement Presence.
Continue reading to learn how to add Presence to your Unreal Engine video game with PubNub, starting from understanding how to configure PubNub and the different features Presence can be used for so players can have a better understanding of the status of their friends and players alike.
Getting Started with PubNub
Before you begin to understand how to set up online status detection with Presence, you’ll need to understand the PubNub platform and how to configure your application to take advantage of the platform’s features.
Overview
PubNub is based on the Pub/Sub (Publish/Subscribe) model. A user will publish a message, which is essentially a payload that contains all relevant information, to the PubNub network. Users who want to receive or listen to the message and other generated events will subscribe to the PubNub network and parse the message.
To ensure the message gets to the right recipients, channels are used as the mechanism through which the data is transmitted from one device to another. Channels are required each time a device wants to publish and subscribe to the PubNub network. While a user can only publish one message at a time, a user can subscribe to many different channels at a time, listening for an assortment of messages. Channels are used in Presence to trigger Presence Events that are sent to event listeners when a user comes online or goes offline, so you’ll need to consider what channels you need that make sense for your game.
Install and Configure the PubNub Unreal SDK
To begin, you'll need to install any PubNub dependencies and configure the PubNub Unreal SDK to connect your application to the PubNub network. This guide assumes you have Unreal Engine installed (v.5.0 or later) and code editors such as Visual Studio/Rider/XCode installed. Please refer to the Unreal SDK docs for full details, but as an overview for the workflow, you will need to:
Download the PubNub Unreal SDK Plugin from GitHub.
Create a free PubNub account and obtain your PubNub Keys. You’ll need to enable the features on your keyset that your application needs. For this guide, you’ll need to enable Presence with the default settings. You can learn more in our How to Enable Presence guide.
Create a new Unreal project or open your existing game. If you are creating a new project, you’ll need to create an empty
Plugins
folder in your Unreal project’s root directory. You can also download an existing Unreal Project that is already integrated with PubNub and simply needs the PubNub Unreal SDK Plugin installed (the folders are already set up).Configure your PubNub object with your API keys and set a User ID.
Add an event listener for your game to react to Events, where your back end will be receiving notifications about when players come online and go offline.
Blueprints:
C++:
There are different Events that the event listener will capture, but for this guide, you’ll simply need to capture the Presence Event.
Subscribing & Presence
Once you’ve finished configuring your PubNub GameObject and setting up the event listener, you can determine how many players are actively online, as well as capture events for when players come online and go offline.
Determine Initial Player Count
You can obtain information about how many players are currently active when your players first log into the game by listing the users subscribed to a specifically designated Presence channel via ListUsersFromChannel:
Blueprints:
C++:
ListUsersFromChannel
returns information about the current state of a channel including a list of unique user-ids currently subscribed to the channel and the total occupancy count. You can utilize this information to update any relevant UI, such as for player counts.
Listen for Events
Once you’ve determined the current online status of all active players when you first load the game, you’ll need to listen for Presence Events via the Event Listener that we set up earlier to catch when players join and leave the game.
To do this, you’ll need to first subscribe to the channels you want for the player to connect to the PubNub platform. PubNub will then trigger presence (and other) events as users come online or go offline from the application. For instance, if you want to track the total player count and display it on the main menu of your game, consider having a dedicated global player count and ensure that every player subscribes to this channel. If you want to track when friends come online, consider using Channel Groups to manage a large group of channels and subscribing to that channel group to only receive events that are relevant to that player.
Regardless of how many channels or channel groups you would like to subscribe to, you can subscribe for Presence events by subscribing to the channel by appending -pnpres
at the end of the channel. This is because Presence Events generate many transactions as one would imagine and is useful to separate Presence Events into their channel to manage. These presence channels are named after the main ones but contain an additional -pnpres
suffix:
Blueprints:
C++:
In this call, Presence Events are generated and caught by the listener for the randomChannel-pnpres
channel.
Regardless of how you decide to subscribe to generate Presence Events, once a player subscribes using one of these methods, a Presence Event is generated. There are five main types of presence events:
join: Fires when a user subscribes to a channel.
leave: Fires when a user unsubscribes from a channel (when unsubscribe is called, closes the game, or the connection is terminated).
timeout: Fires when a connection to a channel is lost and the subscriber hasn't been seen in 300 seconds.
state-change: Fires anytime the user's state is changed. More on state in the next section
interval: Fires in interval mode to provide an occupancy count and optionally include delta changes. You can learn more about interval mode and delta changes in our Presence How-to.
Once you receive the event in the Presence Event Listener, you’ll need to check the type of the event and handle it appropriately.
Implement Custom Player Status and Location: Presence State
Nice work - you have successfully implemented Presence in your Unreal Engine game for players to be able to obtain the initial online player count, as well as listen for Presence events when players come online when they subscribe to the PubNub network via the Presence Event Listener. While this is already a powerful feature that is both easy to implement and scalable, online games should be able to provide more specific information to players about where their friends' exact online status is while in the game - are they in a lobby, playing a match, browsing their collectibles, or clearing a dungeon with a group?
To remedy this issue, you can implement a dynamic custom state using Presence State for your players on one or more channels and store this information on a channel as long as that player stays subscribed. This means that as long as they are online, you can update and retrieve their custom state at any point, which is perfect for displaying this information for other players. The events generated when you are retrieving or setting their custom state will be caught by the same Presence Event Listener we set up earlier. Just make sure to check that the event is of type state-change.
You can use the Presence State API for the Unreal SDK to get the Presence State of another player by supplying the specific player’s User ID and channels:
Blueprints:
C++:
In this call, we are obtaining the custom state of a given UserID
for the channel ChannelName
that can be used to track this specific player’s current status within the game. Although the example is extremely simplified, being able to obtain the custom state of friends and other players within the game is extremely powerful.
To be able to change a player’s state once they transition to a new activity or location, you can call the SetState operation:
Blueprints:
C++:
With the SetState()
operation, we are setting the custom state of a user (after formatting the FPubnubSetStateSettings structure) once they transition to performing a new activity, such as opening loot boxes. Since the State passed contains keys that are type object, you can elect to set custom metadata outside of just location and status to be accessible by other players. However, keep in mind that Presence State is only available to be retrieved for players who are actively online and connected to the PubNub network. To store custom metadata about players that you would like to persist, such as profile information and settings, you will want to use the App Context API.
Whenever a user’s state changes, other players will receive a Custom Presence Event (State Change) that can be used to update their UI depending on the game mode.
What’s Next
In this how-to guide, you’ve learned how to add Presence to your Unreal Engine game to track the online and custom status of players in your game. We've discussed everything from how to initialize a PubNub object in your environment, to subscribing to channels to receive Presence Events and even retrieving/setting custom statuses for your players to manage their specific location/activity in your game.
Learn more with the following resources:
Read our Unreal SDK documentation to learn everything you need to know about adding real-time presence to your game, as well as other real-time multiplayer features such as matchmaking, live leaderboards, in-game chat, player inventories.
Take a look at a sample Unreal project utilizing the PubNub Unreal SDK that implements in-game chat and presence.
Dive into the Unreal SDK source code.
Feel free to reach out to the Developer Relations Team at devrel@pubnub.com for any questions or concerns.