Jump to content

How to make modded character constantly lose sanity?


Recommended Posts

I'm making a mod for Hanma Yujiro, and I don't want him to lose or gain sanity through normal means. Instead, I want him to essentially have a timer counting down until he's insane and the only way to delay it is killing something. I already have the code for him to gain sanity on kill/attack, now only the other half of the equation is left. Any more experienced modders have a solution?

Link to comment
Share on other sites

If you want him to only be able to get sanity by killing something and lose sanity by letting time pass, you can add 

AddClassPostConstruct("components/sanity",function(self)
	local old_dodelta = self.DoDelta
	self.DoDelta = function(self,delta,overtime,...)
		if self.inst.prefab == "your_character_name" then
			return old_dodelta(self,0,overtime,...)
		else
			return old_dodelta(self,delta,overtime,...)
		end
	end
end)

to your modmain. This will change all sanity changes that are made to your character to 0, so no change at all. Don't forget to change the character name ;)

The problem now is that you still want to change the sanity for some specific things. As you can't use inst.components.sanity:DoDelta anymore, as it will all be changed to 0, you need to change it to:

inst.components.sanity.current = inst.components.sanity.current + 5 -- only an example, change the value to what you want.
inst.components.sanity.DoDelta(0)

We do the DoDelta to refresh the sanity of the character, so that you can see if he is already insane.

As for the timer, the easiest solution is to add this the character master postinit:

inst:DoPeriodicTask(1,function(inst)
	if inst.components.sanity then
      		inst.components.sanity.current = inst.components.sanity.current - 1
      		inst.components.sanity:DoDelta(0)
    	end
end)

This will reduce the sanity by 1 each second, you can also change it the values you want to have.

If you choose to not use the first code snippet I added you can also replace this by using 

inst:DoPeriodicTask(1,function(inst)
	if inst.components.sanity then
      		inst.components.sanity:DoDelta(1)
    	end
end)

These are in fact the same, the first one just bypasses the restrictions we made in the first code snippet.

If you have more questions let me know!

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