Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Ultroman    460

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

 

Share this post


Link to post
Share on other sites
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! 

Share this post


Link to post
Share on other sites