0
\$\begingroup\$

sorry for the unhelpful title. I'm trying to understand how to convert information to bytes so that I can save it using Google's Saved Games feature that comes with Play Services, as explained here.

https://codelabs.developers.google.com/codelabs/playservices_unity/index.html?index=..%2F..%2Findex#9

I've mostly copy-pasted the code but it's not really working out for me.

However, my initial question is about bytes. So it looks like Google stores game information in bytes[] . The example only shows a single int being converted to byte and then being saved. What if I wanted to save multiple integers in Google's Saved Game services? I'm not exactly sure what this byte array is in the first place and how everything can be converted to bytes. If anybody has any good resources or examples, it would be much appreciated! Thank you

\$\endgroup\$
  • \$\begingroup\$ Byte array is literally array of bytes. Any information is stored in bytes on your PC. Array of byte, in terms of programming, is representation of some sort of data in most simple form. For instance, you have some custom data type. And you want to store its data somewhere. But for some reason, you can't simply save data in format you've created. Then you simply convert this data to byte array, which is simple data format, and can be saved mostly anywhere. Then you just get this data back, when you need it, and convert it back to actual data type. \$\endgroup\$ – lentinant Sep 8 '16 at 7:20
1
\$\begingroup\$

You can convert pretty much anything to a byte[] but the built-in methods are not all the same.

For numeric, non-string values you can do:

byte[] _intValue = BitConverter.GetBytes(_someInt);

For string values, you can do:

byte[] _stringValue = Encoding.ASCII.GetBytes(_someString);

If this is something you plan to do a lot of, you may want to create your own overloaded classes so you have a single call you can make to convert any data you need, probably with methods to covert to and from byte[] for each.

\$\endgroup\$
  • 2
    \$\begingroup\$ When there is a possibility that your string might contain international characters, you should of course use Encoding.UTF8 instead of Encoding.ASCII. \$\endgroup\$ – Philipp Sep 8 '16 at 12:22
  • \$\begingroup\$ @Philipp yes, for sure. These are two examples of many possible methods for converting to byte array. \$\endgroup\$ – Jesse Williams Sep 8 '16 at 12:31
  • \$\begingroup\$ What if I wanted to save multiple things in a byte array? For example , say I wanted to save an int and a string. Now I have two byte arrays - but Google play saved games only takes one byte array. Can I combine the two byte arrays and mark the start and end so the information doesn't get mixed up? \$\endgroup\$ – squeegene Sep 8 '16 at 22:19
  • \$\begingroup\$ Yes, if they are always a fixed length, or you can pad them to a fixed length, you can just dump them in. If they aren't, I'd recommend using a particular series of unlikely characters to mark a separation between elements. Of course, they should always be packed in the same order. If they may be in a different order, you could also use specifiers. \$\endgroup\$ – Jesse Williams Sep 8 '16 at 22:44
  • 1
    \$\begingroup\$ Great idea, thank you so much. Sorry for the delayed comment \$\endgroup\$ – squeegene Sep 14 '16 at 23:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.