- Home /
How to make randomly generated floats the same across multiplayer? (GPGS)
Is there any way to make randomly generated floats the same in different instances of a game in multiplayer? I am using Google Play Games Services to do real time multiplayer in my android app and there are cubes that are randomly instantiated. However, I need each opponent on multiplayer to have the cubes in the same place as eachother.
These cubes get randomly generated at the start, but must be the same for people on multiplayer. For example, my game allows two people to play eachother. Both of them need to see the cubes in the same place. Is there any way to do this?
Any help is greatly appreciated, thanks.
Answer by tanoshimi · Dec 27, 2015 at 03:04 PM
By definition, you can't separately generate two random values and expect them to be the same. So, you either need to generate the random value only in one place (on a server, or one particular client) and then send the value to all clients. Or, you need to not have a truly "random" value, but one which is controllable: on the game start you could set a common seed value among all clients and use that to seed a series of numbers on all clients by using a common algorithm.
Thanks for the answer! Do you know how I would set the values on one client and send those values to the other client?
I'd have some sort of lobby system - the first player to join the game becomes the "server". It is on their instance that all game-specific values are generated. These are then sent to all the other clients using exactly the same methods that you use for network play within the game.