Jump to content

Sanity code busted


Recommended Posts

I've been using this code for all my modded characters, but during the latest beta it was broken. I was hoping it would work after the release but that hasn't been the case and it's broken further.

As of now the sanity icon looks like this

 PlAZMvJ.png 

I'd like some help updating the code. 

Spoiler

local function CreateOrHookSanityAura(inst, rate)
    if inst.components.sanityaura == nil
    then
        inst:AddComponent("sanityaura")
    end
    local GetAura_old = inst.components.sanityaura.GetAura
    inst.components.sanityaura.GetAura = function(self, observer, ...)
        if observer.prefab == "wrwilson"
        then
            return GetAura_old(self, observer, ...) + rate
        end
        return GetAura_old(self, observer, ...)
    end
end

local function AddOrAddToSanityAuraRateOfPrefab(prefab, rate)
    AddPrefabPostInit(
        prefab,
        function(inst)
            if not GLOBAL.TheWorld.ismastersim
            then
                return
            end
            inst:DoTaskInTime(
                0,
                function(inst)
                    CreateOrHookSanityAura(inst, rate)
                end
            )
        end
    )
end
	
AddOrAddToSanityAuraRateOfPrefab("researchlab3", TUNING.SANITYAURA_SMALL)
AddOrAddToSanityAuraRateOfPrefab("crawlinghorror", -TUNING.SANITYAURA_MED)
AddOrAddToSanityAuraRateOfPrefab("terrorbeak", -TUNING.SANITYAURA_MED)
AddOrAddToSanityAuraRateOfPrefab("glommer", TUNING.SANITYAURA_MED)
AddOrAddToSanityAuraRateOfPrefab("statuemaxwell", -TUNING.SANITYAURA_TINY)
AddOrAddToSanityAuraRateOfPrefab("bishop", -TUNING.SANITYAURA_TINY)
AddOrAddToSanityAuraRateOfPrefab("knight", -TUNING.SANITYAURA_TINY)
AddOrAddToSanityAuraRateOfPrefab("rook", -TUNING.SANITYAURA_TINY)
AddOrAddToSanityAuraRateOfPrefab("waxwell", -TUNING.SANITYAURA_TINY)
AddOrAddToSanityAuraRateOfPrefab("flower_evil", -TUNING.SANITYAURA_MED)


AddOrAddToSanityAuraRateOfPrefab("spider", TUNING.SANITYAURA_SMALL)
AddOrAddToSanityAuraRateOfPrefab("spider_warrior", TUNING.SANITYAURA_MED)

AddOrAddToSanityAuraRateOfPrefab("spider_hider", TUNING.SANITYAURA_SMALL)
AddOrAddToSanityAuraRateOfPrefab("spider_spitter", TUNING.SANITYAURA_SMALL)
AddOrAddToSanityAuraRateOfPrefab("spider_dropper", TUNING.SANITYAURA_SMALL)

AddOrAddToSanityAuraRateOfPrefab("spiderqueen", TUNING.SANITYAURA_HUGE)

local AddDapperness = function(inst, owner)
	if not owner or not owner:HasTag("wretched") then
		return
	end
	
	if inst.components.equippable then
		inst.components.equippable.dapperness = inst.components.equippable.dapperness + 1
		print("Adding dapperness: "..inst.components.equippable.dapperness)
	end
end

local RemoveDapperness = function(inst, owner)
	if not owner or not owner:HasTag("wretched") then
		return
	end
	
	if inst.components.equippable then
		inst.components.equippable.dapperness = inst.components.equippable.dapperness - 1
		print("Removing dapperness: "..inst.components.equippable.dapperness)
	end
end

AddPrefabPostInit("nightsword", function(inst)
	if inst.components.equippable then
		local oldequip = inst.components.equippable.onequipfn
		inst.components.equippable.onequipfn = function(inst, owner)
			if oldequip ~= nil then
				oldequip(inst, owner)
			end
			AddDapperness(inst, owner)
		end
		
		local oldunequip = inst.components.equippable.onunequipfn
		inst.components.equippable.onunequipfn = function(inst, owner)
			if oldunequip ~= nil then
				oldunequip(inst, owner)
			end
			RemoveDapperness(inst, owner)
		end
	end
end)

 

 

Edited by Player_056
formating
Link to comment
Share on other sites

You can do a lot of debugging with print-statements. Making sure the state (values) has been changed after your code has been run, by printing the state before and after. See if you can find some line in the log files complaining about something in your mod without crashing. I can't really see anything in the code you posted that could mess up the sanity badge.

Link to comment
Share on other sites

Okay so turn's out one of my hud mods was the problem. BUT turns out the sword code doesn't work properly either.

Spoiler

[00:08:44]: [TooManyItems] SpawnPrefab: spider    
[00:09:04]: Removing dapperness: -0.33333333333333    
[00:09:08]: [TooManyItems] SpawnPrefab: spider    
[00:09:40]: Adding dapperness: 0.66666666666667    
[00:09:50]: Removing dapperness: -0.33333333333333    
[00:10:06]: Serializing user: session/584C4EED84CA6640/A7NTEA9MFP4J/0000000029
[00:10:06]: Serializing world: session/584C4EED84CA6640/0000000029
[00:10:07]: Registering master server in US lobby
[00:10:22]: Adding dapperness: 0.66666666666667    
[00:10:29]: Removing dapperness: -0.33333333333333    
[00:10:50]: Adding dapperness: 0.66666666666667    
[00:10:55]: Removing dapperness: -0.33333333333333    
[00:10:57]: Adding dapperness: 0.66666666666667    
[00:10:58]: Removing dapperness: -0.33333333333333    
[00:11:00]: Adding dapperness: 0.66666666666667    
[00:11:16]: Removing dapperness: -0.33333333333333    
[00:11:23]: Adding dapperness: 0.66666666666667    
[00:11:47]: Removing dapperness: -0.33333333333333    
[00:11:48]: Adding dapperness: 0.66666666666667    
[00:11:52]: Removing dapperness: -0.33333333333333    
[00:11:55]: Adding dapperness: 0.66666666666667    
[00:11:58]: Removing dapperness: -0.33333333333333

I can't find anything wrong in the log, my guess is that how dapperness works was changed

Link to comment
Share on other sites

Dapperness still works like it always has. Just checked. It looks like it's changing things. How is it not working?

I think you'll have to do some investigating. Something like printing the total equipped dapperness before and after you make the change. The ReCalc function in the sanity component calculates it like this:

local total_dapperness = self.dapperness or 0
local mitigates_rain = false
for k,v in pairs (self.inst.components.inventory.equipslots) do
	if v.components.dapperness then
		total_dapperness = total_dapperness + v.components.dapperness:GetDapperness(self.inst)
		if v.components.dapperness.mitigates_rain then
			mitigates_rain = true
		end
	end		
end

local dapper_delta = total_dapperness*TUNING.SANITY_DAPPERNESS

So you can do something like this:

local function calculateDappernessDelta(sel)
	local total_dapperness = self.dapperness or 0
	local mitigates_rain = false
	for k,v in pairs (self.inst.components.inventory.equipslots) do
		if v.components.dapperness then
			total_dapperness = total_dapperness + v.components.dapperness:GetDapperness(self.inst)
			if v.components.dapperness.mitigates_rain then
				mitigates_rain = true
			end
		end		
	end
	
	return total_dapperness*TUNING.SANITY_DAPPERNESS
end

And then use it all over, like this:

print("dapper_delta: "..calculateDapperDelta(player_inst.components.sanity))

 

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