Thebrettstoner Posted April 9, 2019 Share Posted April 9, 2019 Hello Everyone! Im working on a character that automaticly eats everything he has on him when his hunger gets too low until his hunger is full again. I have everything in place exept for the part where he actually eats any food he has. And i have no idea how to do this to be fair. Would someone be willing to lend me a hand? 1 Link to comment Share on other sites More sharing options...
Ultroman Posted April 9, 2019 Share Posted April 9, 2019 (edited) I'd just put a DoPeriodicTask into the character's master_postinit. -- DoPeriodicTask calls the given function every X seconds. -- I set it to 1.0. You can set it to e.g. 0.5 seconds if you want. inst:DoPeriodicTask(1.0, function(inst) -- Search for things labelled food in your inventory, and eat it. end) To find certain items in your inventory, the inventory component has this function: FindItem(fn) It works like this: -- Find the first food item in the inventory. -- FindItem goes through every slot, and checks the item using the given custom function. local foodItem = inst.components.inventory:FindItem(function(item) -- Do your checks, and return true if the item satisfies your needs. -- You may want to add that the food item should actually increase hunger and/or -- should not be a "bad food", as well. Otherwise, your character will also eat -- Wet Goop, Flower Petals and spoiled foods. -- This code just literally picks the first item that has an edible component on it. if item.components.edible then return true end return false end) -- If we have found a food item, we just look at the eater component, and find that it has an Eat function. if foodItem then -- Eat the item inst.components.eater:Eat(foodItem) end Edited April 9, 2019 by Ultroman Link to comment Share on other sites More sharing options...
Thebrettstoner Posted April 9, 2019 Author Share Posted April 9, 2019 2 hours ago, Ultroman said: I'd just put a DoPeriodicTask into the character's master_postinit. -- DoPeriodicTask calls the given function every X seconds. -- I set it to 1.0. You can set it to e.g. 0.5 seconds if you want. inst:DoPeriodicTask(1.0, function(inst) -- Search for things labelled food in your inventory, and eat it. end) To find certain items in your inventory, the inventory component has this function: FindItem(fn) It works like this: -- Find the first food item in the inventory. -- FindItem goes through every slot, and checks the item using the given custom function. local foodItem = inst.components.inventory:FindItem(function(item) -- Do your checks, and return true if the item satisfies your needs. -- You may want to add that the food item should actually increase hunger and/or -- should not be a "bad food", as well. Otherwise, your character will also eat -- Wet Goop, Flower Petals and spoiled foods. -- This code just literally picks the first item that has an edible component on it. if item.components.edible then return true end return false end) -- If we have found a food item, we just look at the eater component, and find that it has an Eat function. if foodItem then -- Eat the item inst.components.eater:Eat(foodItem) end I didn't know that was possible, thank you so much for teaching me! works exactly like i wanted it to now! Link to comment Share on other sites More sharing options...
Ultroman Posted April 9, 2019 Share Posted April 9, 2019 You're welcome Glad it works Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now