Can you help me see what's wrong with it?

Place to get help with not working mods / modding interface.
Post Reply
sdgmlj
Fast Inserter
Fast Inserter
Posts: 127
Joined: Sun Jul 04, 2021 11:12 pm
Contact:

Can you help me see what's wrong with it?

Post 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

Pi-C
Smart Inserter
Smart Inserter
Posts: 1655
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Can you help me see what's wrong with it?

Post 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
https://lua-api.factorio.com/latest/LuaEntityPrototype.html#LuaEntityPrototype.crafting_categories wrote: crafting_categories :: dictionary[string → boolean] [Read]
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
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

sdgmlj
Fast Inserter
Fast Inserter
Posts: 127
Joined: Sun Jul 04, 2021 11:12 pm
Contact:

Re: Can you help me see what's wrong with it?

Post 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)"

sdgmlj
Fast Inserter
Fast Inserter
Posts: 127
Joined: Sun Jul 04, 2021 11:12 pm
Contact:

Re: Can you help me see what's wrong with it?

Post 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
https://lua-api.factorio.com/latest/LuaEntityPrototype.html#LuaEntityPrototype.crafting_categories wrote: crafting_categories :: dictionary[string → boolean] [Read]
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)"

User avatar
jodokus31
Smart Inserter
Smart Inserter
Posts: 1604
Joined: Sun Feb 26, 2017 4:13 pm
Contact:

Re: Can you help me see what's wrong with it?

Post 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

sdgmlj
Fast Inserter
Fast Inserter
Posts: 127
Joined: Sun Jul 04, 2021 11:12 pm
Contact:

Re: Can you help me see what's wrong with it?

Post 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 :D

Pi-C
Smart Inserter
Smart Inserter
Posts: 1655
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Can you help me see what's wrong with it?

Post 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
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

User avatar
jodokus31
Smart Inserter
Smart Inserter
Posts: 1604
Joined: Sun Feb 26, 2017 4:13 pm
Contact:

Re: Can you help me see what's wrong with it?

Post 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.

Post Reply

Return to “Modding help”