Trying to check type of energy_interface

Place to get help with not working mods / modding interface.
protocol_1903
Long Handed Inserter
Long Handed Inserter
Posts: 98
Joined: Fri Sep 09, 2022 4:33 pm
Contact:

Trying to check type of energy_interface

Post by protocol_1903 »

I'm trying to run the following code to check if a machine uses electricity, then change it to burner once that is done. It loads into Factorio but for whatever reason it isn't working. It is probably something wrong with the syntax, I'm new to LUA. I think its something with how I'm checking the source type.

Code: Select all

for _, machine in pairs(data.raw) do
  if machine.energy_source ~= nil then
    if machine.energy_source.type == "electric" then
      machine.energy_source = {
        type = "burner",
        effectivity = 0.5,
        fuel_inventory_size = 1
      }
    end
  end
end
If you need to reach me, message me on discord.

I make qol mods. Check them out, maybe.
https://mods.factorio.com/user/protocol_1903
If you have a mod idea, I can look into it.

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

Re: Trying to check type of energy_interface

Post by Pi-C »

protocol_1903 wrote:
Sat Dec 23, 2023 8:09 am
I'm trying to run the following code to check if a machine uses electricity, then change it to burner once that is done. It loads into Factorio but for whatever reason it isn't working. It is probably something wrong with the syntax, I'm new to LUA. I think its something with how I'm checking the source type.
Your syntax is OK, but your data isn't.

Code: Select all

for _, machine in pairs(data.raw) do
	…
end
You don't want to check everything below data.raw, but only the prototypes of the type "assembling-machine". This should work:

Code: Select all

for _, machine in pairs(data.raw["assembling-machine"]) do
	…
end
By the way, I've always found it helpful for debugging to log the value of the loop variables. This would have shown you that you'd tried to operate on the list of prototype names, not the prototypes themselves.
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

protocol_1903
Long Handed Inserter
Long Handed Inserter
Posts: 98
Joined: Fri Sep 09, 2022 4:33 pm
Contact:

Re: Trying to check type of energy_interface

Post by protocol_1903 »

You don't want to check everything below data.raw, but only the prototypes of the type "assembling-machine".
I want to change every entity possible, not just every assembling machine.
By the way, I've always found it helpful for debugging to log the value of the loop variables. This would have shown you that you'd tried to operate on the list of prototype names, not the prototypes themselves.
How do you do that, and where do you access the log?
If you need to reach me, message me on discord.

I make qol mods. Check them out, maybe.
https://mods.factorio.com/user/protocol_1903
If you have a mod idea, I can look into it.

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

Re: Trying to check type of energy_interface

Post by Pi-C »

protocol_1903 wrote:
Sat Dec 23, 2023 1:22 pm
You don't want to check everything below data.raw, but only the prototypes of the type "assembling-machine".
I want to change every entity possible, not just every assembling machine.
This should do the trick:

Code: Select all

for t, types in pairs(data.raw) do
	for p, prototype in pairs(types) do
		log(string.format("p: %s\tprototype.type: %s\tprototype.energy_source: %s", p, p.type, serpent.line(prototype.energy_source)))	
		if prototype.energy_source and prototype.energy_source.type == "electric" then
			prototype.energy_source = {
				type = "burner",
			        effectivity = 0.5,
			        fuel_inventory_size = 1
			}
		end
	end
end
By the way, I've always found it helpful for debugging to log the value of the loop variables. This would have shown you that you'd tried to operate on the list of prototype names, not the prototypes themselves.
How do you do that, and where do you access the log?
I've added a logging instruction above. In case you wonder why I've logged just "prototype.type" instead of "prototype": prototype definitions may be huge, and if you loop over all entities in data.raw, the log file will grow so big that it is rather hard to read and, therefore, useless.

I'm using the stand-alone version of Factorio. There, the log file is factorio-current.log in the folder where I've unpacked the game. You can find more detailed info if you check the wiki for Application directory.
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

protocol_1903
Long Handed Inserter
Long Handed Inserter
Posts: 98
Joined: Fri Sep 09, 2022 4:33 pm
Contact:

Re: Trying to check type of energy_interface

Post by protocol_1903 »

Sorry for the late reply, but your advice worked! Thanks for the help! I used this to develop the Powerless mod.
If you need to reach me, message me on discord.

I make qol mods. Check them out, maybe.
https://mods.factorio.com/user/protocol_1903
If you have a mod idea, I can look into it.

Post Reply

Return to “Modding help”