Page 1 of 1

Devilishness with a code

Posted: Sun Nov 24, 2024 6:33 pm
by Keysivi
I have some kind of devilry going on with the code.

I'm trying to remove the unlocking of recipes in the game's technologies and other mods.

I use a special code for this. Example:

Code: Select all

	if data.raw.technology["explosive-rocketry"] then
		for i = 1, #data.raw.technology["explosive-rocketry"].effects do
			effect = data.raw.technology["explosive-rocketry"].effects[i]
			if effect.type == "unlock-recipe" and effect.recipe == "flamethrower-ammo" then
				index = i
			end
		end

		table.remove(data.raw.technology["explosive-rocketry"].effects, index)
	end
With vanilla technologies it works quite well... But with mods some devilry starts!!!

For example, in the chemical-rocket mod https://mods.factorio.com/mod/chemical-rocket/ it says:

Code: Select all

local technology = data.raw.technology["explosive-rocketry"]
table.insert(technology.effects, {type = "unlock-recipe", recipe = "chemical-rocket"})
table.insert(technology.effects, {type = "unlock-recipe", recipe = "incendiary-rocket"})
table.insert(technology.effects, {type = "unlock-recipe", recipe = "flamethrower-ammo"})
I'm trying to delete these recipes:

Code: Select all

if mods["chemical-rocket"] then

	if data.raw.technology["explosive-rocketry"] then
		for i = 1, #data.raw.technology["explosive-rocketry"].effects do
			effect = data.raw.technology["explosive-rocketry"].effects[i]
			if effect.type == "unlock-recipe" and effect.recipe == "chemical-rocket" then
				index = i
			end
		end

		table.remove(data.raw.technology["explosive-rocketry"].effects, index)
	end
	
	if data.raw.technology["explosive-rocketry"] then
		for i = 1, #data.raw.technology["explosive-rocketry"].effects do
			effect = data.raw.technology["explosive-rocketry"].effects[i]
			if effect.type == "unlock-recipe" and effect.recipe == "incendiary-rocket" then
				index = i
			end
		end

		table.remove(data.raw.technology["explosive-rocketry"].effects, index)
	end
	
	if data.raw.technology["explosive-rocketry"] then
		for i = 1, #data.raw.technology["explosive-rocketry"].effects do
			effect = data.raw.technology["explosive-rocketry"].effects[i]
			if effect.type == "unlock-recipe" and effect.recipe == "flamethrower-ammo" then
				index = i
			end
		end

		table.remove(data.raw.technology["explosive-rocketry"].effects, index)
	end
	

end
But instead of deleting them, recipe = "explosive-rocket" is deleted

What the hell?""""!!!!!!!

Re: Devilishness with a code

Posted: Sun Nov 24, 2024 6:41 pm
by s6x
Are you using dependencies to make sure that your mod's code runs after the mods you want to modify?

That code you pasted will delete the last index it finds (whatever the value of i was) if it finds if it isn't able to match anything, and it won't find anything if the other mod's code hasn't run yet.

Re: Devilishness with a code

Posted: Sun Nov 24, 2024 6:49 pm
by Keysivi
For sure!

Here is the code from info.json :

Code: Select all

{
  "name": "My_add_pack_updated",
  "version": "0.0.4",
  "factorio_version": "2.0",
  "title": "All kinds of things (updated)",
  "author": "KeySiVi",
  "description": "Mod where I plan to add my own developments and forks",
  "dependencies": [
	"? NapalmArtillery",
    "? chemical-rocket",
	"? atomic-artillery-reborn",
	"! flamethrower-fluid"
  ] 
}

Re: Devilishness with a code

Posted: Sun Nov 24, 2024 6:56 pm
by Keysivi
This is the second mod with which such a hassle...

The first was flamethrower-fluid

There the technologies were written in such a way:

Code: Select all

for _,t in pairs(data.raw.technology) do
	for _,e in pairs(t.effects or {}) do
		if e.type == "unlock-recipe" then
			if e.recipe == "flamethrower-ammo" then
				table.insert(t.effects,{type = "unlock-recipe",recipe = "flamethrower-fuel"})
				table.insert(t.effects,{type = "unlock-recipe",recipe = "improved-flamethrower-ammo"})
			end
		end
	end
end
I suffered and suffered with it, and then simply copied it into my mod...

Re: Devilishness with a code

Posted: Sun Nov 24, 2024 8:29 pm
by s6x
Ok, you're doing this in data.lua, right?

The issue is similar to dependencies, but a little bit trickier. I took a look at chemical-rockets and I noticed it adds its recipes in data-updates.lua. So if your code is in data.lua, then it's ultimately the same problem; chemical-rockets hasn't run its code yet so your code won't see the recipes added. This may be why some mods cause you problems and others don't.

Re: Devilishness with a code

Posted: Mon Nov 25, 2024 2:10 am
by Keysivi
It turns out that I also need to place my code in data-updates.lua?

Re: Devilishness with a code

Posted: Mon Nov 25, 2024 2:16 am
by robot256
Since all you are doing is modifying existing technologies and recipes, it would be appropriate to put it in data-final-fixes.

Typically, we use the data phases like this:

data.lua = Make completely new prototypes for entities, items, recipes, technologies, etc.
data-updates.lua = Make new prototypes that are copied from or depend on existing prototypes, and make major modifications to existing prototypes. Use mod dependencies if you need to run after another mod's data-updates.
data-final-fixes.lua = Modify parameters of existing prototypes for consistency and compatibility, without creating any new prototypes, knowing that you will be able to manipulate every prototype that is likely to be added by any mod.

Re: Devilishness with a code

Posted: Mon Nov 25, 2024 3:21 am
by Keysivi
s6x wrote: Sun Nov 24, 2024 8:29 pm Ok, you're doing this in data.lua, right?

The issue is similar to dependencies, but a little bit trickier. I took a look at chemical-rockets and I noticed it adds its recipes in data-updates.lua. So if your code is in data.lua, then it's ultimately the same problem; chemical-rockets hasn't run its code yet so your code won't see the recipes added. This may be why some mods cause you problems and others don't.
Moved my code to data-updates.lua

It worked!!!... The problem is really in this

Thank you very much!!! I already thought that I was going crazy...

Re: Devilishness with a code

Posted: Mon Nov 25, 2024 3:23 am
by Keysivi
robot256 wrote: Mon Nov 25, 2024 2:16 am Since all you are doing is modifying existing technologies and recipes, it would be appropriate to put it in data-final-fixes.

Typically, we use the data phases like this:

data.lua = Make completely new prototypes for entities, items, recipes, technologies, etc.
data-updates.lua = Make new prototypes that are copied from or depend on existing prototypes, and make major modifications to existing prototypes. Use mod dependencies if you need to run after another mod's data-updates.
data-final-fixes.lua = Modify parameters of existing prototypes for consistency and compatibility, without creating any new prototypes, knowing that you will be able to manipulate every prototype that is likely to be added by any mod.
Thank you very much for the explanation! I'll try to do as you suggested.

Re: Devilishness with a code

Posted: Mon Nov 25, 2024 3:27 am
by EustaceCS
(unless the tech you modify also appears during final-fixes processing and failed to be caught by load order generated through dependancies tree.
Depending on what specific goal you have in mind, you might consider moving your removal code to runtime stage.
As far as I'm aware of, you can't simply make your mod load last on some specific stage.
If you're looking into removing things indiscriminately by recipe name rather than by mod x recipe combination, https://lua-api.factorio.com/latest/index-runtime.html is da wei)

Re: Devilishness with a code

Posted: Mon Nov 25, 2024 6:33 am
by Keysivi
EustaceCS wrote: Mon Nov 25, 2024 3:27 am (unless the tech you modify also appears during final-fixes processing and failed to be caught by load order generated through dependancies tree.
Depending on what specific goal you have in mind, you might consider moving your removal code to runtime stage.
As far as I'm aware of, you can't simply make your mod load last on some specific stage.
If you're looking into removing things indiscriminately by recipe name rather than by mod x recipe combination, https://lua-api.factorio.com/latest/index-runtime.html is da wei)
Thank you very much! Unfortunately, for me, this is the highest magic....

Re: Devilishness with a code

Posted: Mon Nov 25, 2024 12:29 pm
by EustaceCS
Fret not, for this game's modkit is VERY user-friendly.
If in doubt, make changes in your mod's files, load the game with these and observe.
If something went sour (read: outright crashed or tried to do something VERY unintended) - the game will offer you to re-start loading process (among other things). Which you can do after altering your mod's files. And repeat as necessary.
(bonus points for hella exhaustive error message describing which specific code piece is guilty of what happened)
In other words, doing everything byte-perfect is not necessary, you have convenient means for debugging most spectacular problems on the go!