play_games

Use Google Play Games Services on your Flutter app; this allows for signin and achievements so far, but more additions are very welcome. If you like it, give us a star and feel free to help and contribute.

Part 1: Sign In

Call the sign in method to sign in; you must check if the login was successufully.

    SigninResult result = await PlayGames.signIn();
    if (result.success) {
        await PlayGames.setPopupOptions();
        this.account = result.account;
    } else {
        this.error = result.message;
    }
    this.loading = false;

But don't think it will be that easy! Actually, GPGS signin is quite a hassle to do, but this tutorial should help you out with this nefarious task.

Reasons for failure will be specified in the error property. Otherwhise, you can access the account property to get the account.

Part 2: Achievements

You can both award achievements (unlock/increment) and show the Achievements screen in your game.

In order to unlock or increment an achievement, use the provided APIs:

  PlayGames.unlockAchievementByName(name);

This is async and you wait for the return if desired. The name is the name in your games-ids.xml file, you can also call the by id method.

Also, there is a method to display a popup with your achievements:

    PlayGames.showAchievements();

Again, this is async and returns only when the player closes the popup. So maybe you want to pause your game until that happens.

Part 3: Saved Games

Firstly, you must enable Saved Games. Go to your Google Play Console > Game Services > Select your game > Game details and toggle Saved Games option on. Then Save and publish. Beware, this cannot be later turned off.

Example of Saved Games

Secondly, in order to use the Saved Games feature, you must required explicit permission upon logging in. Thankfully, the signIn method can be configured to ask for that permission via the scopeSnapshot option, like so:

    SigninResult result = await PlayGames.signIn(scopeSnapshot: true); // allow to load/save games later

By default that option is false, so be sure to check it in. In Saved Games, a snapshot is of one save slot, that is, one representation of the game state that can be reloaded later. It is comprised of content, a blob, and metadata, a structured key-value pairs set.

You can use the methods openSnapshot and saveSnaphost to load and save game data under a given snapshotName. If your game has several slots, use several differente names for then. Otherwise, use only a single snapshotName for everything. A few points to ponder:

  • You put your save data in the content field, which is a String. You can put anything there (json, custom format, etc).
  • Metadata are also retrieved but for now only the description can be set. These metadata are useful if you are using the default save game screen provided (not support by this lib). Otherwise you can get some information from it.
  • Whenever you save a snapshot, if you intend to save again, you must reopen. You can either keep it open util the player saves the game, then save and reopen, or keep it closed, when the player saves, you open and save. Whenever you save, it is closed.

Here follows an example of the usage of the APIs from the crystap sample open-source game used to showcase this plugin:

    Future<int> fetchStartAmount() async {
        Snapshot save = await PlayGames.openSnapshot('crystap.main'); // load the existing save or create a new empty one if none exists
        if (save.content == null || save.content.trim().isEmpty) {
            return 0; // default value when there is no save
        }
        return int.parse(save.content);
    }

    Future<bool> saveAmount(int amount) async {
        bool result = await PlayGames.saveSnapshot('crystap.main', amount.toString()); // save the current state to the snapshot
        await PlayGames.openSnapshot('crystap.main'); // reopen snapshot after save
        return result;
    }

Here our data is just a single integer, that we convert to a String in order to save it within the content. Again, your save can be an arbitrarily complex JSON or anything like that. See the full Crystap game for more in-depth details on how to use the API.

There are also APIs for conflict resolution. If you want more robuts integration, check those out in the source code.

Libraries

play_games

Dart

dart:ui
Built-in types and core primitives for a Flutter application. [...]

Core

dart:async
Support for asynchronous programming, with classes such as Future and Stream. [...]
dart:collection
Classes and utilities that supplement the collection support in dart:core. [...]
dart:convert
Encoders and decoders for converting between different data representations, including JSON and UTF-8. [...]
dart:core
Built-in types, collections, and other core functionality for every Dart program. [...]
dart:developer
Interact with developer tools such as the debugger and inspector. [...]
dart:math
Mathematical constants and functions, plus a random number generator. [...]
dart:typed_data
Lists that efficiently handle fixed sized data (for example, unsigned 8 byte integers) and SIMD numeric types. [...]

VM

dart:io
File, socket, HTTP, and other I/O support for non-web applications. [...]
dart:isolate
Concurrent programming using isolates: independent workers that are similar to threads but don't share memory, communicating only via messages. [...]