Reputation: 121
i want to build a realtime multiplayer game with google play services where when a player moves a card on his screen, the other player sees the movement the first player made. Basically, i do not want to pass just a score between players, but actually make them play on the exact same screen and see the movements of their oponents.
I followed this tutorial: https://developers.google.com/games/services/android/realtimeMultiplayer
And it seems like the players send byte arrays between each other. How can i pass the whole game state as byte array?
If it helps to understand: Say i want to build a realtime racing game using google play services, how can i make the players see each others cars and cars' movements?
Is there an example with source code about it so i can study it?
Upvotes: 0
Views: 179
Reputation: 436
I ran into a similar problem, and I believe I have something for you to check out:
http://www.tutorialspoint.com/java/java_serialization.htm
Unfortunately, I haven't implemented it correctly, but I think I am getting close.
The whole idea of serialization is that you get your game state as an object (Probably make another class like Race State or something) that has all your data, as fields, write it as a byte array, and return that byte array when you are sending the reliable/unreliable message (I still suggest reliable unless you plan to correct for missing/out of order packets).
On the other side (reading the packet) you just accept the byte array (in onMessageReceived()
), call getMessage()
(which I assumes gets rid of any extraneous transfer data), and read the bytes and cast it as your gamestate object. You should then be able to get()
the field data for your game state and use as you need.
Again, it is still a work in progress for me (frustrating, I know), but do not bother sending strings as bytes or encoding, that muddles up the data. I wish the documentation was a little more clear on implementing this, although it assumes most people have worked with network data before.
Feel free to comment with questions, and good luck.
Upvotes: 0