Gaming

Add Trading and Leaderboards to your Unity Game with PubNub

Darryn Campbell on Aug 30, 2023
Add Trading and Leaderboards to your Unity Game with PubNub

This blog is the second part of a two-part series about adding real-time capabilities to your Unity game with the PubNub Unity SDKPart one described messaging and social features.  This second part will cover item trading, real-time leaderboards, and moderation.

The best way for any game to improve player engagement and retention is to integrate real-time interactivity; this means features like chat, in-game alerts, leaderboards & presence. However, developing these features yourself can take time and effort. PubNub can help.  We are trusted by multiple customers whose players depend on PubNub daily to make their gaming experience engaging and interactive.

The PubNub Unity Showcase application, available on GitHub, shows how you could add real-time features to your application, so let’s look at how we implemented some of that functionality.

Player Item Trading

Trading items within any game will increase a player’s engagement and enhance their experience.  By introducing rare items, a game designer can increase a game’s competitiveness and give the player a sense of achievement as they ‘trade up’ gradually to complete their inventory.  Item trading is also an important revenue stream for many games, as the process can be monetized, allowing the player to become more invested in the game [pun intended].

Item trading is a great fit for PubNub; you want high availability and immediate feedback through your user experience front-end as trades complete.  To prevent cheating, you also want to encrypt messages and prevent message interception, which is possible using PubNub.

The Unity showcase app has a basic implementation of trading whereby hats are exchanged between different players.  The workflow allows any player to propose a trade to another online player who can choose to accept, reject or counter the offer.  This workflow uses PubNub’s Pub/Sub API, described earlier for chat, to exchange real-time messages and agree on a trade between two remote participants. 

Item Storage

There are multiple ways to define what is traded, but the Unity showcase app uses PubNub’s Application Context feature.  App Context is a serverless storage solution for any custom data, but it was originally designed with player attributes in mind, i.e., player name and avatar alongside any custom data, such as, in our case, the player’s inventory of hats. 

Any authorized player can retrieve any other player’s attributes and be informed of changes in real-time, making it trivial to implement an item trading system with PubNub.  The following code will retrieve a player’s attributes from App Context and notify other players when that inventory changes: 

Leaderboards & Challenges

PubNub lets you stream live score updates and player statistics to real-time dashboards.  Leaderboards can add challenge to your game and extend its lifespan, fostering competitiveness amongst your player base. 

To create a leaderboard for any aspect of your game, you need a global view of all your player data; either you could have players submit their high scores or achievements to your custom backend and handle the infrastructure yourself, or you could make use of PubNub’s serverless functions to create and distribute your dashboard for you automatically.

PubNub functions capture events happening on the PubNub platform and allow you to write code to re-route, augment, filter, and take action on the message data.  Creating a leaderboard in PubNub only requires three steps:

  1. Each gamer submits their score on a predefined channel; in the case of the Unity showcase application, this is 'score.leaderboard'

  2. A PubNub function listens on the 'scores.leaderboard' channel, and whenever a message is received with a new score, that score, along with the player name, is added to a key-value store controlled by the function.  You can find the code listing for the Unity showcase app’s leaderboard function in the repository’s readme file.

  3. The updated leaderboard is published to all recipients so everyone knows the leaderboard has been updated; the Unity showcase application uses the 'leaderboard_scores' channel for this.

Scalability is critical with multiplayer games.  As your player base scales, PubNub scales with you, so your leaderboard logic will always be optimized to handle the required number of score updates.

Challenges

Many games add challenges and achievements to extend their replay value.  Tracking and competing with your friend’s achievements is far more engaging than just grinding your own achievements offline.

PubNub is ideal for tracking achievements.  You could either track achievements locally in the game for later upload or choose to detect the achievements in a serverless function in real-time.  Using a serverless function would also give you more flexibility as a game developer, allowing you to add achievements or tweak existing ones without forcing players to update their clients.

Once gained, any achievement could be immediately shared with their friends over PubNub, similar to how in-app chat messages are exchanged (as described previously).

Moderation & Translation

What’s better than having an awesome community form around your game?  Probably having that community be global.  But the larger your community becomes, the more important it is to ensure that player interactions are moderated to prevent abuse in all forms.

All of this is possible with PubNub, specifically using Functions.  As discussed previously, when describing a leaderboard implementation, PubNub Functions capture events happening on the PubNub platform and allow you to take actions based on events generated by those messages.  Specifically for translation and moderation, a function can be run against the ‘Before Publish or Fire’ event, allowing us to intercept and modify any message before forwarding it to the intended recipients.

Translation

The Unity showcase app can translate your messages into around 25 different languages; this works as follows:

  1. The player can choose their language from the settings screen

  2. Messages are composed by the sender and sent over PubNub along with their locale.

  3. When received, if the message is in a different language from the recipient’s selected language, it is sent through the 'translate.' channel to be translated.

  4. A PubNub function is listening on the 'translate.*' channel, so will receive the message, translate it into the desired language, and then send the translated message back to the sender.

How the message is translated within the PubNub function is flexible; PubNub offers a wide variety of connections to external services, including translation services, through the PubNub Integrations catalog.

Moderation

PubNub functions can filter out any message profanity and analyze the sentiment of messages and exchanges to determine whether the conversation content is harmful.

The workflow is a simple interception of messages before they are forwarded to their recipient.

  1. Messages are sent from player A to player B (or to a group of players, etc.)

  2. Messages are intercepted by a PubNub function listening for all chat messages using a wildcard pattern, ‘chat.*’

  3. The received message is analyzed, and any harmful content is replaced with asterisks before being allowed to continue onto its original recipient.

Again, the PubNub Integrations catalog has a range of moderation services ranging from simple regex substitution to more complex heuristics.  Note that the message is blocked en route while it is moderated, so the complexity of the chosen service and the roundtrip latency to that service can cause a delay in receipt. 

The Unity showcase app will filter out any message profanity before it reaches the recipient regardless of language, but will not analyze the message for harmful sentiment. 

In Summary

PubNub can help you improve your player engagement and retention by quickly integrating real-time features into your video game regardless of scale.  Please check out our gaming solution page and Unity software developer path to learn more. 

To get started with PubNub, you can sign up for a free trial account with no obligation and follow our Unity tutorial. When you are ready to continue your journey or have questions, our support and devrel teams are here to help.

PubNub is a cross-platform solution suitable for enterprises offering SDKs for multiple languages and platforms, including Unity, Apple iOS, Android (including push notifications), JavaScript, Python, React, and C# for Microsoft Windows. PubNub supports a wide range of use cases and verticals, from gaming to IoT, Robotics, blockchain, and e-commerce.