Jump to content

Recommended Posts

Hello, I need some help trying to fix my mossling's behavior... So this is the code I need help with...

local function EatFoodAction(inst)	--Look for food to eat

	local target = nil
	local action = nil

	if inst.sg:HasStateTag("busy")
		and not inst.sg:HasStateTag("wantstoeat") then
		return
	end

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

	local pt = inst:GetPosition()
	local ents = TheSim:FindEntities(pt.x, pt.y, pt.z, SEE_FOOD_DIST, nil, NO_TAGS, inst.components.eater:GetEdibleTags())

	if not target then
		for k,v in pairs(ents) do
			if v and v:IsOnValidGround() and
			inst.components.eater:CanEat(v) and
			v:GetTimeAlive() > 5 and
			v.components.inventoryitem and not
			v.components.inventoryitem:IsHeld() and
			TargetNotClaimed(inst, v) then
				target = v
				break
			end
		end
	end

	if target then
		local action = BufferedAction(inst,target,ACTIONS.PICKUP)
		return action
	end
end

  ^This code allows mosslings to pickup food items that they can eat off the ground and eat it. My problem's when the mosslings pick up the food off the ground and eat it the food item gets destroyed unlike when they harvest berry bushes and stuff. Can someone please tell me which part of this code has to be removed/edited then they don't destroy the food items they eat and keep it in there inventory? Help would make me really happy because I really want my mosslings be able to store food in there inventory without "digesting" it :(...

Thank you for reading :D!!

Okay, so I fixed my mossling destroying the food he ate but I have a new problem now :(... I want to put a 5 second limit on when my mossling can pick up the food he comes across so I tried this 

if target then
	inst:DoTaskInTime(5,
    function()
    local action = BufferedAction(inst,target,ACTIONS.PICKUP)
	return action
    end)
	end

but it doesn't do anything at all! So how can I make that code have a 5 second timer till he preforms "local action = BufferedAction(inst,target,ACTIONS.PICKUP) return action" !?

@SuperDavid Having it DoTaskInTime just delays the action by 5 seconds. This will probably cause it to queue it up a bunch of times, and then after 5 seconds actually try to do it a bunch of times.

Instead, what you want to do is have a local "lock" variable, and have it check if it's locked or not, and then use the DoTaskInTime to unlock it. So for example:

	if target and inst.pick_up_food_on_cooldown == nil then
		inst.pick_up_food_on_cooldown = true
		inst:DoTaskInTime(5, function() inst.pick_up_food_on_cooldown = nil end)
		local action = BufferedAction(inst,target,ACTIONS.PICKUP)
		return action
	end

Edit: changed the code, actually better to attach it to the inst instead of having it local to the brain (that would share it between mosslings)

Edited by rezecib

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