Can someone explain what "client" executes on_events?

Place to get help with not working mods / modding interface.
Post Reply
AlienX
Fast Inserter
Fast Inserter
Posts: 103
Joined: Wed May 17, 2017 7:13 pm
Contact:

Can someone explain what "client" executes on_events?

Post 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?

User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2903
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

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

Post by darkfrei »

It's pseudo random, all clients have the same generator.

Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

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

Post 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?

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3699
Joined: Tue May 13, 2014 11:06 am
Contact:

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

Post by DaveMcW »

Yes, your problem is any variable that is not stored in global is not multiplayer-safe.

AlienX
Fast Inserter
Fast Inserter
Posts: 103
Joined: Wed May 17, 2017 7:13 pm
Contact:

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

Post by AlienX »

Alright that makes sense, thanks guys :)

Post Reply

Return to “Modding help”