A common feature that developers want to implement for their multiplayer games is to display the number of players who are currently online. This can be something useful for multiplayer games, because players might want to know how many other players are online at the current time for deciding which match types to play.
In this tutorial, we'll show you how this can be done using Redis to store your online player data.
What is Redis?
Redis provides a way to quickly store simple data structures as key/value pairs, and you also have access to a Redis instance for your game. Redis is:
- A fast, efficient, in-memory data store and is significantly faster than database solutions for storing and retrieving small quantities of data.
- The option of choice when you want to perform fast data manipulation on transient data, such as Set operations, numeric sorting of data, and so on. For more information on the type operations you can perform, please see our API page here.
Configuring Player Connected
The first thing we need to do is head over to our Cloud Code editor by going to Cloud Code > System > Player Connected. This is where we'll be putting our Cloud Code logic for when a player connects to the game.
When the player connects, we need to a get a reference to Redis and check if it contains the key OnlinePlayers. If this is the first time a player is connected, then this value will be null because we haven't set it yet. If it is null, we have to initially set the value of the OnlinePlayers in Redis.
After the initial value is set, the next time a player connects the value stored in Redis will be incremented by one. This is done by using the incrBy operation and passing in a numeric value to increase the value by that amount:
Configuring Player Disconnected
To add the Cloud Code logic for when a player disconnects, head over to Cloud Code > System > Player Disconnected. This Cloud Code script is triggered when there is a clean disconnect - that is, the player quits the game. If there is an unclean disconnect - that is, the player's internet drops - the websocket connection can be left open for up to 30 minutes, which gives the player time to reconnect. In this kind of instance, the script won't be executed until the connection is fully closed.
The Cloud Code in this script is similar to the previous script except we are decrementing the value this time. This is done by using the decrBy operation and passing in a numeric value to decrease the value by that amount:
Configuring Get Online Player event
Now that we have set up our logic for when a player connects and when a player disconnects, we need an Event to get the value for the number of currently connected players and send the value back to the client.
To create a new Event, go to Configurator > Events > Add new Event. In the Event configuration screen, give the Event a Shortcode, Name, and Description. In this example, we've called the Event GetOnlinePlayers with the same Shortcode. Note that we haven't add any Attributes for this Event, because none are needed for this example.
Go back to the Cloud Code editor and you should see your newly created Event in the Events folder. You can add this Cloud Code script to your Event to get the value stored in Redis and send it back to the client via scriptData when the LogEventRequest for GetOnlinePlayers is called:
On the client, you can now:
- Parse this scriptData by supplying the key you set for your scriptData into a local variable.
- Use this variable to then display in a GUI the number of online players.
Lastly, please take note when calling the GetOnlinePlayers LogEventRequest. You might be tempted to call this request on every frame to get the most up to date value. However, we strongly recommend that you don't do this, because this would not be scalable in the long term and will cause performance issues for your game. Instead, we recommend calling this request periodically, such as every 5 to 10 minutes, because it's not necessary for most use cases that players know the exact number of players online right now, and it would serve adequately to have an estimated value.