Jump to content

Recommended Posts

Now that I've made an official release for the custom character I've been working on, I want to tackle that last perk I have planned out.

Since the character loses hunger twice as fast as normal characters, the advantage I had in mind to balance this would be adding bonuses to cooked foods and crock pot meals such as assigning more health, sanity, hunger or timed bonuses like defense, action speed or damage dealing.

An example would be taking monster lasagna's health penalty of -20 and changing it to 0 upon being eaten by said character.
I thought looking into wx78's code for gear eating would of helped but the coding seems tied more to foodtypes rather actual food names.
I tried out some strings for testing but sadly there weren't any changes made:

local function oneat(inst, food)
    if food == "pumpkin" then
        inst.components.edible.healthvalue = 5
        inst.components.edible.hungervalue = 5
        inst.components.edible.sanityvalue = 5
    end
end

Can anyone help me on this?

if you want to take that approach, you would have to do food.prefab, since food is the whole item

but changing the food value after its eaten would be pointless plus, you would need to do food.components.edible.healthvalue =5, not inst, since inst is the eater.

So you would either have to alter the eater's stats after eating like for monster lasanga example:

local function oneat(inst, food)
    if food == "pumpkin" then
        inst.components.edible.healthvalue = 5
        inst.components.edible.hungervalue = 5
        inst.components.edible.sanityvalue = 5
    elseif food.prefab == "monsterlasagna" then
        inst.components.health:DoDelta(TUNING.HEALING_MED) -- gives +20, to negate the hp removal (but if you had less than 20 hp, you would still die)
    end
end 

Or.. you could edit the eater component a bit to do some changes BEFORE eating rather than after.

--modmain.lua
AddComponentPostInit("eater", function(self)
	local oldEat = self.Eat
	function self:Eat(food, feeder)
		feeder = feeder or self.inst
		
		if self:PrefersToEat(food) then
			if self.modeatfn ~= nil --[[and self.inst.prefab == "coach" ]]then
				self.modeatfn(self.inst, food)
			end
			
			return oldEat(self, food, feeder)
		end
	end
end)

--yourcharacter.lua
local function preEat(inst, food)
	if food.prefab == "monsterlasagna" then
		food.components.edible.health = 0
	elseif food.prefab == "pumpkin" then
		food.components.edible.health = 5
		food.components.edible.hunger = 5
		food.components.edible.sanity = 5
		--food.components.edible:SetOnEatenFn(newEatFn) --to add effects (make sure you dont override old effects if any)
	end
end

--master_postinit in yourcharacter.lua
inst.components.eater.modeatfn = preEat

Untested btw

Edited by Aquaterion
1 hour ago, Aquaterion said:

if you want to take that approach, you would have to do food.prefab, since food is the whole item

but changing the food value after its eaten would be pointless plus, you would need to do food.components.edible.healthvalue =5, not inst, since inst is the eater.

So you would either have to alter the eater's stats after eating like for monster lasanga example:


local function oneat(inst, food)
    if food == "pumpkin" then
        inst.components.edible.healthvalue = 5
        inst.components.edible.hungervalue = 5
        inst.components.edible.sanityvalue = 5
    elseif food.prefab == "monsterlasagna" then
        inst.components.health:DoDelta(TUNING.HEALING_MED) -- gives +20, to negate the hp removal (but if you had less than 20 hp, you would still die)
    end
end 

Or.. you could edit the eater component a bit to do some changes BEFORE eating rather than after.


--modmain.lua
AddComponentPostInit("eater", function(self)
	local oldEat = self.Eat
	function self:Eat(food, feeder)
		feeder = feeder or self.inst
		
		if self:PrefersToEat(food) then
			if self.modeatfn ~= nil --[[and self.inst.prefab == "coach" ]]then
				self.modeatfn(self.inst, food)
			end
			
			return oldEat(self, food, feeder)
		end
	end
end)

--yourcharacter.lua
local function preEat(inst, food)
	if food.prefab == "monsterlasagna" then
		food.components.edible.health = 0
	elseif food.prefab == "pumpkin" then
		food.components.edible.health = 5
		food.components.edible.hunger = 5
		food.components.edible.sanity = 5
		--food.components.edible:SetOnEatenFn(newEatFn) --to add effects (make sure you dont override old effects if any)
	end
end

--master_postinit in yourcharacter.lua
inst.components.eater.modeatfn = preEat

Untested btw

Thank you for replying.

Sadly I have attempted to use both methods to which none seem to have changed the stats applied to both foods.

try this instead:

--modmain.lua
AddComponentPostInit("edible", function(self)
	local oldGetStat = { health = self.GetHealth, hunger = self.GetHunger, sanity = self.GetSanity }
	local oldOnEaten = self.OnEaten
	
	local function getstat(self, eater, statname)
		local stat = oldGetStat[statname](self, eater)
		if eater and eater.extrafoodstats then
			stat = stat + (eater.extrafoodstats[self.inst.prefab] and eater.extrafoodstats[self.inst.prefab][statname] or 0)
		end
		return stat
	end
	
	function self:GetHealth(eater)
		return getstat(self, eater, "health")
	end
	
	function self:GetHunger(eater)
		return getstat(self, eater, "hunger")
	end
	
	function self:GetSanity(eater)
		return getstat(self, eater, "sanity")
	end
	
	function self:OnEaten(eater)
		oldOnEaten(self, eater)
		
		if eater and eater.extrafoodstats and eater.extrafoodstats[self.inst.prefab] and eater.extrafoodstats[self.inst.prefab].fn then
			eater.extrafoodstats[self.inst.prefab].fn(self.inst, eater)
		end
	end
end)


--yourcharacter.lua master_postinit

inst.extrafoodstats = {
	pumpkin = { health = 5, hunger = 5, sanity = 5, fn = function(food, eater) eater.components.talker:Say("Great Pumpkin") end},
	monsterlasagna = { health = 20 },
}

 

Edited by Aquaterion

I'm not sure if this is what you need, but you can try this:

Go to your modmain.lua and add

    GetPlayer = GLOBAL.GetPlayer    

    AddPrefabPostInit("monsterlasagna", function(inst)
        if GetPlayer().prefab == "YourCharacterHere" then
            inst.components.edible.healthvalue = 0
        end
    end)

7 hours ago, BraveChicken said:

I'm not sure if this is what you need, but you can try this:

Go to your modmain.lua and add

    GetPlayer = GLOBAL.GetPlayer    

    AddPrefabPostInit("monsterlasagna", function(inst)
        if GetPlayer().prefab == "YourCharacterHere" then
            inst.components.edible.healthvalue = 0
        end
    end)

that would work just fine, if it was 'dont starve' and not 'dont starve together'

Edited by Aquaterion
17 hours ago, Aquaterion said:

try this instead:


--modmain.lua
AddComponentPostInit("edible", function(self)
	local oldGetStat = { health = self.GetHealth, hunger = self.GetHunger, sanity = self.GetSanity }
	local oldOnEaten = self.OnEaten
	
	local function getstat(self, eater, statname)
		local stat = oldGetStat[statname](self, eater)
		if eater and eater.extrafoodstats then
			stat = stat + (eater.extrafoodstats[self.inst.prefab] and eater.extrafoodstats[self.inst.prefab][statname] or 0)
		end
		return stat
	end
	
	function self:GetHealth(eater)
		return getstat(self, eater, "health")
	end
	
	function self:GetHunger(eater)
		return getstat(self, eater, "hunger")
	end
	
	function self:GetSanity(eater)
		return getstat(self, eater, "sanity")
	end
	
	function self:OnEaten(eater)
		oldOnEaten(self, eater)
		
		if eater and eater.extrafoodstats and eater.extrafoodstats[self.inst.prefab] and eater.extrafoodstats[self.inst.prefab].fn then
			eater.extrafoodstats[self.inst.prefab].fn(self.inst, eater)
		end
	end
end)


--yourcharacter.lua master_postinit

inst.extrafoodstats = {
	pumpkin = { health = 5, hunger = 5, sanity = 5, fn = function(food, eater) eater.components.talker:Say("Great Pumpkin") end},
	monsterlasagna = { health = 20 },
}

 

That one worked! Thank you.

I'll inform you if I run into anything that needs questioning.

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