Page 1 of 1
Can you help me see what's wrong with it?
Posted: Fri Jun 10, 2022 6:39 am
by sdgmlj
Code: Select all
local g = game.recipe_prototypes[item_name]
for _,e in pairs(game.entity_prototypes) do
for _,c in pairs(e.crafting_categories) do
if c == g.category then
。。。。。。。。
end
end
end
I want to find out all the production machines of this formula, but it always reports errors. Where is the error? Thank you
Re: Can you help me see what's wrong with it?
Posted: Fri Jun 10, 2022 8:42 am
by Pi-C
sdgmlj wrote: Fri Jun 10, 2022 6:39 am
Code: Select all
local g = game.recipe_prototypes[item_name]
for _,e in pairs(game.entity_prototypes) do
for _,c in pairs(e.crafting_categories) do
if c == g.category then
。。。。。。。。
end
end
end
I want to find out all the production machines of this formula, but it always reports errors. Where is the error? Thank you
So changing the inner loop like this should work:
Code: Select all
-- for _,c in pairs(e.crafting_categories) do
for c, _ in pairs(e.crafting_categories) do
By the way, this is something you could easily have found out yourself by adding some debugging output. I usually always print out the current values of loop variables when I add a new function, just to be sure that the tables I use really contain what I need:
Code: Select all
local function show(msg, var)
log(string.format("%s: %s", msg, serpent.block(var)))
end
local g = game.recipe_prototypes[item_name]
show("g", g)
for _,e in pairs(game.entity_prototypes) do
show(_, e)
for _,c in pairs(e.crafting_categories) do
show(_, c)
if c == g.category then
。。。。。。。。
end
end
end
Re: Can you help me see what's wrong with it?
Posted: Fri Jun 10, 2022 10:52 am
by sdgmlj
First of all, thank you very much for your help. I will change the content to:
for c,_ in pairs(e.crafting_categories) do
It still reports an error with the content "bad argument \1 of 2 to'pairs'(table expected, got Nil)"
Re: Can you help me see what's wrong with it?
Posted: Fri Jun 10, 2022 10:54 am
by sdgmlj
Pi-C wrote: Fri Jun 10, 2022 8:42 am
sdgmlj wrote: Fri Jun 10, 2022 6:39 am
Code: Select all
local g = game.recipe_prototypes[item_name]
for _,e in pairs(game.entity_prototypes) do
for _,c in pairs(e.crafting_categories) do
if c == g.category then
。。。。。。。。
end
end
end
I want to find out all the production machines of this formula, but it always reports errors. Where is the error? Thank you
So changing the inner loop like this should work:
Code: Select all
-- for _,c in pairs(e.crafting_categories) do
for c, _ in pairs(e.crafting_categories) do
First of all, thank you very much for your help. I will change the content to:
for c,_ in pairs(e.crafting_categories) do
It still reports an error with the content "bad argument \1 of 2 to'pairs'(table expected, got Nil)"
Re: Can you help me see what's wrong with it?
Posted: Fri Jun 10, 2022 11:02 am
by jodokus31
sdgmlj wrote: Fri Jun 10, 2022 10:52 am
First of all, thank you very much for your help. I will change the content to:
for c,_ in pairs(e.crafting_categories) do
It still reports an error with the content "bad argument \1 of 2 to'pairs'(table expected, got Nil)"
Then you have to check that e.crafting_categories is not nil
Code: Select all
if e.crafting_categories then
for c,_ in pairs(e.crafting_categories) do
..
end
end
Re: Can you help me see what's wrong with it?
Posted: Fri Jun 10, 2022 11:05 am
by sdgmlj
jodokus31 wrote: Fri Jun 10, 2022 11:02 am
sdgmlj wrote: Fri Jun 10, 2022 10:52 am
First of all, thank you very much for your help. I will change the content to:
for c,_ in pairs(e.crafting_categories) do
It still reports an error with the content "bad argument \1 of 2 to'pairs'(table expected, got Nil)"
Then you have to check that e.crafting_categories is not nil
Code: Select all
if e.crafting_categories then
for c,_ in pairs(e.crafting_categories) do
..
end
end
OK, that's the reason. My problem has been solved. Thank you

Re: Can you help me see what's wrong with it?
Posted: Fri Jun 10, 2022 11:05 am
by Pi-C
jodokus31 wrote: Fri Jun 10, 2022 11:02 am
Then you have to check that e.crafting_categories is not nil
Code: Select all
if e.crafting_categories then
for c,_ in pairs(e.crafting_categories) do
..
end
end
Even simpler:
Code: Select all
for c,_ in pairs(e.crafting_categories or {}) do
..
end
Re: Can you help me see what's wrong with it?
Posted: Fri Jun 10, 2022 11:13 am
by jodokus31
Pi-C wrote: Fri Jun 10, 2022 11:05 am
jodokus31 wrote: Fri Jun 10, 2022 11:02 am
Then you have to check that e.crafting_categories is not nil
Code: Select all
if e.crafting_categories then
for c,_ in pairs(e.crafting_categories) do
..
end
end
Even simpler:
Code: Select all
for c,_ in pairs(e.crafting_categories or {}) do
..
end
Good point.