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...
test,_ = next(game.players,test) only returns __self?
test,_ = next(game.players,test) only returns __self?
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
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
Re: test,_ = next(game.players,test) only returns __self?
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
- eradicator
- Smart Inserter
- Posts: 5206
- Joined: Tue Jul 12, 2016 9:03 am
- Contact:
Re: test,_ = next(game.players,test) only returns __self?
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).
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.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
Re: test,_ = next(game.players,test) only returns __self?
`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?
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?
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.
- eradicator
- Smart Inserter
- Posts: 5206
- Joined: Tue Jul 12, 2016 9:03 am
- Contact:
Re: test,_ = next(game.players,test) only returns __self?
As expected, thanks for confimation.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.
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.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.