This guide describes how to set up a native C or C++ game project to use the Play Games Services v2 Native SDK and integrate the Sign-in service. Sign-in integration is required in order to integrate other Play Games Services features into your game and to integrate Play Games Services into your backend game server.
Supported features
The Play Games Services v2 Native SDK is in beta and only supports the Sign-in service. It doesn't yet support other Play Games Services features.
API reference documentation
The header files for the SDK contain reference documentation for the APIs. The
header files are located in the include
folder in the SDK files, which are
available after you sync your project with SDK repository.
Requirements
A game project that uses native C or C++ as the primary programming language.
Your game project and development environment must have the Gradle build system set up.
Before you start
You must set up Play Games Services in Google Play Console.
Set up your game project
Complete the following steps to set up your game project.
Update CMakeLists.txt
In your CMakeLists.txt
file, add the following code:
find_package(com.google.android.gms.games.v2.c REQUIRED CONFIG)
// link games_static for -DANDROID_STL=c++_static or default
// link games_shared for -DANDROID_STL=c++_shared
target_link_libraries(
app PUBLIC com.google.android.gms.games.v2.c::games_static)
Update build.gradle
In your app-level build.gradle
file do the following:
Make sure the prefab build feature is enabled.
Add the dependency for the Play Games Services v2 Native SDK:
com.google.android.gms:play-services-games-v2-native-c:17.0.0-beta1
Here's an example:
android {
...
buildFeatures {
prefab true
}
...
}
dependencies {
...
implementation "com.google.android.gms:play-services-games-v2-native-c:17.0.0-beta1"
}
Update AndroidManifest.xml
In your
AndroidManifest.xml
file, define your Play Games Services project ID. You can do so by adding the following lines to the file:<manifest> <application> <meta-data android:name="com.google.android.gms.games.APP_ID" android:value="@string/game_services_project_id"/> </application> </manifest>
Create a string resource for your project ID. This allows your game to access the ID at build time. To create the resource, create the file
project_root/app/src/main/res/values/games-ids.xml
, and add the following:<?xml version="1.0" encoding="utf-8"?> <resources> <string name="game_services_project_id" translatable="false">add your Project ID here</string> </resources>
Build and test your game. If successful, when you launch your game it displays a sign-in prompt or a successful sign-in banner.
Get the player ID
Your game can access player information for a signed in player by retrieving
their player ID. You can retrieve a player ID by calling the GetPlayerID
function, which is demonstrated in the following example.
#include <assert.h>
#include "gni/gni.h"
#include "gni/gni_task.h"
#include "pgs/pgs_play_games.h"
#include "pgs/pgs_players_client.h"
// A callback for a GniTask returned from PgsPlayersClient_getCurrentPlayerId.
void OnGetCurrentPlayerIdCompleteCallback(GniTask *task, void *user_data) {
if (!GniTask_isSuccessful(task)) {
const char *error_message = nullptr;
GniTask_getErrorMessage(task, &error_message);
// Log error message here.
GniString_destroy(error_message);
GniTask_destroy(task);
return;
}
const char *result = nullptr;
PgsPlayersClient_getCurrentPlayerId_getResult(task, &result);
// Log player id here.
GniString_destroy(result);
GniTask_destroy(task);
}
// Gets the player ID.
void GetPlayerId(jobject main_activity) {
static const PgsPlayersClient *players_client =
PgsPlayGames_getPlayersClient(main_activity);
GniTask *get_current_player_id_task =
PgsPlayersClient_getCurrentPlayerId(players_client);
assert(get_current_player_id_task != nullptr);
GniTask_addOnCompleteCallback(get_current_player_id_task,
OnGetCurrentPlayerIdCompleteCallback,
nullptr);
}
// Entry point for our test app
void TestPGSNative(JNIEnv *env, jobject main_activity) {
JavaVM *java_vm;
env->GetJavaVM(&java_vm);
GniCore_init(java_vm, main_activity);
GetPlayerId(main_activity);
}
Re-launch the sign-in prompt
If a player declines the initial Play Games Services sign-in prompt that is
automatically displayed when your game launches, they may change their mind
during the game session. You can re-launch the sign-in prompt by calling
PgsGamesSignInClient_signIn
as long as no players are currently signed in.
Game server authorization
Once a player successfully signs into to Play Games Services, your game client
can request a server authorization code that your backend game server can use to
securely communicate with Play Games Services. This allows your game server to
retrieve, update, and store data for the signed in player. You can retrieve the
server authorization code by calling the
PgsGamesSignInClient_requestServerSideAccess
function.
For more information, see the server access guide.