Ipairs don't work?

Place to get help with not working mods / modding interface.
Post Reply
kiba
Filter Inserter
Filter Inserter
Posts: 344
Joined: Thu Jun 11, 2015 5:32 am
Contact:

Ipairs don't work?

Post by kiba »

Code: Select all

script.on_event(defines.events.on_tick, function(event)
  local n = 0
  for i,p in ipairs(game.players) do
    n = n + 1
  end
  print(n)
end)
For some reason, it's not iterating through game.players every tick?

What am I doing wrong?

keyboardhack
Filter Inserter
Filter Inserter
Posts: 478
Joined: Sat Aug 23, 2014 11:43 pm
Contact:

Re: Ipairs don't work?

Post by keyboardhack »

0.13.0 release notes wrote: Changed game.players, game.surfaces, game.entity_prototypes, game.item_prototypes, game.fluid_prototypes, force.recipes, force,technologies to use custom access + iterator objects for improved performance.
I assume this means you can't use ipairs. Try using pairs or turn it into a for loop instead.
Waste of bytes : P

kiba
Filter Inserter
Filter Inserter
Posts: 344
Joined: Thu Jun 11, 2015 5:32 am
Contact:

Re: Ipairs don't work?

Post by kiba »

keyboardhack wrote:
0.13.0 release notes wrote: Changed game.players, game.surfaces, game.entity_prototypes, game.item_prototypes, game.fluid_prototypes, force.recipes, force,technologies to use custom access + iterator objects for improved performance.
I assume this means you can't use ipairs. Try using pairs or turn it into a for loop instead.
I tried out pairs. It worked, though the changelog was confusing to me.

Thanks.

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

Re: Ipairs don't work?

Post by Rseding91 »

If all your after is the count of players:

Code: Select all

#game.players
If you want to get ahold of me I'm almost always on Discord.

kiba
Filter Inserter
Filter Inserter
Posts: 344
Joined: Thu Jun 11, 2015 5:32 am
Contact:

Re: Ipairs don't work?

Post by kiba »

Rseding91 wrote:If all your after is the count of players:

Code: Select all

#game.players
I am not after the count of players, just that I don't know why ipairs won't work on game.players.

User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: Ipairs don't work?

Post by aubergine18 »

'ipairs' only works on numerically referenced tables, where as 'pairs' works on name referenced tables. In lua it's possible to define custom ipairs and pairs functions for a table, and also custom getter for table length (although that's deprecated it memory serves).

I'm guessing the changes were made to allow lazy initialisation of tables, to boost performance, in which case the standard iterators would not work (because they expect the table to already be populated).
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.


kiba
Filter Inserter
Filter Inserter
Posts: 344
Joined: Thu Jun 11, 2015 5:32 am
Contact:

Re: Ipairs don't work?

Post by kiba »

The Lua API documentation is fine for what it does, but it's neither a tutorial nor a FAQ.

I would have never thought about custom table and such.

User avatar
DedlySpyder
Filter Inserter
Filter Inserter
Posts: 253
Joined: Fri Jun 20, 2014 11:42 am
Contact:

Re: Ipairs don't work?

Post by DedlySpyder »

kiba wrote: The Lua API documentation is fine for what it does, but it's neither a tutorial nor a FAQ.

I would have never thought about custom table and such.
I know, I dropped that here for the people wondering why ipairs doesn't work

Post Reply

Return to “Modding help”