Page 1 of 1

Ipairs don't work?

Posted: Sat Jul 09, 2016 1:44 am
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?

Re: Ipairs don't work?

Posted: Sat Jul 09, 2016 1:52 am
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.

Re: Ipairs don't work?

Posted: Sat Jul 09, 2016 1:57 am
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.

Re: Ipairs don't work?

Posted: Sat Jul 09, 2016 7:53 am
by Rseding91
If all your after is the count of players:

Code: Select all

#game.players

Re: Ipairs don't work?

Posted: Sat Jul 09, 2016 8:10 am
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.

Re: Ipairs don't work?

Posted: Mon Jul 25, 2016 9:22 pm
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).

Re: Ipairs don't work?

Posted: Tue Jul 26, 2016 3:08 pm
by DedlySpyder

Re: Ipairs don't work?

Posted: Tue Jul 26, 2016 4:07 pm
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.

Re: Ipairs don't work?

Posted: Tue Jul 26, 2016 4:34 pm
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