Prototype "inheritance"

Place to post guides, observations, things related to modding that are not mods themselves.
User avatar
zx64
Long Handed Inserter
Long Handed Inserter
Posts: 58
Joined: Fri Dec 16, 2016 3:57 pm
Contact:

Prototype "inheritance"

Post by zx64 »

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.

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
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:

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"
})
Of course, I could have missed something and this is all unnecessary or breaks in unfortunate ways with more complex use. :-)
Post Reply

Return to “Modding discussion”