How to send a random seed to clients?

TheBroman90

Member
In my multiplayer game I want to make sure that all players have the same seed before spawning objects at random locations. In the hosts create event he creates a random seed which he later sends to connecting players.

Code:
randomize();
global.seed = random_get_seed();
When a player connects he is first sent to an empty room where he recieves the random seed from the host. After the random seed has been recieved he is sent to the game world where he should use the seed to spawn objects at the same locations as the host. My problem is that the client spawns objects at the same locations every time, like his game has not been "randomized".

I thought that maybe there was some problems which caused him to not recieve the random seed from the host. So I drew a text on the screen that tells you which seed you have. The client always had the same seed as the host. I then made one of the random objects grab the current seed in it's create event an put it in a text variable. That way I could see if the random seed was recieved before or after the objects were created. Again, the seed was correct. But the clients objects always spawned in the same location every time...

Is this the correct way to send a random seed or am I missing something?
 

TheBroman90

Member
As you can see the seed is the same for both players, but the green path is not generated in the same way.
The code for the path is basically a control object that spawns a green square at its x, y and then uses "choose()" to select a new direction.
So both games should choose the same direction.

 

FrostyCat

Redemption Seeker
I think the seed is only reflective of the initial state, not what comes after. If even a single more random function call slips in, you'd get an avalanche effect from that point on. Check your server and client code and ensure that the random calls are used in the exact same order and manner.
 

TheBroman90

Member
I noticed something now. Saving a randomized seed and send to the client is what causes trouble.

Code:
randomize();
global.seed = random_get_seed();
If I choose a seed myself and send to the client it works and both players spawn objects at the same locations.

Code:
global.seed = 10;
random_set_seed(global.seed);
The bottom code works while the top one doesn't. All other code is the same.
Why won't a randomized seed work, but a selected seed will?

EDIT: I noticed another thing. Sometimes the randomized code actually works and sometimes it doesn't. And everytime it doesn't work, the client always get the same incorrect seed: "2147483648". I googled this and found more people with the same problem and the same seed, but I couldn't find an answer.
 
Last edited:
you have to get the seed and then set it on both ends.

another thing that I found is that you have to read and write the seed as an integer, the seed is never negative that I can see, so might as well use an unsigned integer (buffer_u32). You can't transmit the seed as a floating point.
 

TheBroman90

Member
Thanks for your reply. I did some more research and apparently randomize() can generate numbers bigger than game maker can handle. So if you try to set a seed to something bigger than 2147483648 it always rounds it back down. That's why the client sometimes got this magic number. I solved it by making a loop that randomizes the seed until it's smaller than 2147483648 before saving it.
 
EDIT: okay, hopefully this will be my last edit which totally alters the content of my message.

To bypass the loop, what you can do is use irandom() to get a seed, set it, and then transmit it to the client.

randomize();
seed = irandom(2147483647);
random_set_seed(seed);
 
Last edited:
Top