Jump to content

Recommended Posts

Hello, if someone can help me with this that really be great :)!

So, I want my character to take no Sanity penalty from eating spoiled/stale food, I looked in edible.lua & it seems whenever a character eats spoiled food they always get a small sanity penalty from this function?

Spoiler

function Edible:GetSanity(eater)
    local ignore_spoilage = not self.degrades_with_spoilage or self.sanityvalue < 0 or (eater ~= nil and eater.components.eater ~= nil and eater.components.eater.ignoresspoilage)

    if not ignore_spoilage and self.inst.components.perishable ~= nil then
        if self.inst.components.perishable:IsStale() then
            if self.sanityvalue > 0 then
                return 0
            end
        elseif self.inst.components.perishable:IsSpoiled() then
            return -TUNING.SANITY_SMALL
        end
    end

    return self.sanityvalue
end

 

is there a way I can edit this to not include my character prefab without copypaste the edible component & editing it?

 

I tried to do AddComponentPostInit but failed hard

Spoiler

AddComponentPostInit("edible", function(inst)
if not GLOBAL.TheWorld.ismastersim then
	return
end
        local GetSanity_old = inst.components.edible.GetSanity
        inst.components.edible.GetSanity = function(inst, ...)
            if inst.prefab == "adam" then
                return
            end
            return GetSanity_old(inst, ...)
	end
end)

 

 

thanks for reading my problem have a good day/night :D!

Edited by SuperDavid

I believe that you haven't read the code yet. If you do so, you will find sth interesting:

eater.components.eater.ignoresspoilage

which means you only need to set the eater component of your character prefab in master_postinit with:

inst.components.eater.ignoresspoilage = true

by the way, the inst in AddComponentPostInit is the component itself, not the prefab.

 

28 minutes ago, ptr said:

eater.components.eater.ignoresspoilage

That will make my character not take hunger & health penalty too, I just want him to take no Sanity penalty but still take hunger & health penalty :).

Well, try this then

Quote

AddComponentPostInit("edible", function(inst)
    if not GLOBAL.TheWorld.ismastersim then
        return
    end
    local GetSanity_old = inst.GetSanity
    inst.GetSanity = function(self, eater)
        if eater.prefab == "adam" then
            return self.sanityvalue
        end
        return GetSanity_old(self, eater)
    end
end)

 

10 hours ago, ptr said:

Well, try this then

Thank you this code seems to work great. there's just 1 more thing if you know how to do is that when food is spoiled or stale it doesn't lose the positive sanity it gives, do you know how I can do that? E.g. stale/spoiled icecream still gives full sanity as fresh one. I'd like for stale food to only give half sanity value & spoiled food to give none.

If you can help with that then that'd really be great & thanks for your help man :wilson_laugh:!!

 

ps: do you also maybe know a way I can make my character take less health/hunger penalty from stale/spoiled food? I'm guessing I can do something with Wickerbottom's code, but I don't know what value I should put to take 25% less penalty than default characters?

Spoiler

inst.components.eater.stale_hunger = TUNING.WICKERBOTTOM_STALE_FOOD_HUNGER
    inst.components.eater.stale_health = TUNING.WICKERBOTTOM_STALE_FOOD_HEALTH
    inst.components.eater.spoiled_hunger = TUNING.WICKERBOTTOM_SPOILED_FOOD_HUNGER
    inst.components.eater.spoiled_health = TUNING.WICKERBOTTOM_SPOILED_FOOD_HEALTH

 

Edited by SuperDavid

I have tested the spoiled icecream, and it still gives the full sanity?

for health/hunger, it works like this: If the health/hunger is negative, stale/spoiled state won't affect its value. When they are positive, they will be multiplied by a number less than one to reduce the value. So if what you want is to reduce 25% less when stale/spoiled, you can modify the character with

    inst.components.eater.stale_hunger = TUNING.STALE_FOOD_HUNGER * 0.75 + 0.25
    inst.components.eater.stale_health = TUNING.STALE_FOOD_HEALTH * 0.75 + 0.25

    inst.components.eater.spoiled_hunger = TUNING.SPOILED_FOOD_HUNGER * 0.75 + 0.25
    inst.components.eater.spoiled_health = TUNING.SPOILED_FOOD_HEALTH * 0.75 + 0.25

1 hour ago, ptr said:

I have tested the spoiled icecream, and it still gives the full sanity?

I just tested it & my character get 50 sanity regardless of icecream spoilage, is there a way to make it give half sanity when it's stale & no sanity when spoiled?

 

1 hour ago, ptr said:

for health/hunger, it works like this: If the health/hunger is negative, stale/spoiled state won't affect its value. When they are positive, they will be multiplied by a number less than one to reduce the value. So if what you want is to reduce 25% less when stale/spoiled, you can modify the character with

    inst.components.eater.stale_hunger = TUNING.STALE_FOOD_HUNGER * 0.75 + 0.25
    inst.components.eater.stale_health = TUNING.STALE_FOOD_HEALTH * 0.75 + 0.25

 

Thanks, can I just ask if I want to make my character take less penalty would I increase the "+ 0.25" value? And thanks for your help man :wilson_smile:!!

ps: i'm not very good at math or coding so can I ask why is the " TUNING.STALE_FOOD_HUNGER " being multiplied by .75 then + .25 instead of just + .25? I just want to understand if I want to change in the future xD & thanks!

Edited by SuperDavid

Oh, I misunderstood that,

Quote

AddComponentPostInit("edible", function(inst)
    if not GLOBAL.TheWorld.ismastersim then
        return
    end
    local GetSanity_old = inst.GetSanity
    inst.GetSanity = function(self, eater)
        if eater.prefab == "adam" then
            if self.inst.components.perishable:IsStale() then
                -- do not reduce when sanityvalue is already negative
                if self.sanityvalue > 0 then
                    return self.sanityvalue * 0.5
                end
            elseif self.inst.components.perishable:IsSpoiled() then
                if self.sanityvalue > 0 then
                    return 0
                end
            end
            return self.sanityvalue
        end
        return GetSanity_old(self, eater)
    end
end)

 

The math behind that ...

Take the 'stale_hunger' as an example, which is the percentage of remaining hunger when food stale.

Meanwhile, the original value for hunger is 'hungervalue'.

 

So, the actual hunger value when food stale is

        ( hunger value when stale )

     = hungervalue * stale_hunger       -------------   ( Eqn. 1 )

 

And, the hunger value reduced is

        ( hunger value reduced )

     = hungervalue - ( hunger value when stale )

     = hungervalue - hungervalue * stale_hunger

     = hungervalue * ( 1 -  stale_hunger )

 

When you want to reduce x% less, the hunger value reduced should be

        ( current hunger value reduced )

      = ( hunger value reduced ) * ( 1 - x% )

      = hungervalue * ( 1 -  stale_hunger ) * ( 1 - x% )

      = hungervalue * [ 1 - x% -  stale_hunger * ( 1 - x% )]

 

And now, the current hunger value will be 

         ( current hunger value )

      = hungervalue - ( current hunger value reduced )

      = hungervalue - hungervalue * [ 1 - x% -  stale_hunger * ( 1 - x% )]

      = hungervalue * { 1 - [ 1 - x% -  stale_hunger * ( 1 - x% )]}

      = hungervalue * [ x% + stale_hunger * ( 1 - x% )]

 

Compare to Eqn.1, you will find the original stale_hunger is being replaced by

       x% + stale_hunger * ( 1 - x% )

 

That's why the expression should be 

      TUNING.STALE_FOOD_HUNGER * 0.75 + 0.25

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