Prerequisites
- Unreal Engine 5.x (C++ project)
- A LightLeaderboard account, game, and API key
- The
HTTPmodule added to your.Build.cs
Blueprint support: The component below is marked
BlueprintCallable — once you compile, you can call SubmitScore and GetLeaderboard directly from Blueprint nodes.Step 1 — Enable the HTTP Module
Add "HTTP" to PublicDependencyModuleNames in your game's .Build.cs file.
// MyGame.Build.cs — add "HTTP" to public dependencies
PublicDependencyModuleNames.AddRange(new string[] {
"Core", "CoreUObject", "Engine", "InputCore",
"HTTP", // <-- add this
});Step 2 — Create the Component Header
A UActorComponent keeps the API logic self-contained and attachable to any Actor.
// LeaderboardComponent.h
#pragma once
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "HttpModule.h"
#include "Interfaces/IHttpRequest.h"
#include "Interfaces/IHttpResponse.h"
#include "LeaderboardComponent.generated.h"
UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent))
class MYGAME_API ULeaderboardComponent : public UActorComponent
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable)
void SubmitScore(int32 Score, FString PlayerId, FString PlayerName);
UFUNCTION(BlueprintCallable)
void GetLeaderboard(int32 Limit = 10);
private:
static const FString ApiUrl;
static const FString ApiKey;
void OnScoreSubmitted(
FHttpRequestPtr Req, FHttpResponsePtr Res, bool bSuccess);
void OnLeaderboardReceived(
FHttpRequestPtr Req, FHttpResponsePtr Res, bool bSuccess);
};Step 3 — Implement in .cpp
Implement SubmitScore, GetLeaderboard, and their response callbacks.
// LeaderboardComponent.cpp
#include "LeaderboardComponent.h"
const FString ULeaderboardComponent::ApiUrl =
TEXT("https://yourapp.com/api/v1/games/my_game");
const FString ULeaderboardComponent::ApiKey =
TEXT("YOUR_API_KEY");
void ULeaderboardComponent::SubmitScore(
int32 Score, FString PlayerId, FString PlayerName)
{
auto Req = FHttpModule::Get().CreateRequest();
Req->SetURL(ApiUrl + TEXT("/scores"));
Req->SetVerb(TEXT("POST"));
Req->SetHeader(TEXT("Authorization"), TEXT("Bearer ") + ApiKey);
Req->SetHeader(TEXT("Content-Type"), TEXT("application/json"));
FString Body = FString::Printf(TEXT(
"{\"score\":%d,\"playerRefId\":\"%s\","
"\"playerName\":\"%s\",\"submissionId\":\"%s\"}"),
Score, *PlayerId, *PlayerName,
*FGuid::NewGuid().ToString());
Req->SetContentAsString(Body);
Req->OnProcessRequestComplete().BindUObject(
this, &ULeaderboardComponent::OnScoreSubmitted);
Req->ProcessRequest();
}
void ULeaderboardComponent::OnScoreSubmitted(
FHttpRequestPtr, FHttpResponsePtr Res, bool bSuccess)
{
if (bSuccess && Res->GetResponseCode() == 200)
UE_LOG(LogTemp, Log,
TEXT("Score submitted: %s"), *Res->GetContentAsString());
}
void ULeaderboardComponent::GetLeaderboard(int32 Limit)
{
auto Req = FHttpModule::Get().CreateRequest();
Req->SetURL(ApiUrl +
FString::Printf(TEXT("/leaderboard?limit=%d"), Limit));
Req->SetVerb(TEXT("GET"));
Req->SetHeader(TEXT("Authorization"), TEXT("Bearer ") + ApiKey);
Req->OnProcessRequestComplete().BindUObject(
this, &ULeaderboardComponent::OnLeaderboardReceived);
Req->ProcessRequest();
}
void ULeaderboardComponent::OnLeaderboardReceived(
FHttpRequestPtr, FHttpResponsePtr Res, bool bSuccess)
{
if (bSuccess && Res->GetResponseCode() == 200)
UE_LOG(LogTemp, Log,
TEXT("Leaderboard: %s"), *Res->GetContentAsString());
// parse JSON with TJsonReader or a plugin like VaRest
}Tips
- To parse the JSON response, use UE5's built-in
TJsonReader/FJsonObject, or add the VaRest plugin for a Blueprint-friendly approach. - Store
ApiKeyin a config file (DefaultGame.ini) and load it withGConfig->GetString()rather than hardcoding in source. - The
FGuid::NewGuid().ToString()call produces a uniquesubmissionIdper request — safe to retry on timeout. - Attach the component in Blueprint's Details panel or via
CreateDefaultSubobjectin your GameMode constructor.