Jump to content

Having trouble silencing the sound of sanity regeneration


Recommended Posts

I'm having a problem with changing the statusdisplays widget. I have a snowman character who periodically regains sanity and health while it is snowing. I have the following code to make sure that the game doesn't make sounds when sanity and health is increased because of snow, since that is annoying to constantly hear during the winter:

 

local regen_cause = "Snow" 


AddClassPostConstruct("widgets/statusdisplays", function(self)    
    local old_HealthDelta = self.HealthDelta    -- Copied from StatusDisplays
    local old_SanityDelta = self.SanityDelta
    local HealthDelta_nosound = function(self, data)        
        self.heart:SetPercent(data.newpercent, self.owner.components.health.maxhealth,self.owner.components.health:GetPenaltyPercent())        
        if data.oldpercent > .33 and data.newpercent <= .33 then            
            self.heart:StartWarning()        
        else            
            self.heart:StopWarning()        
        end        

        if not data.overtime then            
            if data.newpercent > data.oldpercent then                
                self.heart:PulseGreen()                
                -- TheFrontEnd:GetSound():PlaySound("dontstarve/HUD/health_up")            
            elseif data.newpercent < data.oldpercent then                
                -- TheFrontEnd:GetSound():PlaySound("dontstarve/HUD/health_down")                
                self.heart:PulseRed()            
            end        
        end    
    end    
    
    local SanityDelta_nosound = function(self, data)
        self:SetSanityPercent(data.newpercent)

        if self.brain ~= nil and self.brain.SanityDelta_nosound then
            self.brain:SanityDelta_nosound(self, data)
        else
            if not data.overtime then
                if data.newpercent > data.oldpercent then
                    self.brain:PulseGreen()
                    --TheFrontEnd:GetSound():PlaySound("dontstarve/HUD/sanity_up")
                elseif data.newpercent < data.oldpercent then
                    self.brain:PulseRed()
                    --TheFrontEnd:GetSound():PlaySound("dontstarve/HUD/sanity_down")
                end
            end
        end
    end
 
-- The new health delta function    
    function self:HealthDelta(data)        
        if (data.cause == regen_cause) then            
            return HealthDelta_nosound(self, data)        
        else            
            return old_HealthDelta(self, data)        
        end    
    end
    
    function self:SanityDelta(data)        
        if (data.cause == regen_cause) then            
            return SanityDelta_nosound(self, data)        
        else            
            return old_SanityDelta(self, data)        
        end    
    end
end) 

For some reason, this works perfectly for health, causing no sound to be produced when the player regains HP from snow. 

However, for some reason, sanity seems unaffected by the code, still generating sound when regained from snow. Here is the periodic task that regenerates HP and sanity for reference (It also serves to increase speed in snow):
 

local function fastinsnow(inst)
    --this will check if it is raining and either set your speed to normal if it is not, or increase it to 1.5 if it is snowing
    if TheWorld.state.issnowing == true then
        inst.components.locomotor:SetExternalSpeedMultiplier(inst, "character_speed_mod", 1.5)
            local t = GetTime()
    inst.components.health:DoDelta(1, false, "Snow")
    inst.components.sanity:DoDelta(2, false, "Snow")

    else
        inst.components.locomotor:SetExternalSpeedMultiplier(inst, "character_speed_mod", 1)
    end
    
end

 

Does anyone know why this code only works for health? Everything seems to match in terms of syntax and names, so I just am not sure what to change here.
 

Link to comment
Share on other sites

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
 Share

×
  • Create New...