Eranthis Posted March 11, 2018 Share Posted March 11, 2018 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? Link to comment https://forums.kleientertainment.com/forums/topic/88584-help-with-custom-perk-food-bonuses/ Share on other sites More sharing options...
Aquaterion Posted March 11, 2018 Share Posted March 11, 2018 (edited) 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 March 11, 2018 by Aquaterion Link to comment https://forums.kleientertainment.com/forums/topic/88584-help-with-custom-perk-food-bonuses/#findComment-1013873 Share on other sites More sharing options...
Eranthis Posted March 11, 2018 Author Share Posted March 11, 2018 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. Link to comment https://forums.kleientertainment.com/forums/topic/88584-help-with-custom-perk-food-bonuses/#findComment-1013890 Share on other sites More sharing options...
Aquaterion Posted March 11, 2018 Share Posted March 11, 2018 (edited) 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 March 11, 2018 by Aquaterion Link to comment https://forums.kleientertainment.com/forums/topic/88584-help-with-custom-perk-food-bonuses/#findComment-1013916 Share on other sites More sharing options...
BraveChicken Posted March 11, 2018 Share Posted March 11, 2018 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) Link to comment https://forums.kleientertainment.com/forums/topic/88584-help-with-custom-perk-food-bonuses/#findComment-1013975 Share on other sites More sharing options...
Aquaterion Posted March 12, 2018 Share Posted March 12, 2018 (edited) 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 March 12, 2018 by Aquaterion Link to comment https://forums.kleientertainment.com/forums/topic/88584-help-with-custom-perk-food-bonuses/#findComment-1014101 Share on other sites More sharing options...
Eranthis Posted March 12, 2018 Author Share Posted March 12, 2018 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. Link to comment https://forums.kleientertainment.com/forums/topic/88584-help-with-custom-perk-food-bonuses/#findComment-1014246 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