Hi,
what is the good way to test if my mod is used in multiplayer ?
Because some functions can be used only in MP (like game.server_save) and stop the game if called in solo.
Thx for your help.
Test if multiplayer game ?
Test if multiplayer game ?
My mods on the Factorio Mod Portal
Re: Test if multiplayer game ?
You can catch such errors with pcall.
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
Re: Test if multiplayer game ?
NOT TESTED:
I think i saw on one thread someone mentioned that game.players is an array type in multiplayer but it is just a normal variable in single player. So just a typecheck there ?
I think i saw on one thread someone mentioned that game.players is an array type in multiplayer but it is just a normal variable in single player. So just a typecheck there ?
Re: Test if multiplayer game ?
game.players is always a table containing all the players that have played in the current game. It will just contain one entry if you've only ever played SP. But #game.players will still be >1 if you continue a MP game in SP so just checking for that is not enough, game.server_save() still won't work then.seronis wrote:NOT TESTED:
I think i saw on one thread someone mentioned that game.players is an array type in multiplayer but it is just a normal variable in single player. So just a typecheck there ?
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
Re: Test if multiplayer game ?
yes, i already tested the game.players, but it works like prg said...
i'm quite new to lua, and did't know this pcall protected mode. Nice information ! Thanks.
i'm quite new to lua, and did't know this pcall protected mode. Nice information ! Thanks.
My mods on the Factorio Mod Portal
Re: Test if multiplayer game ?
Alas, pcall(game.server_save()) also trigger an error from the game...
Pcall seems to react only to real lua errors.
Or did I miss something ? A require ?
Pcall seems to react only to real lua errors.
Or did I miss something ? A require ?
My mods on the Factorio Mod Portal
Re: Test if multiplayer game ?
With pcall(game.server_save()), you're calling server_save outside of pcall and passing the result of that to pcall. You need to pass the function game.server_save itself to pcall, i.e. pcall(game.server_save).
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!