C++ 用 Play Games サービス
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
Google ログイン API のサポート終了に伴い、games v1 SDK は 2026 年に削除されます。2025 年 2 月以降、games v1 SDK を新しく統合したタイトルを Google Play で公開することはできなくなります。代わりに、games v2 SDK を使用することをおすすめします。
以前のゲーム v1 と統合されている既存のタイトルは数年間は引き続き機能しますが、2025 年 6 月以降は v2 に移行することをおすすめします。
このガイドは、Play Games サービス v1 SDK の使用を対象としています。Play Games サービス v2 の C++ SDK はまだご利用いただけません。
Google Play ゲームサービス C++ SDK は、Google Play ゲームサービスで使用する C++ API を提供しており、ゲームの C++ 版をすでに実装済みのデベロッパー向けです。
この SDK は現時点では、以下のサービスを提供しています。
- 認証
- 実績
- リーダーボード
- イベント
- 保存済みゲーム
- Nearby Connections(Android のみ)
- プレーヤーの統計情報
コンセプト
概略としては、以下の手順で SDK を使用します。
- Android 向けにプラットフォーム構成をセットアップします。
GameServices::Builder
を使用して GameServices
オブジェクトを構成して作成します。GameServices
オブジェクトは自動的にログインを試行し、その結果を OnAuthActionFinished()
コールバックを通じて返します。コールバックで返された結果をメモしておきます。自動ログインの試行が失敗した場合、ログインできるボタンをユーザーに表示することができます。
OnAuthActionFinished()
という結果を受け取ると、GameServices
オブジェクトとその子の Manager を使って以下のような Play ゲームサービスを呼び出すことができます。
- ログイン(認証失敗後):
StartAuthorizationUI()
- 実績のロック解除:
Achievements().Unlock()
- 組み込みの UI を使った実績の表示:
Achievements().ShowAllUI()
- ハイスコアの送信:
Leaderboards().SubmitScore()
- ログアウト:
SignOut()
GameServices
オブジェクトの使用が完了したら、リセットするか破棄します。
詳細な手順:
プラットフォーム構成の初期化: プラットフォーム固有の初期化情報を含むオブジェクトです。Android では、プラットフォーム構成には Java VM や現在の Activity
へのポインタが含まれます。
// In android_main(), create a platform configuration
// and bind the object activity.
// Alternately, attach the activity in JNI_Onload().
gpg::AndroidPlatformConfiguration platform_configuration;
platform_configuration.SetActivity(state->activity->clazz);
GameServices
オブジェクトの構築: このオブジェクトは Google Play ゲームサービス機能のメインのエントリ ポイントとなります。GameServices
インスタンスは GameServices::Builder
で作成されます。
ほとんどの実装では、特定の GameServices
オブジェクトはお使いの C 環境が存続する限り保持されます。Android Activity
を一時停止や再開するときに再度初期化する必要はありません。
// Creates a GameServices object that has lambda callbacks.
game_services_ = gpg::GameServices::Builder()
.SetDefaultOnLog(gpg::LogLevel::VERBOSE)
.SetOnAuthActionStarted([started_callback](gpg::AuthOperation op) {
is_auth_in_progress_ = true;
started_callback(op);
})
.SetOnAuthActionFinished([finished_callback](gpg::AuthOperation op,
gpg::AuthStatus status) {
LOGI("Sign in finished with a result of %d", status);
is_auth_in_progress_ = false;
finished_callback(op, status);
})
.Create(pc);
Manager のクラスを使って GameServices
オブジェクトを管理します。複数の Manager に、GameServices
インスタンスやグループ関連の機能から同時にアクセスできます。例として、実績やリーダーボードの Manager が挙げられます。Manager の状態がユーザーに表示されることはありません。Manager は参照渡しで返され、それを含む GameServices
インスタンスがそのライフサイクルを制御します。クライアントは、Manager の参照を直接保持せずに、代わりに、クライアントは GameServices
インスタンスを保持する必要があります。
Manager は、変更不能な値型のオブジェクトを介してデータを返します。クエリが行われた場合、その時点におけるデータに基づく一貫したビューがこれらの値に反映されます。
// Submit a high score
game_services_->Leaderboards().SubmitScore(leaderboard_id, score);
// Show the default Achievements UI
game_services_->Achievements().ShowAllUI();
GameServices
オブジェクトの使用を終えたら、オブジェクトを所有する unique_ptr
に対して reset()
を呼び出すか、スコープ外になった unique_ptr
が自動的に破棄されることで、クリーンアップします。
スレッドモデル
特に記載のない限り、GameServices
と Manager のすべてのメソッドは、スレッドセーフで、非同期に実装されます。外部ロックを行わず、どのスレッドでも呼び出すことが可能で、呼び出された順に実行されます。
状態を読み取る Accessor メソッドには、主に 2 種類のバリアントがあります。その 1 つは、FetchProperty()
のような名前のメソッドで、コールバックに対して非同期に結果を返します。もう 1 つは FetchPropertyBlocking()
のような名前のメソッドで、同期して呼び出し元のスレッドに結果を返します。
// Blocking callback
gpg::AchievementManager::FetchAllResponse fetchResponse =
game_services_->Achievements().FetchAllBlocking(std::chrono::milliseconds(1000));
// Non-blocking callback
game_services_->Achievements().FetchAll(gpg::DataSource::CACHE_OR_NETWORK,
[] (gpg::AchievementManager::FetchAllResponse response) {
LogI("Achievement response status: %d", response.status);});
ユーザーのコールバックはすべて、専用のコールバック スレッドで呼び出されます。このスレッドは、「メインスレッド」または「UI スレッド」のようなプラットフォームのコンセプトとは異なることがあります。また、ユーザーのコールバックはすみやかに実行を図る必要があります。処理の滞ったコールバック スレッドは、ユーザーが感知するような問題(たとえば、ログアウト リクエストの完了の遅延)を引き起こすことがあります。
Android で Play ゲーム C++ SDK を使い始めるには、引き続きクイックスタート ガイドをご覧ください。
参考資料
詳細については、Google Play ゲームサービス C++ SDK に付属のクラスに関するドキュメントをご覧ください。また、サンプルページで SDK の使用方法の例をご確認ください。
ゲームでバックエンド サーバーを使用している場合は、Google Play Games サービスへのサーバー側アクセスの有効化をご覧ください。
このページのコンテンツやコードサンプルは、コンテンツ ライセンスに記載のライセンスに従います。Java および OpenJDK は Oracle および関連会社の商標または登録商標です。
最終更新日 2025-07-26 UTC。
[null,null,["最終更新日 2025-07-26 UTC。"],[],[],null,["# Play Games Services for C++\n\nFollowing the deprecation of the\n[Google Sign-In](https://android-developers.googleblog.com/2024/09/streamlining-android-authentication-credential-manager-replaces-legacy-apis.html)\nAPI, we are removing the games v1 SDK in 2026. After February 2025, you will be unable to publish\ntitles that are newly integrated with games v1 SDK, on Google Play. We recommend that you use the\ngames v2 SDK instead. \n\nWhile existing titles with the previous games v1 integrations continue to function for a\ncouple of years, you are encouraged to\n[migrate to v2](/games/pgs/android/migrate-to-v2)\nstarting June 2025. \n\nThis guide is for using the Play Games Services v1 SDK. The C++ SDK for\nPlay Games Services v2 is not yet available.\n\nThe Google Play games Services C++ SDK provides a C++ API for use with Google Play Game\nservices, and is meant for developers who have an existing C++ implementation\nof their game.\n\nCurrently, the SDK implements the following services:\n\n- Authorization\n- Achievements\n- Leaderboards\n- Events\n- Saved Games\n- Nearby Connections (Android only)\n- Player Statistics\n\nConcepts\n--------\n\nAt a high level, you use the SDK by following these steps:\n\n1. Set up a platform configuration for Android.\n2. Use a `GameServices::Builder` to configure and construct a `GameServices` object. The `GameServices` object automatically attempts to sign in, and returns the result via an `OnAuthActionFinished()` callback. Take note of the result returned by the callback. If the automatic sign-in attempt failed, you can display a button to let users sign in.\n3. After receiving the `OnAuthActionFinished()` result, you can use the\n `GameServices` object and its child Managers to make Play Games services calls,\n including:\n\n - Sign in (after authorization fails): `StartAuthorizationUI()`\n - Unlock achievements: `Achievements().Unlock()`\n - Show achievements using built-in UI: `Achievements().ShowAllUI()`\n - Submit a high score: `Leaderboards().SubmitScore()`\n - Sign out: `SignOut()`\n4. When you are done using the `GameServices` object, reset or destroy it.\n\nAt a more detailed level:\n\n1. Initialize a platform configuration: This is an object that contains\n platform-specific initialization information. On Android, the platform configuration contains the\n Java VM and a pointer to the current `Activity`:\n\n // In android_main(), create a platform configuration\n // and bind the object activity.\n // Alternately, attach the activity in JNI_Onload().\n gpg::AndroidPlatformConfiguration platform_configuration;\n platform_configuration.SetActivity(state-\u003eactivity-\u003eclazz);\n\n2. Construct a `GameServices` object: This object is the main entry point for\n Google Play games Services functionality. `GameServices` instances are created\n with `GameServices::Builder`.\n\n In most implementations, a given `GameServices` object will persist as long as\n your C environment does; you do not need to reinitialize it when your\n Android `Activity` pauses and resumes. \n\n // Creates a GameServices object that has lambda callbacks.\n game_services_ = gpg::GameServices::Builder()\n .SetDefaultOnLog(gpg::LogLevel::VERBOSE)\n .SetOnAuthActionStarted([started_callback](gpg::AuthOperation op) {\n is_auth_in_progress_ = true;\n started_callback(op);\n })\n .SetOnAuthActionFinished([finished_callback](gpg::AuthOperation op,\n gpg::AuthStatus status) {\n LOGI(\"Sign in finished with a result of %d\", status);\n is_auth_in_progress_ = false;\n finished_callback(op, status);\n })\n .Create(pc);\n\n3. Use the Manager classes to manage your `GameServices` object. Managers are accessed from a `GameServices` instance and group related functionality\n together. Examples of these\n include the Achievement and Leaderboard Managers. They contain no user-visible\n state themselves. Managers are returned by reference, and the containing\n `GameServices` instance controls their lifecycle. Your client should never hold\n onto a Manager reference. Instead, your client should hold on to the\n `GameServices` instance.\n\n Managers return data via immutable value type objects. These values\n reflect a consistent view of the underlying data at the point in time when\n the query was made. \n\n // Submit a high score\n game_services_-\u003eLeaderboards().SubmitScore(leaderboard_id, score);\n\n // Show the default Achievements UI\n game_services_-\u003eAchievements().ShowAllUI();\n\n4. When you are finished using the `GameServices` object, clean up by\n calling `reset()` on the `unique_ptr` that owns it, or by letting the\n `unique_ptr` automatically destroy it when going out of scope.\n\nThreading model\n---------------\n\nUnless otherwise noted, all `GameServices` and Manager methods have\nthread-safe, asynchronous implementations. They can be called on any thread without\nexternal locking, and will execute in an order consistent with their invocation\norder.\n\nAccessor methods (those that read state) come in two major variants. The first\ntype of method (with names like `FetchProperty()`) asynchronously supplies its results\nto a provided callback; the second (with names like\n`FetchPropertyBlocking()`) synchronously returns its results to the calling\nthread. \n\n // Blocking callback\n gpg::AchievementManager::FetchAllResponse fetchResponse =\n game_services_-\u003eAchievements().FetchAllBlocking(std::chrono::milliseconds(1000));\n\n // Non-blocking callback\n game_services_-\u003eAchievements().FetchAll(gpg::DataSource::CACHE_OR_NETWORK,\n [] (gpg::AchievementManager::FetchAllResponse response) {\n LogI(\"Achievement response status: %d\", response.status);});\n\nAll user callbacks are invoked on a dedicated callback thread. This thread is\npotentially distinct from any platform concept of a \"main thread\" or \"UI\nthread\". You should also try to ensure that user callbacks execute quickly; a stalled callback thread\nmay cause user-visible issues (for example, delayed completion of a sign-out\nrequest).\n\nPlatform-specific information\n-----------------------------\n\nTo get started using the Play Games C++ SDK on Android, continue to the\n[quickstart guide](/games/pgs/v1/cpp/quickstart).\n\nFurther reading\n---------------\n\nBe sure to read the class documentation that comes in the Google Play Game\nservices C++ SDK for further details, and check out the\n[samples](https://github.com/playgameservices/) that demonstrate how to use the SDK.\n\nIf your game uses a backend server, see\n[Enabling Server-Side Access to Google Play Games Services](/games/pgs/v1/android/server-access)."]]