Jump to content

Overriding containers.widgetsetup(...) from containers.lua in your mod


Recommended Posts

So it would seem that containers.widgetsetup(...) has changed at some point in the last few months. It gained a third parameter, making things a bit easier on modders who want to have custom containers. Unfortunately, a lot of modders have overridden this function in such a way that it only accepts and carries through the first two parameters.

 

Luckily lua gives us a better way to override base functions so we can better ensure our mods don't break future mods.

 

When overriding this function (or any function):

function containers.widgetsetup(container, prefab, data) -- Original code in hereend
 
We should be doing the following:
local oldwidgetsetup = containers.widgetsetupcontainers.widgetsetup = function(container, prefab, data, ...)    if container.inst.prefab == "my_awesome_new_container" or prefab == "my_awesome_new_container" then       data = my_awesome_new_container_data    end    return oldwidgetsetup(container, prefab, data, ...)end

Adding ... to the end of the param list and carrying that through to the return future proofs your override and insures that all params will be passed through. In fact, you only need to list out the params you're using and ... the rest. If you're not using any of the params you can just do my_method(...) return original_method(...) and you never have to worry about the param count.

 

The problem I've run into is that a lot of folks have overriden it this way:

local oldwidgetsetup = containers.widgetsetupcontainers.widgetsetup = function(container, prefab)    if container.inst.prefab == "my_awesome_new_container" or prefab == "my_awesome_new_container" then       -- Something happens here that modifies container directly    end    oldwidgetsetup(container, prefab)end

 

The problem with the method above is that the addition of parameters in future API versions are dropped in your code. With the lua maigic of ,... added at the end of your param list, you future proof your overrides.

 

Unfortunately this error in older mods makes future mods look like the culprit, since the crash or malfunction happens when their code uses the new params but they're not passed through by your old mod code that didn't carry those new params forward.

 

This is also an acceptable override:

local oldwidgetsetup = containers.widgetsetupcontainers.widgetsetup = function(container, prefab, ...)    if container.inst.prefab == "my_awesome_new_container" or prefab == "my_awesome_new_container" then       -- Something happens here that modifies container directly    end    oldwidgetsetup(container, prefab, ...)end

Now, it's probably not super likely we'll see additional parameters added often, or that those additional parameters will be found and used, but in this case that is exactly what happened.

Edited by nickwest
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...