menghuancat Posted February 16, 2019 Share Posted February 16, 2019 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: Heals health when used Decrease sanity 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 More sharing options...
Ultroman Posted February 16, 2019 Share Posted February 16, 2019 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 More sharing options...
menghuancat Posted February 17, 2019 Author Share Posted February 17, 2019 @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 Or do I set up a new component and call the component in the item prefab? 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 More sharing options...
Ultroman Posted February 17, 2019 Share Posted February 17, 2019 (edited) The first version is correct. And I messed up 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 February 17, 2019 by Ultroman Link to comment Share on other sites More sharing options...
menghuancat Posted February 20, 2019 Author Share Posted February 20, 2019 @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 More sharing options...
Ultroman Posted February 20, 2019 Share Posted February 20, 2019 (edited) 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 February 20, 2019 by Ultroman Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now