I need a little help. I'm making a simple enough game in Unity3D and I'd like to save a few variables (gold, inventory and such) using SavedGames (Google Games Services). I've read about it and apparently I have to send the data to Google Games as a byte array. So I've read about bytes arrays too, but I don't understand it. Every article or question about it seems to assume a certain degree of knowledge about it (which I do not have obviously). I'm stuck. My question is : how do you go from int variables to bytes arrays?
For example: User has 52 gold coins (int gold_coins), three healing potions (int heal_pot) and 4 mana potions (int mana_pot). I want to put those three variables into a byte array that I can send add to Google when the user saves the game state. Then, when he loads it again the data from the byte array needs to go back to the ints. (That last part I can probably figure out by myself when I see how to put them into the array in the first place).
I hope you guys can explain it to me or point me into the right direction. In the meantime, have a nice monday. Thank you.
EDIT 2: So, I've got this :
in SaveData.cs :
using UnityEngine;
using ProtoBuf;
[ProtoContract]
public enum DataType{
pu0 = 0; //gold in game
pu1 = 1; //heal_pot ingame
pu2 = 2; //mana_pot ingame
}
[ProtoContract]
public class PBSaveData{
[ProtoMember(1)]
public DataType type;
[ProtoMember(2)]
public int amountStored;
}
in PBGameState.cs:
using ProtoBuf;
[ProtoContract]
public class PBGameState{
[ProtoMember(1)]
public PBSaveData[] saveData;
}
in SaveManager.cs:
using ProtoBuf;
using System.IO;
public class SaveManager{
private string gameStateFilePath;
void SaveState(){
PBStateGame state = new PBGameState();
state.saveData = new PBSaveData[3];
for (int i=0; i<state.saveData.Length{
state.saveData[i] = new PBSaveData();
state.saveData[i].type = "pu" + (i+1);
state.saveData[i].amoundStore = its[i] //its is where is store my inventory locally.
}
using(var ms = new MemoryStream()){
// Serialize the data to the stream
Serializer.Serialize(ms, state);
byteArray = ms.ToArray();
}
// Create a texture the size of the screen, RGB24 format
int width = Screen.width;
int height = Screen.height;
Texture2D Screenshot = new Texture2D( width, height, TextureFormat.RGB24, false );
// Read screen contents into the texture
Screenshot.ReadPixels( new Rect(0, 0, width, height), 0, 0 );
Screenshot.Apply();
long TotalPlayedTime = 20000;
string currentSaveName = "sg";
string description = "Modified data at: " + System.DateTime.Now.ToString("MM/dd/yyyy H:mm:ss");
GooglePlaySavedGamesManager.ActionGameSaveResult += ActionGameSaveResult;
GooglePlaySavedGamesManager.instance.CreateNewSnapshot(currentSaveName, description, Screenshot, byteArray, TotalPlayedTime);
}
}
void LoadState()
{
PBGameState state = new PBGameState ();
using(var ms = new MemoryStream(byteArray)){
state = Serializer.Deserialize<PBGameState> (ms);
AndroidMessage.Create ("loadtest", state.saveData[0].amountStored.ToString());
}
if (state.saveData != null) {
// Iterate through the loaded game state
for (int i = 0; i < state.saveData.Length; i++) {
its [i] = state.saveData [i].amountStored;
}
} else {
AndroidMessage.Create ("loadtest", "notworking");
}
}
So saving works apparently, but when I restart the game, the data don't seem to be loaded (I have a button that starts the loading process) and the inventory is empty... Any idea what I did wrong? Thank you :)
Here is the link for the tutorial : tuto
Anyone please?