Page 1 of 1

test,_ = next(game.players,test) only returns __self?

Posted: Mon Jul 30, 2018 3:09 am
by ownlyme
i really can't figure out what i'm doing wrong.
if i use for test,_ in pairs(game.players) it works fine

see for yourself:
/c test,_ = next(game.players,test) game.players[1].print(test)
returns __self and nil

i can only assume the engine struggles with this way of iterating...

Re: test,_ = next(game.players,test) only returns __self?

Posted: Mon Jul 30, 2018 10:57 pm
by Nexela

Code: Select all

game.players is a fancy table
This should do what you want.
/c
do
  local IDX, PLAYER = next(game.players, global.IDX)
  global.IDX = IDX  -- Store the index in global because the value can change from one tick to the next --pps This is not the players index just the next connected player
  game.print(PLAYER.name)
end

Re: test,_ = next(game.players,test) only returns __self?

Posted: Wed Aug 08, 2018 2:50 pm
by eradicator
Please state the nature of the medical emergency requirement to use next instead of pairs.
As far as i remember there is no behavioral difference between the two in factorio. What am i missing?

@Nexela: Wait...since when does iterating game.players only return connected players?
And since when is the key of game.players not equal with the player_index? (I don't have savegame with tonnes of players to test, but have always assumed game.players[event.player_index].index == event.player_index).

Re: test,_ = next(game.players,test) only returns __self?

Posted: Wed Aug 08, 2018 5:59 pm
by quyxkh
`next` is a low-level function for iterating native lua tables. `pairs` can be front-ended by user-defined types, its default implementation returns `next` and the table but user-defined types can return a different function that knows how to iterate through the user type (and you can put a `pairs` override in your own metatables to get custom iteration). So `game.players` isn't maintained as a native lua table, you need to call `pairs` to get the right iterator for it, since that checks for custom iterators, and use the docs to see what else you can do with it. The docs say

Code: Select all

players :: custom dictionary uint or string → LuaPlayer [Read-only]
Note: This is a sparse table so pairs(), a known player index, or player name should be used to access elements.

Re: test,_ = next(game.players,test) only returns __self?

Posted: Thu Aug 09, 2018 12:24 am
by Nexela
eradicator wrote:
@Nexela: Wait...since when does iterating game.players only return connected players?
And since when is the key of game.players not equal with the player_index? (I don't have savegame with tonnes of players to test, but have always assumed game.players[event.player_index].index == event.player_index).

Ooopsie on my part, should have been game.connected_players (it is iterable with next)

Re: test,_ = next(game.players,test) only returns __self?

Posted: Sat Aug 11, 2018 7:35 am
by Rseding91
The index you get when iterating game.players is the players index. They're simply not gap-free so you can't do for i = 0; #game.players to iterate it.

Re: test,_ = next(game.players,test) only returns __self?

Posted: Sat Aug 11, 2018 10:54 am
by eradicator
Rseding91 wrote:The index you get when iterating game.players is the players index. They're simply not gap-free so you can't do for i = 0; #game.players to iterate it.
As expected, thanks for confimation.