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
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
sdgmlj wrote: ↑Fri Jun 10, 2022 6:39 amI want to find out all the production machines of this formula, but it always reports errors. Where is the error? Thank youCode: 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
So changing the inner loop like this should work:https://lua-api.factorio.com/latest/LuaEntityPrototype.html#LuaEntityPrototype.crafting_categories wrote: crafting_categories :: dictionary[string → boolean] [Read]
Code: Select all
-- for _,c in pairs(e.crafting_categories) do
for c, _ in pairs(e.crafting_categories) do
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
Pi-C wrote: ↑Fri Jun 10, 2022 8:42 amsdgmlj wrote: ↑Fri Jun 10, 2022 6:39 amI want to find out all the production machines of this formula, but it always reports errors. Where is the error? Thank youCode: 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
So changing the inner loop like this should work:https://lua-api.factorio.com/latest/LuaEntityPrototype.html#LuaEntityPrototype.crafting_categories wrote: crafting_categories :: dictionary[string → boolean] [Read]First of all, thank you very much for your help. I will change the content to:Code: Select all
-- for _,c in pairs(e.crafting_categories) do for c, _ in pairs(e.crafting_categories) do
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 youjodokus31 wrote: ↑Fri Jun 10, 2022 11:02 amThen 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: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
Code: Select all
for c,_ in pairs(e.crafting_categories or {}) do
..
end
Good point.Pi-C wrote: ↑Fri Jun 10, 2022 11:05 amEven simpler: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
Code: Select all
for c,_ in pairs(e.crafting_categories or {}) do .. end