Jump to content

Sanity Rate multiplier


Recommended Posts

3 hours ago, IThatGuyI said:

Add inst.components.sanity.rate_modifier = 'value' with value being the percantage. For example rate_modifier of 1.5 means your character will be losing sanity 50% faster. The default value is 1.

Seems to only affect auras and rate. I want that, but also one-time things like picking normal or evil flowers, eating good or bad food, casting spells, ect...

Link to comment
Share on other sites

If you want to boost sanity gain from ALL sources then the best way to do it would be to modify the sanity component by adding a multiplier and putting it in DoDelta function.

Edited by IThatGuyI
  • Like 1
Link to comment
Share on other sites

1 hour ago, IThatGuyI said:

If you want to boost sanity gain from ALL sources then the best way to do it would be to modify the sanity component by adding a multiplier and putting it in DoDelta function.

I think I did it wrong

inst.components.sanity:DoDelta(*1.5)

Doesn't seem to like that the * is next to (.

Link to comment
Share on other sites

No no no. You need to edit the component using AddClassPostConstruct. You should add a new variable and name it something like 'moddedsanitymultiplier' and then add it to the DoDelta function so that it will multiply the final amount of sanity lost.

Something like that (all of it needs to be in modmain.lua):

local function SanityPostCons(self)
	self.moddedsanitymultiplier = 1
  
	function Sanity:DoDelta(delta, overtime)
		if self.redirect ~= nil then
			self.redirect(self.inst, delta, overtime)
			return
		end

		if self.ignore then
			return
		end

		self.current = math.min(math.max(self.current + (delta*self.moddedsanitymultiplier), 0), self.max - (self.max * self.penalty)) -- Here you can see I added the multiplier

		-- must calculate it due to inducedinsanity ...
		local percent_ignoresinduced = self.current / self.max
		if self.mode == SANITY_MODE_INSANITY then
			if self.sane and percent_ignoresinduced <= TUNING.SANITY_BECOME_INSANE_THRESH then --30
				self.sane = false
			elseif not self.sane and percent_ignoresinduced >= TUNING.SANITY_BECOME_SANE_THRESH then --35
				self.sane = true
			end
		else
			if self.sane and percent_ignoresinduced >= TUNING.SANITY_BECOME_ENLIGHTENED_THRESH then
				self.sane = false
			elseif not self.sane and percent_ignoresinduced <= TUNING.SANITY_LOSE_ENLIGHTENMENT_THRESH then
				self.sane = true
			end
		end

		self.inst:PushEvent("sanitydelta", { oldpercent = self._oldpercent, newpercent = self:GetPercent(), overtime = overtime, sanitymode = self.mode })
		self._oldpercent = self:GetPercent()

		if self:IsSane() ~= self._oldissane then
			self._oldissane = self:IsSane()
			if self._oldissane then
				if self.onSane ~= nil then
					self.onSane(self.inst)
				end
				self.inst:PushEvent("gosane")
				ProfileStatsSet("went_sane", true)
			else
				if self.mode == SANITY_MODE_INSANITY then
					if self.onInsane ~= nil then
						self.onInsane(self.inst)
					end
					self.inst:PushEvent("goinsane")
					ProfileStatsSet("went_insane", true)
				else --self.mode == SANITY_MODE_LUNACY
					if self.onEnlightened ~= nil then
						self.onEnlightened(self.inst)
					end
					self.inst:PushEvent("goenlightened")
					ProfileStatsSet("went_enlightened", true)
				end
			end
		end
	end
end

AddClassPostConstruct("components/sanity", SanityPostCons)

And then just set the sanity drain by adding inst.components.sanity.moddedsanitymultiplier = 'value' to your character. For example setting this to 1.5 means your character will be losing sanity 50% faster than normal.

  • Like 1
Link to comment
Share on other sites

1 hour ago, IThatGuyI said:

No no no. You need to edit the component using AddClassPostConstruct. You should add a new variable and name it something like 'moddedsanitymultiplier' and then add it to the DoDelta function so that it will multiply the final amount of sanity lost.

Something like that (all of it needs to be in modmain.lua):


local function SanityPostCons(self)
	self.moddedsanitymultiplier = 1
  
	function Sanity:DoDelta(delta, overtime)
		if self.redirect ~= nil then
			self.redirect(self.inst, delta, overtime)
			return
		end

		if self.ignore then
			return
		end

		self.current = math.min(math.max(self.current + (delta*self.moddedsanitymultiplier), 0), self.max - (self.max * self.penalty)) -- Here you can see I added the multiplier

		-- must calculate it due to inducedinsanity ...
		local percent_ignoresinduced = self.current / self.max
		if self.mode == SANITY_MODE_INSANITY then
			if self.sane and percent_ignoresinduced <= TUNING.SANITY_BECOME_INSANE_THRESH then --30
				self.sane = false
			elseif not self.sane and percent_ignoresinduced >= TUNING.SANITY_BECOME_SANE_THRESH then --35
				self.sane = true
			end
		else
			if self.sane and percent_ignoresinduced >= TUNING.SANITY_BECOME_ENLIGHTENED_THRESH then
				self.sane = false
			elseif not self.sane and percent_ignoresinduced <= TUNING.SANITY_LOSE_ENLIGHTENMENT_THRESH then
				self.sane = true
			end
		end

		self.inst:PushEvent("sanitydelta", { oldpercent = self._oldpercent, newpercent = self:GetPercent(), overtime = overtime, sanitymode = self.mode })
		self._oldpercent = self:GetPercent()

		if self:IsSane() ~= self._oldissane then
			self._oldissane = self:IsSane()
			if self._oldissane then
				if self.onSane ~= nil then
					self.onSane(self.inst)
				end
				self.inst:PushEvent("gosane")
				ProfileStatsSet("went_sane", true)
			else
				if self.mode == SANITY_MODE_INSANITY then
					if self.onInsane ~= nil then
						self.onInsane(self.inst)
					end
					self.inst:PushEvent("goinsane")
					ProfileStatsSet("went_insane", true)
				else --self.mode == SANITY_MODE_LUNACY
					if self.onEnlightened ~= nil then
						self.onEnlightened(self.inst)
					end
					self.inst:PushEvent("goenlightened")
					ProfileStatsSet("went_enlightened", true)
				end
			end
		end
	end
end

AddClassPostConstruct("components/sanity", SanityPostCons)

And then just set the sanity drain by adding inst.components.sanity.moddedsanitymultiplier = 'value' to your character. For example setting this to 1.5 means your character will be losing sanity 50% faster than normal.

Not sure if this'll work for me, since my modmain has information for two different characters... I didn't mention it cuz I didn't think it'd be relevant...

Link to comment
Share on other sites

12 hours ago, IThatGuyI said:

It should work as long as it's in modmain.

By that I mean that I want it to affect one character, not the other.

 

Sorry that it's one thing after the other with me.

No, wait, now I see what you're telling me to do.

Edited by icantevenname
Added apology, then figured it out.
Link to comment
Share on other sites

Am I missing something? It keeps showing an empty Character Select screen when starting the game and then kicking me back to Desktop when I click anything. I keep looking up and down the local function and it doesn't seem that you expect me to change anything there.

Link to comment
Share on other sites

35 minutes ago, IThatGuyI said:

Ahh, my bad. I provided you with a wrong AddPostInit function. It should be AddComponentPostInit not AddClassPostInit. And use 'sanity' instead of 'components/sanity'.

Jus' double checking, is it supposed to look like this?

AddComponentPostInit("sanity", SanityPostCons)

Cuz now I'm getting this.

20200811163201_1.jpg

Edited by icantevenname
Link to comment
Share on other sites

I have it like this...

local require = GLOBAL.require
local STRINGS = GLOBAL.STRINGS
local Sanity = require "components/sanity"

local function SanityPostCons(self)
	self.moddedsanitymultiplier = 1

And the blank CSS happens.

In the character's luas, I have coding that changes the rate they lose sanity when wet. Think that's interfering with this?

Edited by icantevenname
Extra info
Link to comment
Share on other sites

Ok, remove everything and put this in your modmain.
 

local require = GLOBAL.require
local Sanity = require "components/sanity"

local function SanityPostInit(self)
	self.moddedmultiplier = 1

	function Sanity:DoDelta(delta, overtime)
		if self.redirect ~= nil then
			self.redirect(self.inst, delta, overtime)
			return
		end

		if self.ignore then
			return
		end

		self.current = math.min(math.max(self.current + (delta * self.moddedmultiplier), 0), self.max - (self.max * self.penalty))

		-- must calculate it due to inducedinsanity ...
		local percent_ignoresinduced = self.current / self.max
		if self.mode == SANITY_MODE_INSANITY then
			if self.sane and percent_ignoresinduced <= TUNING.SANITY_BECOME_INSANE_THRESH then --30
				self.sane = false
			elseif not self.sane and percent_ignoresinduced >= TUNING.SANITY_BECOME_SANE_THRESH then --35
				self.sane = true
			end
		else
			if self.sane and percent_ignoresinduced >= TUNING.SANITY_BECOME_ENLIGHTENED_THRESH then
				self.sane = false
			elseif not self.sane and percent_ignoresinduced <= TUNING.SANITY_LOSE_ENLIGHTENMENT_THRESH then
				self.sane = true
			end
		end

		self.inst:PushEvent("sanitydelta", { oldpercent = self._oldpercent, newpercent = self:GetPercent(), overtime = overtime, sanitymode = self.mode })
		self._oldpercent = self:GetPercent()

		if self:IsSane() ~= self._oldissane then
			self._oldissane = self:IsSane()
			if self._oldissane then
				if self.onSane ~= nil then
					self.onSane(self.inst)
				end
				self.inst:PushEvent("gosane")
				ProfileStatsSet("went_sane", true)
			else
				if self.mode == SANITY_MODE_INSANITY then
					if self.onInsane ~= nil then
						self.onInsane(self.inst)
					end
					self.inst:PushEvent("goinsane")
					ProfileStatsSet("went_insane", true)
				else --self.mode == SANITY_MODE_LUNACY
					if self.onEnlightened ~= nil then
						self.onEnlightened(self.inst)
					end
					self.inst:PushEvent("goenlightened")
					ProfileStatsSet("went_enlightened", true)
				end
			end
		end
	end
end

AddComponentPostInit("sanity", SanityPostInit)

I tested this code and it didn't crash for me, so if it will crash your game then it's a problem outside of this code.

  • Like 1
Link to comment
Share on other sites

Sorry for all the false information. Turns out I'm terrible add overriding components, but I think this time is will work. Here's the code:

local function SanityPostInit(component) -- Function to partially override the DoDelta function
	component.moddedmultiplier = 1 -- Your variable
	local old_DoDelta = component.DoDelta -- Old DoDelta function
	
	component.DoDelta = function(self, delta, overtime) -- New DoDelta function
		local moddeddelta = delta*component.moddedmultiplier -- Local variable which uses the default delta and the multiplies it by your multiplier
		return old_DoDelta(self, moddeddelta, overtime) -- Returning the function
	end
end

AddComponentPostInit("sanity", SanityPostInit) -- AddComponentPostInit

 

And then set the moddedmultiplier value in your characters prefab file with:

inst.components.sanity.moddedmultiplier = 'your_value' -- Percent e.g 1.5 = 150%

 

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

7 hours ago, IThatGuyI said:

Sorry for all the false information. Turns out I'm terrible add overriding components, but I think this time is will work. Here's the code:


local function SanityPostInit(component) -- Function to partially override the DoDelta function
	component.moddedmultiplier = 1 -- Your variable
	local old_DoDelta = component.DoDelta -- Old DoDelta function
	
	component.DoDelta = function(self, delta, overtime) -- New DoDelta function
		local moddeddelta = delta*component.moddedmultiplier -- Local variable which uses the default delta and the multiplies it by your multiplier
		return old_DoDelta(self, moddeddelta, overtime) -- Returning the function
	end
end

AddComponentPostInit("sanity", SanityPostInit) -- AddComponentPostInit

 

And then set the moddedmultiplier value in your characters prefab file with:


inst.components.sanity.moddedmultiplier = 'your_value' -- Percent e.g 1.5 = 150%

 

It all works fine! Thanks so much!

  • Like 1
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...