Prototype "inheritance"
Posted: Sun Dec 18, 2016 9:07 pm
I recently wanted to make a custom combinator and couldn't find a way to declare it in my data.lua without copy/pasting a bunch of values from the game's code when I only want to change the bare minimum.
Furthermore, I wanted to add my new item to the existing tech tree, again without wanting to copy/paste the existing tech tree item definition just to make my change.
This is just a first pass, I might revisit this idea to e.g. take a single table that has your new recipe, entity, item etc.
Example usage:
Of course, I could have missed something and this is all unnecessary or breaks in unfortunate ways with more complex use. :-)
Furthermore, I wanted to add my new item to the existing tech tree, again without wanting to copy/paste the existing tech tree item definition just to make my change.
Code: Select all
function extendproto(kind, baseclass, t)
local kinds = data.raw[kind]
-- table.deepcopy provided by Factorio
local derived = table.deepcopy(kinds[baseclass])
for k,v in pairs(t) do
derived[k] = v
end
kinds[derived.name] = derived
end
function extendprotolist(typename, classname, field, value)
local l = data.raw[typename][classname][field]
-- TODO: This only handles a single value
table.insert(l, value)
end
Example usage:
Code: Select all
extendproto("constant-combinator", "constant-combinator", {
name = "test-combinator",
})
extendproto("recipe", "constant-combinator", {
name = "test-combinator",
result = "test-combinator"
})
extendproto("item", "constant-combinator", {
name = "test-combinator",
place_result="test-combinator",
order = "b[combinators]-c[test-combinator]",
})
extendprotolist("technology", "circuit-network", "effects", {
type = "unlock-recipe",
recipe = "test-combinator"
})