Test if multiplayer game ?

Place to get help with not working mods / modding interface.
Post Reply
User avatar
binbinhfr
Smart Inserter
Smart Inserter
Posts: 1524
Joined: Sat Feb 27, 2016 7:37 pm
Contact:

Test if multiplayer game ?

Post by binbinhfr »

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.
My mods on the Factorio Mod Portal :geek:

User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: Test if multiplayer game ?

Post by prg »

You can catch such errors with pcall.
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!

seronis
Fast Inserter
Fast Inserter
Posts: 225
Joined: Fri Mar 04, 2016 8:04 pm
Contact:

Re: Test if multiplayer game ?

Post by seronis »

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 ?

User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: Test if multiplayer game ?

Post by prg »

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 ?
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.
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!

User avatar
binbinhfr
Smart Inserter
Smart Inserter
Posts: 1524
Joined: Sat Feb 27, 2016 7:37 pm
Contact:

Re: Test if multiplayer game ?

Post by binbinhfr »

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.
My mods on the Factorio Mod Portal :geek:

User avatar
binbinhfr
Smart Inserter
Smart Inserter
Posts: 1524
Joined: Sat Feb 27, 2016 7:37 pm
Contact:

Re: Test if multiplayer game ?

Post by binbinhfr »

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 ?
My mods on the Factorio Mod Portal :geek:

User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: Test if multiplayer game ?

Post by prg »

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!

User avatar
binbinhfr
Smart Inserter
Smart Inserter
Posts: 1524
Joined: Sat Feb 27, 2016 7:37 pm
Contact:

Re: Test if multiplayer game ?

Post by binbinhfr »

oh ok, i understand better, thx
My mods on the Factorio Mod Portal :geek:

Post Reply

Return to “Modding help”