Сервисы Google Play Games: сохраненные игры, создание нового снимка - PullRequest
0 голосов
/ 09 июля 2019

Я следую руководству Google для разработчиков, посвященному настройке сохраненных игр в моей игре, когда я дохожу до того момента, когда вы создаете снимок для хранения своих данных, в руководстве не указывается, что делать.

Также я проверил приведенный здесь пример кода https://github.com/gguuss/android-basic-samples/tree/master/BasicSamples/CollectAllTheStars2

но некоторые из используемых методов помечены как устаревшие, и они не обновляли код с 5 лет назад

private String mCurrentSaveName = "snapshotTemp";

/**
 * This callback will be triggered after you call startActivityForResult from the
 * showSavedGamesUI method.
 */
@Override
protected void onActivityResult(int requestCode, int resultCode,
                                Intent intent) {
  if (intent != null) {
    if (intent.hasExtra(SnapshotsClient.EXTRA_SNAPSHOT_METADATA)) {
      // Load a snapshot.
      SnapshotMetadata snapshotMetadata =
          intent.getParcelableExtra(SnapshotsClient.EXTRA_SNAPSHOT_METADATA);
      mCurrentSaveName = snapshotMetadata.getUniqueName();

      // Load the game data from the Snapshot
      // ...
    } else if (intent.hasExtra(SnapshotsClient.EXTRA_SNAPSHOT_NEW)) {
      // Create a new snapshot named with a unique string
      String unique = new BigInteger(281, new Random()).toString(13);
      mCurrentSaveName = "snapshotTemp-" + unique;

      // Create the new snapshot
      // ...
    }
  }
}

Кто-нибудь может мне помочь? и что делать около //create the new snapshot comment?

1 Ответ

0 голосов
/ 10 июля 2019

После долгих исследований я узнал, как это сделать, прежде чем вы сделаете это, ваш игрок должен войти в систему

//the data you want to save must be in bytes
byte[] bytearray;
//name of the snapshot
private String currentSave="snapshot"+1235;

private void executeSaving(){

    // get the snapshoClient
    SnapshotsClient snapshotsClient= Games.getSnapshotsClient(this,account);

    // to save a game
    snapshotsClient.open(currentSave,true).addOnCompleteListener(task -> {

        Snapshot snapshot =task.getResult().getData();

        if (snapshot != null){
            //call of the method writeSnapshot params : the snapshot and the data we 
            //want to save with a description
            writeSnapshot(snapshot,bytearray,"first description").addOnCompleteListener(task1 -> {

                if(task1.isSuccessful()){
                    Toast.makeText(getApplicationContext(),"Succesful",Toast.LENGTH_SHORT).show();
                }else {
                    Log.e("ERR",""+task1.getException());
                }
            });
        }
    });
}

private Task<SnapshotMetadata> writeSnapshot(Snapshot snapshot ,byte[] bytearray,String desc){

    //write your data in the snapshot
    snapshot.getSnapshotContents().writeBytes(bytearray);

    SnapshotMetadataChange snapshotMetadata = new SnapshotMetadataChange.Builder().setDescription(desc).build();

    SnapshotsClient snapshotsClient=Games.getSnapshotsClient(this,account);

    //commit and close to send the new changes to google play games services
    return snapshotsClient.commitAndClose(snapshot,snapshotMetadata);
}
...