This is a game backend starter project creating real-time leaderboards using:
Copy and edit the .env
file:
cp .env.example .env
Your .env
file should contain the connection string you copied from Redis Cloud.
Your .env.docker
file will look similar to .env
, but should use the appropriate docker internal URLs. Here is
an example:
REDIS_URL="redis://redis:6379"
Next, spin up docker containers:
docker compose up -d
Alternatively, you can run the development server outside of docker:
npm install
# then
npm run dev
You should have a server running on http://localhost:<port>
where the port is set in your .env
file (default is 3000). You can test the following routes:
-
POST http://localhost:<port>/api/leaderboard
- Add new entry to leaderboard sorted set, requires content body:{ "key": <leaderboard_key>, "score": <score>, "member": <initials> }
-
GET http://localhost:<port>/api/leaderboard/<leaderboard_key>?count=<number_of_entries>
- Get the top scores and the corresponding initials.
- Claim the VaRest plugin in the Fab Marketplace
- In the Epic Launcher, install the plugin to UE.
- Open the RedisRacer project.
- Go to Edit → Plugins
- Search for VARest
- Tick the checkbox to enable the plugin and restart the editor.
- In the Content Browser, create a new blueprint actor named BP_Leaderboard.
- In the Event Graph, create 3 custom events:
- AddLeaderboardEntry,
- GetLeaderboard, and
- CreateRaceEndWidget
- For EventBeginPlay,
- Get the game mode, cast it to RedisRacerGameMode and promote it to variable.
- From the RedisRacerGameMode, bind event to event RaceEnd
- Create a custom event called RaceEnd
- Promote win, score, and initials to variables.
- Call the AddLeaderboardEntry event
- Followed by the GetLeaderboard event
- AddLeaderboardEntry ⇒ RestAPI call to update leaderboard
- For the AddLeaderboardEntry, we’ll use the VARest subsystem
- ConstructJsonRequest
- It will be a POST API call with Json as the ContentType
- From the return value of the Json Request, we will find SetRequestObject
- Drag out from the JsonObject and MakeJson
- Add 3 string elements to the JsonObject
- key,
- score, and
- member
- Create a new variable and name it LeaderboardKey, remember to compile and input the leaderboard key name later.
- Connect the variables, LeaderboardKey, Score, and Initials to the MakeJson node.
- Drag out from the return value of the ConstructJsonRequest and find ProcessURL
- The URL will be http://localhost:3000/api/leaderboard
- From the return value of the ConstructJsonRequest find BindEventToOnRequestComplete and BindEventToOnRequestFail
- We will create one custom event for both of these events.
- GetResponseContentAsString and connect it to a PrintString
- GetLeaderboard ⇒ RestAPI call to retrieve leaderboard
- Use the VARest subsystem.
- ConstructJsonRequest
- The verb is GET and ContentType is Json
- From the return value, find ProcessURL
- I’ll copy the URL from our testing in Postman
- From the return value of the ConstructJsonRequest find BindEventToOnRequestFail and BindEventToOnRequestComplete, but this time we will create two separate custom events.
- GetLeaderboardFail, will be the same GetResponseContentAsString connected to a PrintString for debugging.
- For GetLeaderboardComplete
- Connect GetResponseObject to a BreakJson
- Add one element named “leaderboard” (all lowercase), and it is an array of objects.
- From the array of objects, get a ForEachLoop, for each array element:
- GetStringField with the FieldName of “value”
- GetIntegerField with the FieldName of “score”
- For the string field
- We’ll take a left with count 3, this is to get just the player initials
- We will then append the array index + 1 to designate the player rank on our leaderboard.
- Create a Leaderboard variable with the type of string:integer map to store the results from the GetLeaderboard API call.
- Get a reference to the Leaderboard variable we just created and find Add to add entries.
- Plug in the StringField result and the IntegerField into the Leaderboard.
- When the ForEachLoop is completed, call CreateRaceEndWidget
- CreateRaceEndWidget
- CreateWidget, find W_RaceEnd
- Connect the Win, Leaderboard, and Score variables to the input of the W_RaceEnd Widget.
- Then add the widget to viewport.
- Compile and set the default value for the LeaderboardKey variable, which is “redis-racer” for this tutorial.
- Add the BP_Leaderboard blueprint to the level, and test play!
If you don't yet have a database setup in Redis Cloud get started here for free.
To connect to a Redis Cloud database, log into the console and find the following:
- The
public endpoint
(looks likeredis-#####.c###.us-east-1-#.ec2.redns.redis-cloud.com:#####
) - Your
username
(default
is the default username, otherwise find the one you setup) - Your
password
(either setup through Data Access Control, or available in theSecurity
section of the database page).
Combine the above values into a connection string and put it in your .env
and .env.docker
accordingly. It should
look something like the following:
REDIS_URL="redis://default:<password>@redis-#####.c###.us-west-2-#.ec2.redns.redis-cloud.com:#####"
To learn more about Redis, take a look at the following resources:
- Redis Documentation - learn about Redis products, features, and commands.
- Learn Redis - read tutorials, quick starts, and how-to guides for Redis.
- Redis Demo Center - watch short, technical videos about Redis products and features.