Jump to content

How to make a healing item that also harms sanity?


Recommended Posts

Hi guys. I've got a character mod that has his own custom healing item. I've realised that by adding the "healer" component, the only option I have is changing the health value. I'd also want this item to harm sanity when used. I know I can make it edible and set the values, but I prefer the 'use healer animation' over the 'eating' animation. To make it clear, I'd like this item to:

  1. Heals health when used
  2. Decrease sanity
  3. Play 'healer' animation when used.

I was looking at flowers' script of how it uses DoDelta( ) and was trying to put that into my item's prefab script but failed (Is it because flowers have pickable component while my item doesn't?) I've heard I can create a new component or something but I'm very new to coding and not sure how can I do that. Can anyone help me, or give me a direction? Thank you!

Link to comment
Share on other sites

Since the Heal-function of the healer-component is not a local function, you can extend it easily.

-- Save the original function
var old_Heal = inst.components.healer.Heal

inst.components.healer.Heal = function(self, target)
	-- Take sanity from target
	if target.components.sanity then
		target.components.sanity:DoDelta(-5)
	end
	
	-- Take sanity from healer
	if self.inst.components.sanity then
		self.inst.components.sanity:DoDelta(-5)
	end
	
	-- Run the original function
	old_Heal(self, target)
end

 

Link to comment
Share on other sites

@Ultroman

Thanks for your help! And sorry that I'm too dumb to understand it clear.

Do I put these code in the item prefab like this

image.thumb.png.f1222c805f361110707780f61ae097e2.png

 

Or do I set up a new component 

image.thumb.png.5af0b4b7115dd9a3fae85bb3ff82640c.png

and call the component in the item prefab?

image.png.968820a1281da24a8f89d013e5b0826d.png

 

 

I tried for both and got the same error when creating the item. It says  '=' expected near 'old_Heal' , which is the first line of the codes you've provided.

Link to comment
Share on other sites

The first version is correct.

And I messed up :D I've been switching between C# and LUA, and in C# you can declare a variable with "var" but in LUA (in this case in particular) what we want is "local". Change the first line to:

local old_Heal = inst.components.healer.Heal

 

Edited by Ultroman
Link to comment
Share on other sites

@Ultroman

Ohhh thank you!! That worked!! But my character says "I can't do that" while healing (but everything is working as what I wanted.) I've checked my character's speech to make sure "I can't do that" is not any of his speech, so I'm not sure where is that coming from. One of the possibilities I was thinking about is I made this item be pickable only by my character. But that still happens when I removed that part.  And since it's now pickable by other characters, I've tried with Wilson and he said the same sentence. I'm not sure which line is causing this. Is it possible if you can have a look at my codes? Thank you.

 

giornobodyreplacementl.lua

Link to comment
Share on other sites

Oops, I forgot that Heal has to return a bool for whether the heal was possible. It's supposed to say "I can't do that." if you try to heal a player with no health-component. And since I never returned anything, it would always return "nil", which translates to "false", and therefore made your character think the heal failed, even though it worked fine. So, now we run the old Heal first and save its result, manually return if its result was "false", but continue to do our thing if it was "true".

-- Save the original function
var old_Heal = inst.components.healer.Heal

inst.components.healer.Heal = function(self, target)
	-- Run the original function, and return "false" if its result is "false". Otherwise, continue.
	if not old_Heal(self, target) then
		return false
	end
	
	-- Take sanity from target. Remove these four lines if you don't want this.
	if target.components.sanity then
		target.components.sanity:DoDelta(-5)
	end
	
	-- Take sanity from healer. Remove these four lines if you don't want this.
	if self.inst.components.sanity then
		self.inst.components.sanity:DoDelta(-5)
	end

	return true
end

 

Edited by Ultroman
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...