Page 1 of 1

Test if multiplayer game ?

Posted: Mon May 09, 2016 11:30 pm
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.

Re: Test if multiplayer game ?

Posted: Mon May 09, 2016 11:41 pm
by prg
You can catch such errors with pcall.

Re: Test if multiplayer game ?

Posted: Tue May 10, 2016 12:35 am
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 ?

Re: Test if multiplayer game ?

Posted: Tue May 10, 2016 12:52 am
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.

Re: Test if multiplayer game ?

Posted: Tue May 10, 2016 8:14 am
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.

Re: Test if multiplayer game ?

Posted: Tue May 10, 2016 3:57 pm
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 ?

Re: Test if multiplayer game ?

Posted: Tue May 10, 2016 4:20 pm
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).

Re: Test if multiplayer game ?

Posted: Tue May 10, 2016 6:31 pm
by binbinhfr
oh ok, i understand better, thx