Sign in to follow this  
RadiantRoma

Custom container doesn't automatically get items

Recommended Posts

RadiantRoma    386

I made a custom backpack, with it's own custom widget. It works, you can store items in it and everything, the only problem is items don't automatically go into it. Like if my inventory is full, and the bag is empty and you try to pick something up, its supposed to go into the backpack, but it doesn't in my case. I might have missed a line while making the widget in modmain. Or something. I don't know. 

Here's the code I used to create the widget

Spoiler


local containers = GLOBAL.require("containers")
local Vector3 = GLOBAL.Vector3
local params={}
params.sleepcap =
{
    widget =
    {
        slotpos =
        {
            Vector3(-37.5, 32 + 4, 0), 
            Vector3(37.4, 32 + 4, 0),
            Vector3(-37.5, -(32 + 4), 0), 
            Vector3(37.5, -(32 + 4), 0),
        },
        animbank = "ui_sleepcap_2x2",
        animbuild = "ui_sleepcap_2x2",
        pos = Vector3(-215, -260, 0),
        side_align_tip = 120,
    },
    issidewidget = true,
    type = "pack",
}
local containers = require "containers"
containers.MAXITEMSLOTS = math.max(containers.MAXITEMSLOTS, params.sleepcap.widget.slotpos ~= nil and #params.sleepcap.widget.slotpos or 0)
local old_widgetsetup = containers.widgetsetup
function containers.widgetsetup(container, prefab, data)
    local pref = prefab or container.inst.prefab
    if pref == "sleepcap" 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

 

And just in case, I'll attach the entire mod to this thread. (It's kinda big, because it's an entire character mod :) )

But I'll expect this will be an easy fix, if there is one.

Wisniowski.zip

Share this post


Link to post
Share on other sites
RadiantRoma    386

I found this code in actions.lua, it mentions having two backpacks at once but I'm not sure what to do with this information...

Spoiler

ACTIONS.PICKUP.fn = function(act)
    if act.doer.components.inventory ~= nil and
        act.target ~= nil and
        act.target.components.inventoryitem ~= nil and
        (act.target.components.inventoryitem.canbepickedup or
        (act.target.components.inventoryitem.canbepickedupalive and not act.doer:HasTag("player"))) and
        not (act.target:IsInLimbo() or
            (act.target.components.burnable ~= nil and act.target.components.burnable:IsBurning()) or
            (act.target.components.projectile ~= nil and act.target.components.projectile:IsThrown())) then

        act.doer:PushEvent("onpickupitem", { item = act.target })

        --special case for trying to carry two backpacks
        if not act.target.components.inventoryitem.cangoincontainer and act.target.components.equippable and act.doer.components.inventory:GetEquippedItem(act.target.components.equippable.equipslot) then
            local item = act.doer.components.inventory:GetEquippedItem(act.target.components.equippable.equipslot)
            if item.components.inventoryitem and item.components.inventoryitem.cangoincontainer then
                
                --act.doer.components.inventory:SelectActiveItemFromEquipSlot(act.target.components.equippable.equipslot)
                act.doer.components.inventory:GiveItem(act.doer.components.inventory:Unequip(act.target.components.equippable.equipslot))
            else
                act.doer.components.inventory:DropItem(act.doer.components.inventory:GetEquippedItem(act.target.components.equippable.equipslot))
            end
            act.doer.components.inventory:Equip(act.target)
            return true
        end

        if act.doer:HasTag("player") and act.target.components.equippable and not act.doer.components.inventory:GetEquippedItem(act.target.components.equippable.equipslot) then
            act.doer.components.inventory:Equip(act.target)
        else
            act.doer.components.inventory:GiveItem(act.target, nil, act.target:GetPosition())
        end
        return true
    end
end

 

 

Share this post


Link to post
Share on other sites
Aquaterion    821

The issue is that the container item isn't worn in the body slot. The inventory code only checks for containers worn there.

To make it apply for headslot too:

--modmain.lua
AddComponentPostInit("inventory", function(self)
    local oldGetOverflow = self.GetOverflowContainer
    function self:GetOverflowContainer()
        if self.ignoreoverflow then
            return
        end
		
        local item = self:GetEquippedItem(EQUIPSLOTS.HEAD)	
        return item ~= nil and item.components.container or oldGetOverflow(self)
    end
end)

However, I'm pretty sure that equipping containers in both head and body slots at the same time will result in only the container in the head slot being able to auto receive items

Edited by Aquaterion

Share this post


Link to post
Share on other sites
RadiantRoma    386
3 hours ago, Aquaterion said:

However, I'm pretty sure that equipping containers in both head and body slots at the same time will result in only the container in the head slot being able to auto receive items

You're 100% correct. Maybe there's a way the function could check if there are two containers equipped, but one is full and so the other one then receives the overflow?

Share this post


Link to post
Share on other sites
Aquaterion    821
8 hours ago, Cherryzion said:

You're 100% correct. Maybe there's a way the function could check if there are two containers equipped, but one is full and so the other one then receives the overflow?

that would then create the issue that if u have a container with all slots having an item, but not all at full stack, an item that could have combined with one of those stacks would instead go into the other container.

 

The only way I see it happening right is if you modify all the functions where the overflow function is being called.

Edited by Aquaterion

Share this post


Link to post
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
Sign in to follow this