Jump to content

Make survivor immediately eat food upon pick-up


Recommended Posts

Hey there!

I've started working on a new survivor. His main feature would be that he would immediately eat any kind of consumable upon pick-up. I would need some kind of reference for the coding, but I don't really know any kind of similar mechanic in the game like this. How could I approach this?

I don't expect a polished coding from you, I would be absolutely fulfilled with a pointing to the right direction (, though, if there's a finished coding on the tip of your tounge, I wouldn't refuse it either).

Side-quest: Is there an easy way to make the character restore 5 Hunger whenever he consumes something?

Thanks in advance!

Have a great day!

Edited by BillTheCipher
Link to comment
Share on other sites

1. in pigbrain.lua

~~
    if inst.sg:HasStateTag("busy") then
        return
    end

    if inst.components.inventory ~= nil and inst.components.eater ~= nil then
        local target = inst.components.inventory:FindItem(function(item) return inst.components.eater:CanEat(item) end)
        if target ~= nil then
            return BufferedAction(inst, target, ACTIONS.EAT)
        end
    end
~~

make function containing this and insert doperiodictask will work I guess

Of course 'not edible consumable' like spider glands will not work you have to add them manually.

 

2. use inst:ListenForEvent("oneat", function)

local function HungerPlus(inst, data)
  if data.food then
    inst.components.hunger:DoDelta(5)
  end
end

inst:ListenForEvent("oneat", HungerPlus)

as I said in up consumable that is not edible like spider gland is different, you have to add them manually like AddPrefabPostInit("spider_gland_prefab_name_idk", function_that_do_fill_hunger_if_specific_character_uses_it).

Link to comment
Share on other sites

@Combustiblemon Many thanks for the codings!

Point 2) works flawlessly, but I'm not sure if I use the pig one the right way. Here's how I added it to my prefab:
 

Spoiler

 

-- This went above local function onload(inst):

local function Stresseat(inst)
if inst.components.inventory ~= nil and inst.components.eater ~= nil then
        local target = inst.components.inventory:FindItem(function(item) return inst.components.eater:CanEat(item) end)
        if target ~= nil then
            return BufferedAction(inst, target, ACTIONS.EAT)
        end
    end
end

-- This went into the local master_postinit = function(inst) section:

inst:DoPeriodicTask( 0.0, function(inst)
        Stresseat(inst)
    end)

 

Have I managed to mess up something?

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