MP efficient events.

Place to get help with not working mods / modding interface.
billw
Long Handed Inserter
Long Handed Inserter
Posts: 67
Joined: Tue May 27, 2014 10:27 am
Contact:

MP efficient events.

Post by billw »

I have a problem with my MP current game where sometimes all clients get freezing when an object is built. i.e. every build object causes a 0.5 to 1 second freeze. For periods of time this will happen, then for no reason I can see it won't happen at all. Having talked to a couple of people it seems this doesn't happen for everyone even with massive bases, so I am concluding that it is probably related to a mod.
My suspicion is that on_entity_built event handlers that access global data that is large could cause this. I want to look into my different installed mods to find out which one is the culprit and try and optimize the access pattern for global data somehow.

So my questions:

Does my conclusion sound right (syncing global data every time an item is built causing freezing)?
What is the best way to do on_entity_built events to be MP efficient, assuming that is the issue?
Rseding91
Factorio Staff
Factorio Staff
Posts: 14591
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: MP efficient events.

Post by Rseding91 »

Factorio doesn't sync mod data runtime (it's determistic so they always run the same on every client). However, it could easily be a mod that was coded poorly and is halting the game while it runs the code in the mod.

You can press F4 and enable the "update times" option to see which mods are taking how much CPU time - then simply do stuff and watch which one is taking the most time.
If you want to get ahold of me I'm almost always on Discord.
johanwanderer
Fast Inserter
Fast Inserter
Posts: 157
Joined: Fri Jun 26, 2015 11:13 pm

Re: MP efficient events.

Post by johanwanderer »

Rseding91 wrote:Factorio doesn't sync mod data runtime (it's determistic so they always run the same on every client). However, it could easily be a mod that was coded poorly and is halting the game while it runs the code in the mod.

You can press F4 and enable the "update times" option to see which mods are taking how much CPU time - then simply do stuff and watch which one is taking the most time.
That's good to know. I guess before we use random() in the mod, we should make sure we save the seed in global to make sure that all clients get the same RNG outputs.
orzelek
Smart Inserter
Smart Inserter
Posts: 3924
Joined: Fri Apr 03, 2015 10:20 am
Contact:

Re: MP efficient events.

Post by orzelek »

johanwanderer wrote:
Rseding91 wrote:Factorio doesn't sync mod data runtime (it's determistic so they always run the same on every client). However, it could easily be a mod that was coded poorly and is halting the game while it runs the code in the mod.

You can press F4 and enable the "update times" option to see which mods are taking how much CPU time - then simply do stuff and watch which one is taking the most time.
That's good to know. I guess before we use random() in the mod, we should make sure we save the seed in global to make sure that all clients get the same RNG outputs.
Lua's random is attached to global map seed. If you can guarantee exact order of calls (same random calls on all clients) it will work correctly.
johanwanderer
Fast Inserter
Fast Inserter
Posts: 157
Joined: Fri Jun 26, 2015 11:13 pm

Re: MP efficient events.

Post by johanwanderer »

orzelek wrote:Lua's random is attached to global map seed. If you can guarantee exact order of calls (same random calls on all clients) it will work correctly.
Great! I think that only left the player (via GUI events) as the sole source of randomness. Now this deviates from the thread's main point, but is there a way to synchronize some part of a mod's state for that reason? Scenario: mod creates a GUI that allows players to set persistent states. Would such a mod be confined to single-player only for now, or is there a way to package such "mod state" and sync it to the other players?
ratchetfreak
Filter Inserter
Filter Inserter
Posts: 952
Joined: Sat May 23, 2015 12:10 pm
Contact:

Re: MP efficient events.

Post by ratchetfreak »

Correct we if I'm wrong but doesn't the game automatically send all player click events to all clients?
johanwanderer
Fast Inserter
Fast Inserter
Posts: 157
Joined: Fri Jun 26, 2015 11:13 pm

Re: MP efficient events.

Post by johanwanderer »

ratchetfreak wrote:Correct we if I'm wrong but doesn't the game automatically send all player click events to all clients?
I will have to play with it more, but each player can have different GUI states (one have X GUI opened, one have Y, etc.) so a "click" may not mean the same to all player. Again, I will play with GUI syncs once the mod I'm working on is more matured. Right now I'm focusing on getting it working for single player.
ratchetfreak
Filter Inserter
Filter Inserter
Posts: 952
Joined: Sat May 23, 2015 12:10 pm
Contact:

Re: MP efficient events.

Post by ratchetfreak »

johanwanderer wrote:
ratchetfreak wrote:Correct we if I'm wrong but doesn't the game automatically send all player click events to all clients?
I will have to play with it more, but each player can have different GUI states (one have X GUI opened, one have Y, etc.) so a "click" may not mean the same to all player. Again, I will play with GUI syncs once the mod I'm working on is more matured. Right now I'm focusing on getting it working for single player.
I meant that on_gui_click gets called on all clients with the relevant info (player index and gui element)
Rseding91
Factorio Staff
Factorio Staff
Posts: 14591
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: MP efficient events.

Post by Rseding91 »

ratchetfreak wrote:
johanwanderer wrote:
ratchetfreak wrote:Correct we if I'm wrong but doesn't the game automatically send all player click events to all clients?
I will have to play with it more, but each player can have different GUI states (one have X GUI opened, one have Y, etc.) so a "click" may not mean the same to all player. Again, I will play with GUI syncs once the mod I'm working on is more matured. Right now I'm focusing on getting it working for single player.
I meant that on_gui_click gets called on all clients with the relevant info (player index and gui element)
Correct. When making a mod you don't need to worry about any of that stuff. The game engine handles firing events in the correct order regardless of which peer originally created the event. If it didn't work this way MP would never work even without mods :)

The only part(s) left when making a mod is make sure your Lua local variables are either static or tied to the mods global variables (which are saved between save/load). Basically: save/load should not change any part of your mod's runtime data.
If you want to get ahold of me I'm almost always on Discord.
Post Reply

Return to “Modding help”