Game ideas & features

Your next feature

Gift Codes

Let players enter and redeem gift codes.

Requires a player session
Unity
Illustration of Gift Codes
Your visible result

A test code is redeemed. Repeat redemption follows your rules.

Create a gift code1:42
Open video separately ↗
Video coming later

This step does not have a video yet. The guide and MCP request are ready to use.

Server player values

Prepare the same project

  1. Choose the same project in the dashboard as in your game configuration.

    Gift Codes →
  2. Create or reuse these test entries in the dashboard form. These are example values, not current project data.

    Code
    WELCOME
    Gift Data (JSON)
    {}
    Max Per User
    1

    Up to eight configured values are credited by the server when redeemed. giftData remains a separate game payload. Display the returned balance; do not add the reward again locally.

    Player values →
  3. Save, reload and verify the entries before running the example. Preserve unrelated definitions.

Build this feature

You can preview the guide. Running it requires an active project key. Prepare project

SDK installed, project configuration imported and player signed in.

Up to eight configured values are credited by the server when redeemed. giftData remains a separate game payload. Display the returned balance; do not add the reward again locally. This SDK example uses develop. Check whether the required APIs exist in your installed SDK.

Save the C# file, attach it to a GameObject and enter Play. Check Console.

C#
Assets/Scripts/GiftCodesDemo.cs
horizOn callSetup
1using UnityEngine;
2using PM.horizOn.Cloud.Core;
3using PM.horizOn.Cloud.Manager;
4​
5// Attach this script to a GameObject in your first scene.
6public class GiftCodesDemo : MonoBehaviour
7{
8 async void Start()
9 {
10 // Setup: initialize, connect and sign in a player.
11 HorizonApp.Initialize();
12 await new HorizonServer().Connect();
13 if (!UserManager.Instance.IsSignedIn)
14 {
15 bool authenticated = UserManager.Instance.HasCachedAnonymousToken()
16 ? await UserManager.Instance.RestoreAnonymousSession()
17 : await UserManager.Instance.SignUpAnonymous("StatisticsPlayer");
18 if (!authenticated) return;
19 }
20​
21 // horizOn: validate a gift code and redeem it.
22 // Run from one explicit player action; serialize this handler until it completes.
23 string userId = UserManager.Instance.CurrentUser.UserId;
24 string project = HorizonApp.Network.GetApiKey();
25 string scope = PM.horizOn.Cloud.Objects.Network.Responses.GiftCodeIntent.ProjectFingerprint(project);
26 string pendingKey = "horizOn_gift_v2_" + scope + "_" + userId;
27 // Preserve an old V1 demo record for reconciliation. Never reinterpret its UUID as a count.
28 if (PlayerPrefs.HasKey("horizOn_gift_pending_" + userId)) return;
29 PM.horizOn.Cloud.Objects.Network.Responses.GiftCodeIntent pending;
30 if (PlayerPrefs.HasKey(pendingKey)) {
31 pending = JsonUtility.FromJson<PM.horizOn.Cloud.Objects.Network.Responses.GiftCodeIntent>(PlayerPrefs.GetString(pendingKey));
32 if (pending == null || !pending.IsValidFor(project, userId)) return;
33 } else {
34 pending = await GiftCodeManager.Instance.PrepareRedemption("WELCOME");
35 if (pending == null || !pending.IsValidFor(project, userId) || PlayerPrefs.HasKey(pendingKey)) return;
36 PlayerPrefs.SetString(pendingKey, JsonUtility.ToJson(pending));
37 PlayerPrefs.Save(); // All seven fields before the first mutating request.
38 }
39 string original = JsonUtility.ToJson(pending);
40 if (PlayerPrefs.GetString(pendingKey) != original) return;
41 var result = await GiftCodeManager.Instance.RedeemPrepared(pending);
42 if (result == null || !result.success) return; // Keep the exact tuple after any uncertain result.
43 if (PlayerPrefs.GetString(pendingKey) == original) {
44 PlayerPrefs.DeleteKey(pendingKey);
45 PlayerPrefs.Save();
46 }
47 if (result.playerValues != null) {
48 foreach (var value in result.playerValues.values) Debug.Log(value.key + "=" + value.balance);
49 }
50 // alreadyApplied/replayed means the sequence slot was consumed, with no copied historical rewards.
51 // Replace balances with playerValues. Never add grantedValues locally.
52 }
53}
Open the complete SDK sample ↗

Test your new feature.

A test code is redeemed. Repeat redemption follows your rules.

Choose the same project in the dashboard as in your game configuration.

Endpoints
  • POST/gift-codes/validate
  • POST/gift-codes/redeem
All endpoints in the API reference
Illustration of Leaderboards
Our suggestion for your next step

Give every round a high score.

Save the score after a round and show who's ahead. Keep using your connection and player session.