Mod multiplayer, identify local player ?

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:

Mod multiplayer, identify local player ?

Post by binbinhfr »

Hi,

I suppose that if you want to write a multiplayer compatible mod, you have to do a lot of
"for _, player in ipairs(game.players) do" everywhere instead of just working on the game.player (working only in single player mode)

but if I want to identify the player who is really local, in front of the PC running the game/mod, how can I do this ?
I thought that player.has_game_view() was the good function, but it does not seem to work.

An idea ?
My mods on the Factorio Mod Portal :geek:

User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5151
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: Mod multiplayer, identify local player ?

Post by Klonan »

binbinhfr wrote:Hi,

I suppose that if you want to write a multiplayer compatible mod, you have to do a lot of
"for _, player in ipairs(game.players) do" everywhere instead of just working on the game.player (working only in single player mode)

but if I want to identify the player who is really local, in front of the PC running the game/mod, how can I do this ?
I thought that player.has_game_view() was the good function, but it does not seem to work.

An idea ?
When you write a mod, all the players are local,
They are each assigned an index. and you can call individual players like

Code: Select all

game.players[i]
So the first player will be game.players[1], second player game.players[2] etc.

Unless you're doing some really tricky ,awkward, or downright incorrect scripting, your mod should be MP compatible regardless of what you're doing

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

Re: Mod multiplayer, identify local player ?

Post by binbinhfr »

I understood the players list, but correct me if i'm wrong :
each user runs one instance of factorio on his own machine, with the exact same configuration than other players, and only GUI commands are exchanged over the network, and you expect that every local client computes the same things as the others and stays in sync ?

So you say that all players are local, but considering one user in front of his PC : he is running his instance of factorio, computing his own player and every other players, but his own player is a little more "local", no ? :-)

So I wonder if there was a way to know whose player is really in front of the client.

I thought that has_game_view() was the purpose of the function : telling which player has really the GUI on this particular PC, running this particular client.
My mods on the Factorio Mod Portal :geek:

User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5151
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: Mod multiplayer, identify local player ?

Post by Klonan »

binbinhfr wrote:I understood the players list, but correct me if i'm wrong :
each user runs one instance of factorio on his own machine, with the exact same configuration than other players, and only GUI commands are exchanged over the network, and you expect that every local client computes the same things as the others and stays in sync ?

So you say that all players are local, but considering one user in front of his PC : he is running his instance of factorio, computing his own player and every other players, but his own player is a little more "local", no ? :-)

So I wonder if there was a way to know whose player is really in front of the client.

I thought that has_game_view() was the purpose of the function : telling which player has really the GUI on this particular PC, running this particular client.
Each player isnt running his own instance, but they all run the same instance, and only players inputs/actions are sent to eachother.
When you write a mod you don't need to know which player in the 'local' player, because any action must be scripted as if all other clients are running the same code.
This is why you can only call the players using the players system

If you can give an example of what you're trying to do, i can better give advice

roy7
Filter Inserter
Filter Inserter
Posts: 337
Joined: Fri Dec 12, 2014 4:24 pm
Contact:

Re: Mod multiplayer, identify local player ?

Post by roy7 »

Klonan, one application I can think of from his questions is, say, you wanted to display a # on the screen of the # of copper in your personal inventory. Not all players inventories or other people, just your own. That'd require a player where i is the player currently playing. Each player would have the same mod/script loaded of course, but each see a different result on their screen.

User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5151
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: Mod multiplayer, identify local player ?

Post by Klonan »

roy7 wrote:Klonan, one application I can think of from his questions is, say, you wanted to display a # on the screen of the # of copper in your personal inventory. Not all players inventories or other people, just your own. That'd require a player where i is the player currently playing. Each player would have the same mod/script loaded of course, but each see a different result on their screen.


But for that you would use a function based on the individual player, so you wouldnt show all players the specific inventory count for a single player, but instead base each players screen on their own inventory.

The way its done is when a gui function is called, it shows which player pressed it. With this you can update the gui and run a script for that player only, without having to update anything else for the other players

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

Re: Mod multiplayer, identify local player ?

Post by binbinhfr »

Klonan wrote:But for that you would use a function based on the individual player, so you wouldnt show all players the specific inventory count for a single player, but instead base each players screen on their own inventory.

The way its done is when a gui function is called, it shows which player pressed it. With this you can update the gui and run a script for that player only, without having to update anything else for the other players
ok , thanks Klonan, I understand better.

I wrote a singleplayer mod, and I understand that it's a big work to go in MP.
Every single variable that I store in global for one single player must be transformed into an array for all players.

As roy said, I thought that for some features that does not involve desync like giving informations in one way only (stats, etc...), there should be any use to duplicate heavy calculations, GUI, messages, etc... So, for such an "info" only mod, I could identify and store the ID of the local player, and only deal with him (assuming that the mod will do the same thing on every other client).

But still, you did not answer my bonus question : what is the has_game_view() function ?
it seems to return true if i'm in single player, but false on all players in MP.
My mods on the Factorio Mod Portal :geek:

Post Reply

Return to “Modding help”