Page 1 of 1

Can someone explain what "client" executes on_events?

Posted: Fri May 19, 2017 10:01 am
by AlienX
Hi All,
I have a general question about how Factorio works with it's Lua Events (such as on_chunk_generated).

Task: Imagine 3 clients are connected to the game, there is a custom script inside the control.lua of the scenario file that re-rolls generated chunks to randomly spawn new entities, such as ore, or enemy bases, all 3 clients are connected to a headless client acting as the server.

Do all clients run the on_chunk_generated code at the same time?

If so, how does Factorio deal with entities that are randomly selected to be spawned or not?

Let's say that Client 1's random algorithm selected to spawn the entity, the headless client also did, but client 2 and 3's random algorithm selected not too - would this cause a desync due to 2 clients not having spawned the entity on their local map variant causing their map tick to have a different CRC than the rest?

The reason I ask this is because I believe that this is happening for me when new chunks are generated everyone desyncs - perhaps i just need to run the on_chunk_generated code on just a single client's game?

Re: Can someone explain what "client" executes on_events?

Posted: Fri May 19, 2017 10:27 am
by darkfrei
It's pseudo random, all clients have the same generator.

Re: Can someone explain what "client" executes on_events?

Posted: Fri May 19, 2017 2:40 pm
by Nexela
The reason I ask this is because I believe that this is happening for me when new chunks are generated everyone desyncs - perhaps i just need to run the on_chunk_generated code on just a single client's game?
All clients have to run the exact same code at the exact same time and get the exact same result
Random is deterministicaly random for everyone :)

Feel free to post your code and someone will look at it. Are you saving variables correctly in the global table?

Re: Can someone explain what "client" executes on_events?

Posted: Fri May 19, 2017 11:51 pm
by DaveMcW
Yes, your problem is any variable that is not stored in global is not multiplayer-safe.

Re: Can someone explain what "client" executes on_events?

Posted: Sat May 20, 2017 11:37 am
by AlienX
Alright that makes sense, thanks guys :)