text
stringlengths
1
372
leaderboard, for example, is published this way, it cannot be
unpublished.
go to play games services → setup and management →
configuration → credentials.
find the get resources button.
it returns an XML file with the play games services IDs.
add a file at android/app/src/main/res/values/games-ids.xml
containing the XML you received in the previous step.
<topic_end>
<topic_start>
2. sign in to the game service
now that you have set up game center and play games services, and
have your achievement & leaderboard IDs ready, it’s finally dart time.
add a dependency on the games_services package.
before you can do anything else, you have to sign the player into
the game service.
<code_start>
try {
await GamesServices.signIn();
} on PlatformException catch (e) {
// ... deal with failures ...
}
<code_end>
the sign in happens in the background. it takes several seconds, so
don’t call signIn() before runApp() or the players will be forced to
stare at a blank screen every time they start your game.
the API calls to the games_services API can fail for a multitude of
reasons. therefore, every call should be wrapped in a try-catch block as
in the previous example. the rest of this recipe omits exception
handling for clarity.
lightbulb tip
it’s a good practice to create a controller. this would be a
ChangeNotifier, a bloc, or some other piece of logic that wraps around
the raw functionality of the games_services plugin.
<topic_end>
<topic_start>
3. unlock achievements
register achievements in google play console and app store connect,
and take note of their IDs. now you can award any of those
achievements from your dart code:
<code_start>
await GamesServices.unlock(
achievement: achievement(
androidID: 'your android id',
iOSID: 'your ios id',
),
);
<code_end>
the player’s account on google play games or apple game center now
lists the achievement.
to display the achievements UI from your game, call the
games_services API:
<code_start>
await GamesServices.showAchievements();
<code_end>
this displays the platform achievements UI as an overlay on your game.
to display the achievements in your own UI, use
GamesServices.loadAchievements().
<topic_end>
<topic_start>
4. submit scores
when the player finishes a play-through, your game can submit the result
of that play session into one or more leaderboards.
for example, a platformer game like super mario can submit both the
final score and the time taken to complete the level, to two separate
leaderboards.
in the first step, you registered a leaderboard in google play
console and app store connect, and took note of its ID. using this
ID, you can submit new scores for the player:
<code_start>
await GamesServices.submitScore(
score: score(
iOSLeaderboardID: 'some_id_from_app_store',
androidLeaderboardID: 'some_id_from_gplay',
value: 100,
),
);
<code_end>
you don’t need to check whether the new score is the player’s
highest. the platform game services handle that for you.
to display the leaderboard as an overlay over your game, make the
following call:
<code_start>
await GamesServices.showLeaderboards(
iOSLeaderboardID: 'some_id_from_app_store',
androidLeaderboardID: 'some_id_from_gplay',
);
<code_end>
if you want to display the leaderboard scores in your own UI, you
can fetch them with GamesServices.loadLeaderboardScores().
<topic_end>
<topic_start>
5. next steps
there’s more to the games_services plugin. with this plugin, you can:
some achievements can be incremental. for example: “you have collected
all 10 pieces of the McGuffin.”
each game has different needs from game services.
to start, you might want to create this controller
in order to keep all achievements & leaderboards logic in one place:
<code_start>