Jump to content

How to set up widget/container? Help please


Recommended Posts

Hi, I was wondering if it's possible to put a widget setup in its own file instead of putting it on modmain.lua?

Here is the example I'm talking about:

--------------------------------------------------------------------------
--[[ compost_box ]]
--------------------------------------------------------------------------

_G = GLOBAL
local params={} 
params.compost_box =
{
    widget =
    {
        slotpos = {},
        animbank = "ui_chest_3x3",
        animbuild = "ui_chest_3x3",
        pos = _G.Vector3(0, 200, 0),
        side_align_tip = 160,
    },
    type = "chest",
}

    for y = 2, 0, -1 do
        for x = 0, 2 do
        table.insert(params.compost_box.widget.slotpos, _G.Vector3(80 * x - 80 * 2 + 80, 80 * y - 80 * 2 + 80, 0))
    end
end

function params.compost_box.itemtestfn(container, item, slot)
	return (item.components.edible and item.components.perishable) or
	       item.prefab == "spoiled_food" or	
	       item.prefab == "rottenegg" or
	       item.prefab == "guano" or 
	       item.prefab == "poop" or 		   
           item:HasTag("fresh") or 
	       item:HasTag("stale") or
	       item:HasTag("spoiled")
end

local containers = _G.require "containers"
containers.MAXITEMSLOTS = math.max(containers.MAXITEMSLOTS, params.compost_box.widget.slotpos ~= nil and #params.compost_box.widget.slotpos or 0)
local old_widgetsetup = containers.widgetsetup
function containers.widgetsetup(container, prefab, data)
        local pref = prefab or container.inst.prefab
        if pref == "compost_box" then
                local t = params[pref]
                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)
                end
        else
                return old_widgetsetup(container, prefab)
    end
end

--------------------------------------------------------------------------
--[[ crate_wooden ]]
--------------------------------------------------------------------------

params.crate_wooden =
{
    widget =
    {
        slotpos = {},
        animbank = "ui_chest_5x12",
        animbuild = "ui_chest_5x12",
        pos = _G.Vector3(90, 220, 0),
        side_align_tip = 160,
    },
    type = "chest",
}

    for y = 4, 0, -1 do
        for x = 0, 11 do
        table.insert(params.crate_wooden.widget.slotpos, _G.Vector3(80 * x - 346 * 2 + 98, 80 * y - 100 * 2 + 42, 0))
    end
end

function params.crate_wooden.itemtestfn(container, item, slot)
	if item.prefab == "chester_eyebone" then    
		return false    
	end    
	    return true    
end

local containers = _G.require "containers"
containers.MAXITEMSLOTS = math.max(containers.MAXITEMSLOTS, params.crate_wooden.widget.slotpos ~= nil and #params.crate_wooden.widget.slotpos or 0)
local old_widgetsetup = containers.widgetsetup
function containers.widgetsetup(container, prefab, data)
        local pref = prefab or container.inst.prefab
        if pref == "crate_wooden" then
                local t = params[pref]
                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)
                end
        else
                return old_widgetsetup(container, prefab)
    end
end

Would it be possible to somehow put it in its own separate file, so that it doesn't take up space in the modmain.lua file? Maybe using modimport or something else? Also if you know of any way to shorten the code above so that it's more compact that would be appreciated. Thank you.

Link to comment
Share on other sites

On 15/03/2017 at 8:52 PM, Luis95R said:

Would it be possible to somehow put it in its own separate file, so that it doesn't take up space in the modmain.lua file?

Sorry for the late answer, yes, it's possible. In modmain, put :

        modimport("scripts/custom_containers.lua")


And in the scripts folder, create a "custom_containers.lua" file, put the code into that file.
 

Sorry can't help for the "shorten" part.

 

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