get local player in mp

Place to get help with not working mods / modding interface.
drs9999
Filter Inserter
Filter Inserter
Posts: 831
Joined: Wed Mar 06, 2013 11:16 pm
Contact:

get local player in mp

Post by drs9999 »

"Simple" question, how can I receive the local player in events (or function in general) which don't return the player index, e.g. OnPutItem, OnGuiClick?

For now I store the playerIndex in the onBuilt event and use it afterwards. That seems to work ok for a single session, but I highly doubt that it will work after continuing a saved game, because the playerIndices probably changed.

If I didn't miss any built-in function I suggest that game.getplayer() don't raise an error, but return the local player instead.
But for now I'm moreintrested in workarounds ;)
JamesOFarrell
Filter Inserter
Filter Inserter
Posts: 402
Joined: Fri May 23, 2014 8:54 am
Contact:

Re: get local player in mp

Post by JamesOFarrell »

Not sure about onputitem but for onguiclick you can use:

Code: Select all

event.element.playerindex
edit: also game.getplayer() would cause desync if you think about it. Your mod has to run on every connected copy of factorio and produce exactly the same results. As game.getplayer() would return a deferent result on different machines it can't work
drs9999
Filter Inserter
Filter Inserter
Posts: 831
Joined: Wed Mar 06, 2013 11:16 pm
Contact:

Re: get local player in mp

Post by drs9999 »

JamesOFarrell wrote:edit: also game.getplayer() would cause desync if you think about it. Your mod has to run on every connected copy of factorio and produce exactly the same results. As game.getplayer() would return a deferent result on different machines it can't work
IF I think about it... But I didn't ;)

btw. I found a solution for my usecase.
I use the onput-event to create custom UIs for entities (e.g. adv treefarm), I thought I would need the specific player who raised the event, but actually that isn't needed. What I do now is whenever the event is raised, I loop through all players and check if anyone selected the specific entity. Afterwards I create the UI for the player
JamesOFarrell
Filter Inserter
Filter Inserter
Posts: 402
Joined: Fri May 23, 2014 8:54 am
Contact:

Re: get local player in mp

Post by JamesOFarrell »

drs9999 wrote:IF I think about it... But I didn't ;)
This is a mistake I keep making as well when it comes to determinism. I'm just not used to thinking this way when it comes to multiplayer.
drs9999 wrote:btw. I found a solution for my usecase.
I use the onput-event to create custom UIs for entities (e.g. adv treefarm), I thought I would need the specific player who raised the event, but actually that isn't needed. What I do now is whenever the event is raised, I loop through all players and check if anyone selected the specific entity. Afterwards I create the UI for the player
I've managed to get what I think should be a working MP GUI going but I get desync if the init code runs before all players have connected to the game. Are you seeing anything like this?
jeroon
Filter Inserter
Filter Inserter
Posts: 351
Joined: Sun Jan 26, 2014 10:18 am
Contact:

Re: get local player in mp

Post by jeroon »

I think it's just the few ticks when a player is joining that corrupt the GUI, I'm trying to stop all GUI updates for a second when someone new joins, just not sure how to do that :)

To get the current player in drawing the GUI, I do a loop through all players, and inside that another loop, and if playerOuterLoop == playerInnerLoop, I don't draw to the screen. If that makes sense.. Probably not ;) :D
drs9999
Filter Inserter
Filter Inserter
Posts: 831
Joined: Wed Mar 06, 2013 11:16 pm
Contact:

Re: get local player in mp

Post by drs9999 »

JamesOFarrell wrote:I've managed to get what I think should be a working MP GUI going but I get desync if the init code runs before all players have connected to the game. Are you seeing anything like this?
No I didn't see any GUI related desyncs. But actually that doesn't mean anything, because I only tested it with one more player and there was no open gui while anyone joined/left.

I'm going to test it later
JamesOFarrell
Filter Inserter
Filter Inserter
Posts: 402
Joined: Fri May 23, 2014 8:54 am
Contact:

Re: get local player in mp

Post by JamesOFarrell »

Interestingly I've found a way to do this. It causes desync but here it is. This will only print to the local console. it wont print to anyone elses console

Code: Select all

for i,player in ipairs(game.players) do 
  if pcall(function () local test = player.gameviewsettings end) then 
    player.print("HERE: " .. i) --code here will only run on local machine. causes desync.
  end
end
Post Reply

Return to “Modding help”