Jump to content

(solved) how to edit an existing container?


Recommended Posts

hello, i need help how to edit a container cause i have no idea what im doing

 

for example i want to edit saltbox and make it accept basically almost all food so this is the original code in container.lua

params.saltbox = deepcopy(params.icebox)

function params.saltbox.itemtestfn(container, item, slot)
	return ((item:HasTag("fresh") or item:HasTag("stale") or item:HasTag("spoiled"))
		and item:HasTag("cookable")
		and not item:HasTag("deployable")
		and not item:HasTag("smallcreature")
		and item.replica.health == nil)
		or item:HasTag("saltbox_valid")
end

and i edit it to this and put it in modmain

params.saltbox = GLOBAL.deepcopy(params.icebox)

function params.saltbox.itemtestfn(container, item, slot)
	return (item:HasTag("fresh") or item:HasTag("stale") or item:HasTag("spoiled"))
		or item:HasTag("saltbox_valid")
end

it doesn't work and I'm clueless what to do I never messed with containers before and there doesnt seem to be any mods on the workshop which edit existing containers

Edited by --- -.-
Link to comment
Share on other sites

1 hour ago, --- -.- said:

params.saltbox = GLOBAL.deepcopy(params.icebox) function params.saltbox.itemtestfn(container, item, slot) return (item:HasTag("fresh") or item:HasTag("stale") or item:HasTag("spoiled")) or item:HasTag("saltbox_valid") end

So this is the only thing in your modmain? "params" is a variable from the containers file, which you'll need to reference if you want to edit containers.

Edited by penguin0616
  • Like 1
Link to comment
Share on other sites

On 3/7/2021 at 8:45 PM, penguin0616 said:

So this is the only thing in your modmain? "params" is a variable from the containers file, which you'll need to reference if you want to edit containers.

this is the whole thing rn atm and it crashes instantly upon loading the game with no log

Spoiler

local containers					= GLOBAL.require("containers")
local params 						= {}
local OVERRIDE_WIDGETSETUP			= false
local containers_widgetsetup_base	= containers.widgetsetup

function containers.widgetsetup(container, prefab, ...)
	local t = params[prefab or container.inst.prefab]
	if t ~= nil then
		for k, v in pairs(t) do
			container[k] = v
		end
		container:SetNumSlots(container.widget.slotpos ~= nil and #container.widget.slotpos or 0)
	else
		containers_widgetsetup_base(container, prefab, ...)
	end
end

params.saltbox = GLOBAL.deepcopy(params.icebox)
function params.saltbox.itemtestfn(container, item, slot)
	return (item:HasTag("fresh") or item:HasTag("stale") or item:HasTag("spoiled"))
		or item:HasTag("saltbox_valid")
end

for k, v in pairs(params) do
	containers.MAXITEMSLOTS = math.max(containers.MAXITEMSLOTS, v.widget.slotpos ~= nil and #v.widget.slotpos or 0)
end
local containers_widgetsetup_custom = containers.widgetsetup
local MAXITEMSLOTS = containers.MAXITEMSLOTS
AddPrefabPostInit("world_network", function(inst)
	if containers.widgetsetup ~= containers_widgetsetup_custom then
		OVERRIDE_WIDGETSETUP = true
		local containers_widgetsetup_base2 = containers.widgetsetup
		function containers.widgetsetup(container, prefab, ...)
			containers_widgetsetup_base2(container, prefab, ...)
		end
	end
	if containers.MAXITEMSLOTS < MAXITEMSLOTS then
		containers.MAXITEMSLOTS = MAXITEMSLOTS
	end
end)

 

Link to comment
Share on other sites

I'm not sure what the crash would be, but you don't want to do this on a PrefabPostInit, especially on world_network. Your container changes might not get set up in time, and you additionally override the widgetsetup more than once.
A side issue is that you're unnecessarily reinitializing the saltbox.

I would just recommend your modmain to be something like this (albeit it will only work on the beta until the QoL update is released). That way, you can skip having to reimplement container stuff.

local containers = require("containers")

function containers.params.saltbox.itemtestfn(container, item, slot)
	return (item:HasTag("fresh") or item:HasTag("stale") or item:HasTag("spoiled"))
		or item:HasTag("saltbox_valid")
end

 

Though if you're doing this on the live branch at the moment, you'll need to get the params upvalue first.

CarlZalph provides a good example of that here:

 

Edited by penguin0616
  • Like 1
  • Wavey 1
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...