Jump to content

Recommended Posts

Hi again. My adventure with container continues. Thanks to people here i managed to make a container.

Now i would like to filter items going in this container. Since i would like to have only wood i tried this :

function params.log_shelter.itemtestfn(container, item, slot)

    -- if item.components.edible.foodtype == FOODTYPE.WOOD then
        -- return true
    -- end
    if item:HasTag("edible_FOODTYPE.WOOD") then
        return true
    end
    
    return false
end

(The commented function was my first try) but it's not working. How could i achieve that ?

I would like to at least accept log, but if possible, all type of "wood" items (log, boards, living log) would be great. Thanks for any help :)

With or without global i have a "attempt to index field 'edible' (a nil value)", so the problem seems to be with the "item.components.edible.foodtype", but i don't know why it's nil. Should i test first if the item have the component ?

 

Edit : it's strange, some objects works without mistake (like log (could put it in) and charcoal (can't put it in)) and some make the game crash (here it was a juicy berry bush) so i guess it's crashing when the item have no edible component ?...

Edited by Lumina

Yes, you should definitely test for the edible component. Not all items have it.

if item.components.edible and item.components.edible.foodtype == GLOBAL.FOODTYPE.WOOD then
    return true
end

Assuming item will never be nil, or you'll want to test it too.

Is there a case when item could be nil ? I took the icebox test as example and i don't see a test like this :


function params.icebox.itemtestfn(container, item, slot)
    if item:HasTag("icebox_valid") then
        return true
    end

    --Perishable
    if not (item:HasTag("fresh") or item:HasTag("stale") or item:HasTag("spoiled")) then
        return false
    end

    --Edible
    for k, v in pairs(FOODTYPE) do
        if item:HasTag("edible_"..v) then
            return true
        end
    end

    return false
end

So i don't see in which case trying to put item in a container could result in a nil item ?

 

(Just curious, not really need help here :D )

Even if you managed to force a case where there was no item, would it make sense? The function is called itemtestfn, after all. No, I just meant that it's a good idea to test anything and everything that can break. There are other cases where you do want to test if the prefab exists.

I have a strange problem i don't really understand. If i am the host (server without cave), i could put items in the container in a classic way without problem (putting them in slot or using the "fast" shortcut). If i am not the host, i cannot do this. The only way to put item in the container is to click on the container itself and it will add the item in the slots, but i cannot directly add items in the slots i want.

The code i use is based on the icebox code, so i don't see why icebox is working fine and why my code isn't. If anyone have a clue ?

I remember the Waiter mod got a similar issue (at least close), with ingredients you couldn't put in crockpot, but the issue was probably different (ingredient side rather than crockpot side i guess).

After look and test, the only difference i can see, since the icebox code works fine, is that my custom code itself, even is fine, could be problematic, maybe because it use component (seems to be the case since usually, if it works fine at host but not in cave/dedi, it's because of components). I'll maybe try a workaround adding tag to the items i want being able to add to the container.


    for k, v in pairs(FOODTYPE) do
        if item:HasTag("edible_"..v) then
            return true
        end
    end

But this part of the code seems to "transform" a foodtype (which is linked to component) in a kind of tag. I don't understand how it works, but could be a way to achieve it...

Edited by Lumina

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