Page 1 of 1

Dynamic prerequisites

Posted: Mon Aug 31, 2020 9:46 pm
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.

Re: Dynamic prerequisites

Posted: Mon Aug 31, 2020 10:11 pm
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?

Re: Dynamic prerequisites

Posted: Mon Aug 31, 2020 10:39 pm
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.

Re: Dynamic prerequisites

Posted: Mon Aug 31, 2020 10:47 pm
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.

Re: Dynamic prerequisites

Posted: Mon Aug 31, 2020 11:14 pm
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.

Re: Dynamic prerequisites

Posted: Mon Aug 31, 2020 11:23 pm
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*.

Re: Dynamic prerequisites

Posted: Tue Sep 01, 2020 12:07 am
by sparr

Code: Select all

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