Page 1 of 1

MP efficient events.

Posted: Thu Jul 30, 2015 4:21 pm
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?

Re: MP efficient events.

Posted: Fri Jul 31, 2015 6:02 pm
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.

Re: MP efficient events.

Posted: Sat Aug 01, 2015 3:05 am
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.

Re: MP efficient events.

Posted: Sat Aug 01, 2015 10:15 am
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.

Re: MP efficient events.

Posted: Mon Aug 03, 2015 4:08 pm
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?

Re: MP efficient events.

Posted: Mon Aug 03, 2015 4:31 pm
by ratchetfreak
Correct we if I'm wrong but doesn't the game automatically send all player click events to all clients?

Re: MP efficient events.

Posted: Mon Aug 03, 2015 7:50 pm
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.

Re: MP efficient events.

Posted: Mon Aug 03, 2015 8:06 pm
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)

Re: MP efficient events.

Posted: Thu Aug 06, 2015 12:22 am
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.