1
votes

I'm following the google developers guide on how to setup saved games in my game, when i reach the part where you create a snapshot to store your data, the guide doesn't specify what to do.

Also i checked the sample code provided here https://github.com/gguuss/android-basic-samples/tree/master/BasicSamples/CollectAllTheStars2

but some of the methods used are marked as deprecated, and they didn't update the code since 5 years ago

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
      // ...
    }
  }
}

Can anyone help me ? and what to be done near //create the new snapshot comment ?

1

1 Answers

1
votes

After much research i found out how to do it, before you do this your player must be signed in

//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);
}