Page 1 of 1
get local player in mp
Posted: Sun Nov 02, 2014 10:04 am
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

Re: get local player in mp
Posted: Sun Nov 02, 2014 11:05 am
by JamesOFarrell
Not sure about onputitem but for onguiclick you can use:
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
Re: get local player in mp
Posted: Sun Nov 02, 2014 3:47 pm
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
Re: get local player in mp
Posted: Sun Nov 02, 2014 8:17 pm
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?
Re: get local player in mp
Posted: Sun Nov 02, 2014 9:39 pm
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

Re: get local player in mp
Posted: Mon Nov 03, 2014 8:01 am
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
Re: get local player in mp
Posted: Sat Nov 08, 2014 12:49 am
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