Thank you for your suggestion and code provided. I never played multiplayer factorio so I very much appreciate your effort to make this mod better.
There was a slight problem (maybe it just seams like) but code in function on_player_creation(player) looks like executing 2x in single and multiplayer new game. So I did little correction but I'm not sure is that right so let me put code here:
newPlayers = {}
isPrinted = false
-- fires on player join (multiplayer)
script.on_event(defines.events.on_player_created, function(event)
-- record this player_index to update inventory once the player has an avatar
newPlayers[event.player_index] = event
if not isPrinted then
game.print("New player joined the game: " .. event.player_index)
end
end)
-- fires on the end of the cutscene (singleplayer)
script.on_event(defines.events.on_cutscene_cancelled, function(event)
-- record this player_index to update inventory once the player has an avatar
newPlayers[event.player_index] = event
if not isPrinted then
game.print("New player joined the game: " .. event.player_index)
end
end)
script.on_event(defines.events.on_tick, function(event)
for player_index, event in pairs(newPlayers) do
if event ~= nil and game.players[player_index] ~= nil then
local player = game.players[event.player_index]
on_player_creation(player)
if not isPrinted then
game.print("Setting inventory for " .. player.name)
end
isPrinted = true
newPlayers[player_index] = nil
end
end
end)
...
and so it the rest of the code where game.print is used.