Dynamic prerequisites

Place to get help with not working mods / modding interface.
PyroGamer666
Burner Inserter
Burner Inserter
Posts: 16
Joined: Fri Aug 28, 2020 5:37 am
Contact:

Dynamic prerequisites

Post by PyroGamer666 »

I am currently developing a mod with a tech tree that precedes the vanilla tech tree. The last technology in my tech tree, with the internal name "iron-axe", should be the prerequisite to all technologies that normally do not have any prerequisites. In order to accomplish this, I wrote the following code before adding my mod's technologies to data in the same file.

Code: Select all

for key,tech in ipairs(table.deepcopy(data.raw["technology"])) do
    if tech.prerequisites==nil or tech.prerequisites=={} then
        tech.prerequisites={"iron-axe"}
        data:extend{tech}
    end
    
end
However, this code fails to add the prerequisite to any technologies. If someone knows how to fix this, I would like to know.
Qon
Smart Inserter
Smart Inserter
Posts: 2164
Joined: Thu Mar 17, 2016 6:27 am
Contact:

Re: Dynamic prerequisites

Post by Qon »

You shouldn't copy things you want to modify, then you are modifying a copy. And you don't extend things already in data. Since it's an update the code should be in data-updates.lua, I'm pretty sure. But maybe that's not necessary for modificatiosn to vanilla prototypes?
My mods: Capsule Ammo | HandyHands - Automatic handcrafting | ChunkyChunks - Configurable Gridlines
Some other creations: Combinassembly Language GitHub w instructions and link to run it in your browser | 0~drain Laser
User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3724
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Dynamic prerequisites

Post by DaveMcW »

Code: Select all

for key,tech in pairs(data.raw["technology"]) do
    if tech.prerequisites==nil or tech.prerequisites=={} then
        tech.prerequisites={"iron-axe"}
    end
end
If you are only modifying vanilla recipes you can use data.lua with a dependency on base.
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Dynamic prerequisites

Post by eradicator »

PyroGamer666 wrote: Mon Aug 31, 2020 9:46 pm

Code: Select all

tech.prerequisites=={} 
This does not do what you want, lua tables have identity

Code: Select all

> print({} == {})
false
You could try table_size()==0.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
PyroGamer666
Burner Inserter
Burner Inserter
Posts: 16
Joined: Fri Aug 28, 2020 5:37 am
Contact:

Re: Dynamic prerequisites

Post by PyroGamer666 »

DaveMcW wrote: Mon Aug 31, 2020 10:39 pm

Code: Select all

for key,tech in pairs(data.raw["technology"]) do
    if tech.prerequisites==nil or tech.prerequisites=={} then
        tech.prerequisites={"iron-axe"}
    end
end
If you are only modifying vanilla recipes you can use data.lua with a dependency on base.
This code does exactly what I was trying to do, thank you.
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Dynamic prerequisites

Post by eradicator »

PyroGamer666 wrote: Mon Aug 31, 2020 11:14 pm
DaveMcW wrote: Mon Aug 31, 2020 10:39 pm

Code: Select all

for key,tech in pairs(data.raw["technology"]) do
    if tech.prerequisites==nil or tech.prerequisites=={} then
        tech.prerequisites={"iron-axe"}
    end
end
If you are only modifying vanilla recipes you can use data.lua with a dependency on base.
This code does exactly what I was trying to do, thank you.
No it doesn't tech.prerequisites=={} is *always false*.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
sparr
Smart Inserter
Smart Inserter
Posts: 1463
Joined: Fri Feb 14, 2014 5:52 pm
Contact:

Re: Dynamic prerequisites

Post by sparr »

Code: Select all

table.compare(tech.prerequisites,{})
#tech.prerequisites == 0
table_size(tech.prerequisites) == 0
...
Post Reply

Return to “Modding help”