Northsoft wrote: Fri Jun 11, 2021 9:44 am
I know that `print` gives output to every user connected. I'd like to do not show players some intermediate information from scripts.
Here's the thing: you can use the native lua `print` from just *one* instance without risking a desync, because stdout is not part of the game state. Local mod state that never affects the game state is perfectly fine, in fact your entire mod state is local, it's what you're (usually) trying to keep in sync with all other instances' states so all their effects on the game state stay in sync.
Best I've been able to tell, there's five locally-discernible situations: setup (which ends when on_load completes or on_init starts), limbo(when ends when any game event other than on_load fires), server, singleplayer, multiplayer.
But the only way I've found to tell whether a multiplayer instance is the first client or the server is if the server chats before any players are connected:
Code: Select all
script.on_event(defines.events.on_console_chat,function(ev)
if #game.connected_players == 0 then mystate='server'
end end)
and you must never feed that variable's value to any game function (or almost any, but you're trying to avoid write_to_file here right?). `print` is a native lua function that doesn't call in to the game engine, Factorio doesn't see or care what values it gets or what it does with them.
You could run your server with for instance `(echo hi this is Northsoft\'s server starting up; cat) | ./bin/x64/factorio` etc, then your mod would see the console chat regardless of whether the game ticks with no players connected, and so you can then have
Code: Select all
function serverprint(text) if mystate=='server' then print (type(text)=='string' and text or serpent.line(text)) end
and from anywhere in your mod you can `serverprint 'Kilroy is here!'`, it will only appear on the server console output because no other lua instance will have that `mystate` value.
fair warning: I have smoketested this, implemented it just now on a local server, but it hasn't been tested in the wild.