Jump to content

Recommended Posts

I'm adding a food storage feature to my building mod.

But it can store more than just food.

Snipaste_2025-03-20_20-24-12.png.6cb36db340bd091b2476805f6b33b9f6.png

In my tests, some items can't be stored, such as Nightmare Fuel("nightmarefuel").

But most things can be stored.

Here are the key code snippets, and the full source code is available in this repo: https://github.com/yanghao5/painmagnet/tree/dev.

prefabs/painmagnet.lua

    inst:AddComponent("container")
    inst.components.container:WidgetSetup("painmagnet_storage")
    inst.components.container.onopenfn = function(inst)
        inst.SoundEmitter:PlaySound("dontstarve/wilson/chest_open")
    end
    inst.components.container.onclosefn = function(inst)
        inst.SoundEmitter:PlaySound("dontstarve/wilson/chest_close")
    end

modmain.lua

local containers = require("containers")
...

local params = {
    widget = {
        slotpos = {},
        animbank = "ui_chest_4x5",
        animbuild = "ui_chest_4x5",
        pos = Vector3(0, 200, 0),
        side_align_tip = 160,
    },
    type = "chest",
    itemtestfn = function(container, item, slot) 
        return item.components.edible ~= nil
    end
}

for y = 3, 0, -1 do -- slot_y = 3
    for x = 0, 4 do -- slot_x = 4
        table.insert(params.widget.slotpos, Vector3(80 * x - 346 * 2 + 90, 80 * y - 100 * 2 + 130, 0))
    end
end

containers.params.painmagnet_storage = params

containers.MAXITEMSLOTS = math.max(containers.MAXITEMSLOTS, #params.widget.slotpos)

Does anyone know how to fix it?

@yanecc 

Thanks!

Your condition filters out more items, but some non-food items can still be stored.

Snipaste_2025-03-21_12-21-16.png.1f6d5f9edbc78fb50aeb4b30fbee5fec.png

Your code gave me a clue.

I tried the following code, everything is fine now.

    itemtestfn = function(container, item, slot) 
        if item.components.edible then
            local foodtypes = item.components.edible.foodtype
            if type(foodtypes) == "table" then
                for _, ft in pairs(foodtypes) do
                    if ft == FOODTYPE.MEAT or ft == FOODTYPE.VEGGIE or ft == FOODTYPE.FRUIT or ft == FOODTYPE.FISH then
                        return true
                    end
                end
            end
        end
        return false
    end
  • GL Happy 1

update itemtestfn

itemtestfn = function(container, item, slot) 
  if item.components.edible then
    if  item.components.edible.foodtype == FOODTYPE.MEAT or item.components.edible.foodtype==FOODTYPE.VEGGIE then
      return true
    end
  end
  return false
end

 

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
×
  • Create New...