Jump to content

Make food modify hunger burn rate


Recommended Posts

hey everyone, 

im making a character who's hunger rate is burning fairly fast, but eating shrooms will make it burn slower for 200 seconds.

here is the code i used at first:

Spoiler

local function OnEat(inst, food)

    if food or food.components.edible or food.components.edible.foodtype == "mushroom" then

        inst.components.hunger.burnratemodifiers:SetModifier(inst, TUNING.MUSHROOMHAT_SLOW_HUNGER)

    end

end

 which worked, but then i tried making it timed, adding somerthing simole which i saw is used in other code for timing:

Spoiler

local function OnEat(inst, food)

    if food or food.components.edible or food.components.edible.foodtype == "mushroom" then

        inst.components.hunger.burnratemodifiers:SetModifier(inst, TUNING.MUSHROOMHAT_SLOW_HUNGER) = 200

    end

end

and now it gives me an error, " unexpected symbol near '=' "

could it be that i cant use the timing function without indicating something first? whatshould i do to make the game count down to 200 seconds before getting rid of the hunger burn rate effect?

 

Link to comment
Share on other sites

That's not how you do a timed function. That's not correct LUA syntax.

You'd need to do this:

inst:DoTaskInTime(200, function(inst)
	-- Remove modifier
end

Only thing is, I'm not sure if it saves the timed tasks, so maybe if you eat the food and quit the game and enter it again, it'll never remove the burn rate modifier. In which case you'd have to make your own timer variable, and save it with the player. Test it out, though. It might be fine. You can always just set it to 10-20 seconds to begin with, just for testing this, so you don't have to wait so long.

Also, I can see that you copied this from the mushroom hat. In its code, "inst" refers to the hat, so giving the "inst" as the "source" parameter (the first parameter) makes sense there, but in your case you should put the "food" as the "source", instead of "inst" which in this case is the player. Since the food instance will be destroyed immediately, we will instead put its .prefab as "source". You also seem to be using "or" when you should be using "and". Try this:

local function OnEat(inst, food)
	if food and food.components.edible and food.components.edible.foodtype == "mushroom" then
		inst.components.hunger.burnratemodifiers:SetModifier(food.prefab, TUNING.MUSHROOMHAT_SLOW_HUNGER)
		inst:DoTaskInTime(200, function(inst)
			-- Remove modifier
			inst.components.hunger.burnratemodifiers:RemoveModifier(food.prefab)
		end
	end
end

 

Link to comment
Share on other sites

13 hours ago, Ultroman said:

That's not how you do a timed function. That's not correct LUA syntax.

You'd need to do this:


inst:DoTaskInTime(200, function(inst)
	-- Remove modifier
end

Only thing is, I'm not sure if it saves the timed tasks, so maybe if you eat the food and quit the game and enter it again, it'll never remove the burn rate modifier. In which case you'd have to make your own timer variable, and save it with the player. Test it out, though. It might be fine. You can always just set it to 10-20 seconds to begin with, just for testing this, so you don't have to wait so long.

Also, I can see that you copied this from the mushroom hat. In its code, "inst" refers to the hat, so giving the "inst" as the "source" parameter (the first parameter) makes sense there, but in your case you should put the "food" as the "source", instead of "inst" which in this case is the player. Since the food instance will be destroyed immediately, we will instead put its .prefab as "source". You also seem to be using "or" when you should be using "and". Try this:


local function OnEat(inst, food)
	if food and food.components.edible and food.components.edible.foodtype == "mushroom" then
		inst.components.hunger.burnratemodifiers:SetModifier(food.prefab, TUNING.MUSHROOMHAT_SLOW_HUNGER)
		inst:DoTaskInTime(200, function(inst)
			-- Remove modifier
			inst.components.hunger.burnratemodifiers:RemoveModifier(food.prefab)
		end
	end
end

 

i put the code as you suggested, but it didn't work - i used a stopwatch and in 50 seconds the food rate stayed the same both times, 50 seconds without the mushroom and 50 seconds with the mushroom

Link to comment
Share on other sites

3 hours ago, Ultroman said:

Did you hook up the OnEat function properly? I'd need to see your code.

here is everything i got in my OnEat category thing:

Spoiler

local function OnEat(inst, food)
    -- hookin the food bonus
    local food_bonus = food_bonuses[food.prefab]
    
    -- if food bonus then do the bonus
    if food_bonus ~= nil then
        
        -- but first check if food bonus        
        if food_bonus["health"] then
            inst.components.health:DoDelta(food_bonus["health"])
        end
        if food_bonus["sanity"] then
            inst.components.sanity:DoDelta(food_bonus["sanity"])
        end
        if food_bonus["hunger"] then
            inst.components.hunger:DoDelta(food_bonus["hunger"])
        end
    end
    --trying to change mushroom effects
    if food and food.components.edible and food.components.edible.foodtype == "mushroom" then
        inst.components.hunger.burnratemodifiers:SetModifier(food.prefab, TUNING.MUSHROOMHAT_SLOW_HUNGER)
        inst:DoTaskInTime(50, function(inst)
            -- Remove modifier
            inst.components.hunger.burnratemodifiers:RemoveModifier(food.prefab)
            end )
        end 
    end

 

 

Link to comment
Share on other sites

There's no such thing as a "mushroom" foodtype.

food.components.edible.foodtype == "mushroom"

In the Hamlet code I could only find these:

  • GENERIC
  • MEAT
  • ELEMENTAL
  • WOOD
  • HORRIBLE
  • RAW
  • VEGGIE
  • ROUGHAGE
  • GEARS
  • GOLDDUST
  • INSECT
  • BONE
  • TASTY
  • maybe more...see constants.lua for FOOD_TYPES, FOOD_GROUPS and more.

If it's one single edible item you want to give this burn rate, then check food.prefab == "prefabname". If it's seceral prefabs, then you need to do the same kind of thing we did with the foodbonuses.

Link to comment
Share on other sites

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.

×
  • Create New...