Jump to content

[Modding-Help] Add Button to Chest Widget


Recommended Posts

EDIT: I have solved my problem by using code of the following mod: Pickle It

 

Hello guys,

I just started modding and to train up and get to know the environment my first goal was to mod a chest that is self sorting. I am an experienced programmer (even though i only worked with C#/Java/PL-SQL before), but I cant really get the hang of LUA and how the different files work together. So far I have read the pinned guides, but they couldnt help me.

 

Enough chitchat here is my Problem:

I have a prefab which is called "sorter". Its basically the same code as in a treasurechest. Now I want to add an button to the chests widget. The button should be called "sort" and call an action that is defined in the modmain.lua. So far so good. I followed this Thread to add an button to my prefab. But i cant get it to work. The mod itself works, the game doesnt crash, but there is no button. no matter what. I've been trying for the last few hours to produce the button, but no matter what i googled and searched in this Forum i wasnt able to find a solution. 

You can find my source_code in the attached ZIP. For those who are too lazy to download the zip ill add the code as quote below this post.

 

I hope you can help me and that the description of my problem was sufficient.

Thank you,

Narmor

 

sorter.lua:

require "prefabutil"

local assets=
{
	Asset("ANIM", "anim/sorter.zip"),
}

local function onopen(inst)
	inst.SoundEmitter:PlaySound("dontstarve/wilson/chest_open")
    inst.AnimState:PlayAnimation("open")
end

local function onclose(inst)
	inst.AnimState:PlayAnimation("closed")
	inst.SoundEmitter:PlaySound("dontstarve/wilson/chest_close")
end

local function onhammered(inst, worker)
	inst.components.lootdropper:DropLoot()
	inst.components.container:DropEverything()
	inst.SoundEmitter:PlaySound("dontstarve/common/destroy_metal")
	inst:Remove()
end

local function onhit(inst, worker)
	inst.AnimState:PlayAnimation("closed")
	inst.components.container:DropEverything()
	inst.components.container:Close()
end

local function onbuilt(inst)
	inst.AnimState:PlayAnimation("place")
	inst.AnimState:PushAnimation("closed")
end

local function itemtest(inst, item, slot)
	return true
end

local function EditContainer(inst)
    local self
    if TheWorld.ismastersim then
        self = inst.components.container
    else
        self = inst.replica.container
    end

    self:WidgetSetup("sorter")

    local function sortfn(inst)
        if TheWorld.ismastersim then
            BufferedAction(inst.components.container.opener, inst, ACTIONS.SORT):Do()
        else
            SendRPCToServer(RPC.DoWidgetButtonAction, ACTIONS.SORT.code, inst, ACTIONS.SORT.mod_name)
        end
    end

    local sort_buttoninfo = {
        text = "Sort",
        position = Vector3(0, -195, 0),
        fn = sortfn,
    }

	-- One buttoninfo per button
	self.widget.sorter_buttons = {
		sort_buttoninfo,
	}
end

local function fn(Sim)
	local inst = CreateEntity()

	inst:AddTag("structure")

    inst.entity:AddTransform()
	inst.entity:AddAnimState()
	inst.entity:AddSoundEmitter()
	inst.entity:AddNetwork()

	local minimap = inst.entity:AddMiniMapEntity()
	minimap:SetIcon( "sorter.tex" )

    inst.AnimState:SetBank("sorter")
    inst.AnimState:SetBuild("sorter")
    inst.AnimState:PlayAnimation("closed", true)

	inst.entity:SetPristine()

	if not TheWorld.ismastersim then
		local _OnEntityReplicated = inst.OnEntityReplicated
		inst.OnEntityReplicated = function(inst)
			if _OnEntityReplicated then
				_OnEntityReplicated(inst)
			end
			EditContainer(inst)
		end
		return inst
	end

	inst:AddComponent("inspectable")
    inst:AddComponent("container")
	inst.components.container.itemtestfn = itemtest
    inst.components.container.onopenfn = onopen
    inst.components.container.onclosefn = onclose

	EditContainer(inst)

    inst:AddComponent("lootdropper")
    inst:AddComponent("workable")
    inst.components.workable:SetWorkAction(ACTIONS.HAMMER)
    inst.components.workable:SetWorkLeft(5)
    inst.components.workable:SetOnFinishCallback(onhammered)
    inst.components.workable:SetOnWorkCallback(onhit)

    return inst
end

return Prefab( "common/sorter", fn, assets),
	MakePlacer("common/sorter_placer", "sorter", "sorter", "closed")

 

 

modmain.lua:

local Spot = GetModConfigData("Spot")

        PrefabFiles =
{
	    "sorter",
}

        Assets =
{
        Asset( "IMAGE", "minimap/sorter.tex" ),
        Asset( "ATLAS", "minimap/sorter.xml" ),
        Asset("ATLAS", "images/inventoryimages/ui_chest_3x4.xml"),
	    Asset("ATLAS", "images/inventoryimages/sorter.xml"),
}

        AddMinimapAtlas("minimap/sorter.xml")

        STRINGS = GLOBAL.STRINGS
        RECIPETABS = GLOBAL.RECIPETABS
        Recipe = GLOBAL.Recipe
        Ingredient = GLOBAL.Ingredient
        TECH = GLOBAL.TECH



        GLOBAL.STRINGS.NAMES.SORTER = "Chest sorter"

        STRINGS.RECIPE_DESC.SORTER = "Put in your problems and they will be sorted out"

        GLOBAL.STRINGS.CHARACTERS.GENERIC.DESCRIBE.SORTER = "I like it"

        local sorter = GLOBAL.Recipe("sorter",{ Ingredient("cutstone", 1)},
        RECIPETABS.TOWN, TECH.NONE, "sorter_placer" )
        sorter.atlas = "images/inventoryimages/sorter.xml"

-----------------------------------------------------------------------------------------------------------------------------------
-- widgetsetup for my prefab -- Not too shure what it does exactly, found it in another mod and it seems to work fine.
local sorter =
{
	widget =
	{
		slotpos = {},
		animbank = "ui_chest_3x3",
		animbuild = "ui_chest_3x3",
		pos = GLOBAL.Vector3(0, 200, 0),
		side_align_tip = 160,
	},
	type = "chest",
}

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

local containers = GLOBAL.require("containers")
containers.MAXITEMSLOTS = math.max(containers.MAXITEMSLOTS, sorter.widget.slotpos ~= nil and #sorter.widget.slotpos or 0)

local _widgetsetup = containers.widgetsetup
function containers.widgetsetup(container, prefab, data)
	local pref = prefab or container.inst.prefab
	if pref == "sorter" then
		for k, v in pairs(sorter) do
			container[k] = v
		end
		container:SetNumSlots(container.widget.slotpos ~= nil and #container.widget.slotpos or 0)
	else
		return _widgetsetup(container, prefab, data)
	end
end
-----------------------------------------------------------------------------------------------------------------------------------


-- Method for sorting the Stuff in the Sorter
local function SortFn(act)
    --Methods Stub to be implemented later
end
AddAction("SORT", "Sort", SortFn)

 

Sorter-Chest.rar

Edited by Narmor
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...