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

Place to get help with not working mods / modding interface.
Post Reply
User avatar
ownlyme
Filter Inserter
Filter Inserter
Posts: 400
Joined: Thu Dec 21, 2017 8:02 am
Contact:

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

Post 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...
mods.factorio.com/user/ownlyme
My requests: uiAbove||Grenade arc||Blueprint allies||Creeps forget command/ don't get removed||Player Modifiers||textbox::selection||Better Heat IF||Singleplayer RCON||tank bug w/ min_range >= projectile_creation_distance

Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

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

Post 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

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

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

Post 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).
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

quyxkh
Smart Inserter
Smart Inserter
Posts: 1028
Joined: Sun May 08, 2016 9:01 am
Contact:

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

Post 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.

Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

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

Post 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)

Rseding91
Factorio Staff
Factorio Staff
Posts: 13209
Joined: Wed Jun 11, 2014 5:23 am
Contact:

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

Post 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.
If you want to get ahold of me I'm almost always on Discord.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

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

Post 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.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

Post Reply

Return to “Modding help”