ゲームのアイデアと機能

次に追加する機能

ギフトコード

プレイヤーがギフトコードを入力して引き換えられるようにする。

プレイヤーセッションが必要
Godot
ギフトコードのイラスト
確認できる結果

テストコードが引き換えられます。繰り返しの引き換えには設定したルールが適用されます。

ギフトコードを作成する1:42

この動画は英語で再生されます。

動画を別のタブで開く ↗
動画は後日公開

この手順の動画はまだありません。ガイドとMCPへの依頼は利用できます。

サーバー上のプレイヤー値

同じプロジェクトを準備する

  1. ゲームの設定と同じプロジェクトをダッシュボードで選択してください。

    ギフトコード →
  2. ダッシュボードのフォームで次のテスト項目を作成するか、既存の項目を使います。表示値は例であり、現在のプロジェクトのデータではありません。

    コード
    WELCOME
    ギフトデータ(JSON)
    {}
    ユーザーあたりの最大数
    1

    引き換え時にサーバーが最大8種類の設定済み値を付与します。giftDataは別のゲームデータです。返された残高を表示し、ローカルで報酬を再加算しないでください。

    プレイヤー数値 →
  3. 実行前に保存、再読み込み、確認を行います。他の定義は保持してください。

この機能を実装する

ガイドは閲覧できます。実行には有効なプロジェクトキーが必要です。 プロジェクトを準備する

SDK をインストールし、プロジェクト設定をインポート済みで、プレイヤーがログインしていること。

引き換え時にサーバーが最大8種類の設定済み値を付与します。giftDataは別のゲームデータです。返された残高を表示し、ローカルで報酬を再加算しないでください。 この SDK サンプルは develop を使用します。必要な API がインストール済みの SDK にあるか確認してください。

GDScriptを保存し、メインシーンのノードにアタッチして実行します。Outputを確認してください。

GDScript
res://scripts/gift_codes.gd
horizOn 呼び出し準備
1# このスクリプトをメインシーンのノードにアタッチします。
2extends Node
3​
4​
5func _ready() -> void:
6 # 準備: 初期化、接続、プレイヤーのサインイン。
7 var connected = await Horizon.connect_to_server()
8 if not connected:
9 print("Failed to connect to horizOn server")
10 return
11 if not Horizon.auth.isSignedIn():
12 var signed_in: bool
13 if Horizon.auth.hasCachedAnonymousToken():
14 signed_in = await Horizon.auth.restoreAnonymousSession()
15 else:
16 signed_in = await Horizon.auth.signUpAnonymous("StatisticsPlayer")
17 if not signed_in:
18 return
19​
20 # horizOn: ギフトコードを確認して引き換える。
21 # Run from one explicit player action; serialize this handler until it completes.
22 var user_id: String = Horizon.auth.getCurrentUser().userId
23 var project: String = Horizon.getConfig().api_key
24 var scope: String = project + "|" + user_id
25 var path: String = "user://gift_pending_" + scope.sha256_text() + ".bin"
26 var pending: Dictionary
27 if FileAccess.file_exists(path):
28 var file := FileAccess.open(path, FileAccess.READ)
29 if file == null: return
30 var saved: Variant = file.get_var(false)
31 file.close()
32 if not HorizonGiftCodes.validIntent(saved, project, user_id): return
33 pending = saved # Invalid/V1 records stay untouched for reconciliation.
34 else:
35 pending = await Horizon.giftCodes.prepareRedemption("WELCOME")
36 if not HorizonGiftCodes.validIntent(pending, project, user_id) or FileAccess.file_exists(path): return
37 var file := FileAccess.open(path, FileAccess.WRITE)
38 if file == null: return
39 file.store_var(pending, false)
40 file.flush()
41 var saved_ok: bool = file.get_error() == OK
42 file.close()
43 if not saved_ok: return
44 var verify := FileAccess.open(path, FileAccess.READ)
45 if verify == null: return
46 var original: Variant = verify.get_var(false)
47 verify.close()
48 if original != pending: return
49 var result: Dictionary = await Horizon.giftCodes.redeemPrepared(pending)
50 if not result.get("success", false): return # Keep all seven original fields for retry.
51 verify = FileAccess.open(path, FileAccess.READ)
52 if verify == null: return
53 original = verify.get_var(false)
54 verify.close()
55 if original == pending: DirAccess.remove_absolute(ProjectSettings.globalize_path(path))
56 if result.get("playerValues") is Dictionary:
57 print(result.playerValues.values) # Current balances, never add grantedValues locally.
58 # Replay has alreadyApplied=true, giftData=null and empty grantedValues/grantedUnlocks.
完全なSDKサンプルを開く ↗

新しい機能をテストしましょう。

テストコードが引き換えられます。繰り返しの引き換えには設定したルールが適用されます。

ゲームの設定と同じプロジェクトをダッシュボードで選択してください。

エンドポイント
  • POST/gift-codes/validate
  • POST/gift-codes/redeem
API リファレンスのすべてのエンドポイント
リーダーボードのイラスト
次のステップへのおすすめ

毎ラウンドにハイスコアを。

ラウンド終了後にスコアを保存し、上位のプレイヤーを表示します。現在の接続とプレイヤーセッションを引き続き使用します。