ビッグバナー

このガイドでは、Play Games サービス c++ SDK のリーダーボード API を使用して、視覚的なリーダーボードを作成する方法、プレーヤーのスコアを記録する方法、以前のゲーム セッションのスコアと比較する方法について説明します。この API は LeaderboardsClient にあります。

始める前に

リーダーボード ゲームのコンセプトをまだ確認されていない場合は、確認することをおすすめします。

リーダーボード API を使用してコーディングを開始する前に:

リーダーボード クライアントを取得する

リーダーボード API の使用を開始するには、まずゲームで LeaderboardsClient オブジェクトを取得する必要があります。そのためには、PgsLeaderboardsClient_create() メソッドを呼び出してアクティビティを渡します。

プレーヤーのスコアを更新する

プレーヤーのスコアが変化した場合(プレーヤーがゲームを終了した場合など)、ゲームでは PgsLeaderboardsClient_submitScoreImmediate を呼び出して、リーダーボードのスコアを更新できます。リーダーボード ID、未加工のスコア値、オプションのスコアタグ、コールバック関数を渡す必要があります。

// Callback function to handle the result of submitting the score
void OnScoreSubmitted(PgsStatusCode status_code,
                      PgsScoreSubmissionData* score_submission_data,
                      void* user_data) {
  if (status_code == PGS_STATUS_SUCCESS) {
    // Score submitted successfully
    // You can inspect score_submission_data for details
    // Remember to release the data when done:
    PgsScoreSubmissionData_Release(score_submission_data);
  } else {
    // Handle error
  }
}

// Function to submit the score
void SubmitScore(PgsLeaderboardsClient* client, const char* leaderboard_id, int64_t score) {
  const char* score_tag = NULL; // Optional tag

  PgsLeaderboardsClient_submitScoreImmediate(
      client,
      leaderboard_id,
      score,
      score_tag,
      OnScoreSubmitted,
      NULL // user_data - optional context pointer
  );
}

// Example usage:
// Assuming 'my_leaderboard_id' is defined elsewhere, e.g., fetched from resources
// SubmitScore(leaderboards_client, my_leaderboard_id, 1337);

リーダーボード ID は、c++ コード内の定数またはリソースとして管理することをおすすめします。

リーダーボードを表示する

特定のリーダーボードのデフォルトのリーダーボード ユーザー インターフェースを表示するには、PgsLeaderboardsClient_showLeaderboardUI を呼び出します。この関数には、クライアント ハンドル、アクティビティ、リーダーボード ID、期間とコレクション、コールバックが必要です。

// Callback function to handle the result of showing the UI
void OnShowLeaderboardUI(PgsStatusCode status_code, bool success, void* user_data) {
  if (status_code == PGS_STATUS_SUCCESS && success) {
    // UI was shown successfully
  } else {
    // Handle error or failure to show UI
  }
}

// Function to show a specific leaderboard UI
void ShowLeaderboard(PgsLeaderboardsClient* client, jobject activity, const char* leaderboard_id) {
  PgsLeaderboardsClient_showLeaderboardUI(
      client,
      activity,
      leaderboard_id,
      PGS_LEADERBOARD_TIME_SPAN_ALL_TIME, // Or PGS_LEADERBOARD_TIME_SPAN_DAILY, PGS_LEADERBOARD_TIME_SPAN_WEEKLY
      PGS_LEADERBOARD_COLLECTION_PUBLIC,  // Or PGS_LEADERBOARD_COLLECTION_FRIENDS
      OnShowLeaderboardUI,
      NULL // user_data - optional context pointer
  );
}

// Example usage:
// ShowLeaderboard(leaderboards_client, android_activity, my_leaderboard_id);

この関数は UI を表示します。activity オブジェクトは、UI を表示するためのコンテキストを提供します。