Jump to content

Help with health drain code?


Recommended Posts

Hi, I'm trying to make a character mod with a character who transforms. I've figured out how to set the character to transform while having 0 sanity (thanks to the forums here), and I would like to enact a health drain effect on her while she is transformed. However, I can't quite figure it out, and I can't find any examples to follow here. Any help is greatly appreciated! Here is my code. Right now it just kills me upon transforming.

local function becomesabirust(inst)
        inst.rust = true
        inst.AnimState:SetBuild("sabirust")
        inst.Transform:SetScale(1, 1, 1)
         inst.components.combat.damagemultiplier = 2.5
        inst.components.health:SetAbsorptionAmount(0.5)
        inst:AddTag("monster")
        if inst.components.health.currenthealth > 1 then
        inst.components.health:DoDelta(-1 * inst.components.health.currenthealth)
        end
end

 

Link to comment
Share on other sites

I'm not very experienced myself in the ways of coding myself, but I think I know why your character is dying from simply transforming.
From what I can read, this is the problem:

        if inst.components.health.currenthealth > 1 then
	inst.components.health:DoDelta(-1 * inst.components.health.currenthealth)

To me, that's read as "If the character's HP is above 1, then take ALL of their health." This is probably why your Rust Mode is killing you upon activation.

As for actually applying a health drain, there's a character mod in the Steam Workshop for Archer from Fate/Stay Night who has that health drain you're looking for.  (The coding for that is in the character's prefab script. (archer.lua))

http://steamcommunity.com/sharedfiles/filedetails/?id=544637010

I'm not sure how to make it so it deactivates upon going back to normal, though... (If that's necessary)
I hope this helps at least a bit until someone with more experience comes along!
 

On a side note, I'm guessing you're making a Sabitsuki/Rust mod? (Used to play .flow and a lot of others way back)

Link to comment
Share on other sites

18 minutes ago, K-KyoruMii said:

I'm not very experienced myself in the ways of coding myself, but I think I know why your character is dying from simply transforming.
From what I can read, this is the problem:


        if inst.components.health.currenthealth > 1 then
	inst.components.health:DoDelta(-1 * inst.components.health.currenthealth)

To me, that's read as "If the character's HP is above 1, then take ALL of their health." This is probably why your Rust Mode is killing you upon activation.

As for actually applying a health drain, there's a character mod in the Steam Workshop for Archer from Fate/Stay Night who has that health drain you're looking for.  (The coding for that is in the character's prefab script. (archer.lua))

http://steamcommunity.com/sharedfiles/filedetails/?id=544637010

I'm not sure how to make it so it deactivates upon going back to normal, though... (If that's necessary)
I hope this helps at least a bit until someone with more experience comes along!
 

On a side note, I'm guessing you're making a Sabitsuki/Rust mod? (Used to play .flow and a lot of others way back)

ahhh thank you very much!! this will be super helpful! :] it's always difficult for me to find stuff like this if its not right in front of me

and yup!! I'm making a Sabi mod for my friend because he loves her. Personally I've never played .flow, but I loved Yume Nikki back in the day

Link to comment
Share on other sites

 

This is my code now. When I transform, my character only takes 1 point of damage, instead of the continuous damage that DoPeriodicTask implies. Does anyone know how to change it? If I put the health drain code in with the code that transforms the character, then it doesn't turn off when you die, or when you reset the transformation.

local function rust_drain(inst)
	inst.drain = true
	if inst.components.health then
	inst.components.health:DoDelta(-1 * 4, true, "Rust")
	end
	inst.rust_speed = 1
	inst.rust_task = inst:DoPeriodicTask (inst.rust_speed, inst.rust_drain )
end

local becomeformtreshold = (0)
local unbecomeformtreshold = (150)
local function sanity_event_listener(inst, data)
	if inst.components.sanity.current <= becomeformtreshold 
	and not inst.rust 
	then
		becomesabirust(inst)
		rust_drain(inst)
		elseif inst.components.sanity.current >= unbecomeformtreshold 
		and inst.rust 
		then
        becomesabi(inst)
		end
end

 

Link to comment
Share on other sites

@shirusnake Can I ask what component you're using for your transformation?

I can tell you the reason why the health drain wont happen over time is because the health component is only set up to update itself whenever you're taking fire damage. The reason effects like weather damage and hunger do health damage over time to you is because those particular components are set to be updated constantly and they remotely drain your health using DoDelta in their respective OnUpdate() functions.

If you want to try the way the Archer mod did it with the DoPeriodicTask function instead, you'll have to change a few things about your code.

First, you'll want to keep the "rust_drain" function just based around the health drain, because I believe by reassigning the task with the same function your trying to call with it I believe will cause an error (not necessarily a crash) but will cease it from working. Reassign the periodic task back to your transform code instead then all you should have to do to stop it is use something like this in whatever code that takes you out of your transform state:

if inst.rust_task ~= nil then
	inst.rust_task:Cancel()
	inst.rust_task = nil
end

Try changing that up and see if it works for you

Edited by w00tyd00d
Link to comment
Share on other sites

12 hours ago, w00tyd00d said:

@shirusnake Can I ask what component you're using for your transformation?

I can tell you the reason why the health drain wont happen over time is because the health component is only set up to update itself whenever you're taking fire damage. The reason effects like weather damage and hunger do health damage over time to you is because those particular components are set to be updated constantly and they remotely drain your health using DoDelta in their respective OnUpdate() functions.

If you want to try the way the Archer mod did it with the DoPeriodicTask function instead, you'll have to change a few things about your code.

First, you'll want to keep the "rust_drain" function just based around the health drain, because I believe by reassigning the task with the same function your trying to call with it I believe will cause an error (not necessarily a crash) but will cease it from working. Reassign the periodic task back to your transform code instead then all you should have to do to stop it is use something like this in whatever code that takes you out of your transform state:


if inst.rust_task ~= nil then
	inst.rust_task:Cancel()
	inst.rust_task = nil
end

Try changing that up and see if it works for you

This code works perfectly thank you so much!! :D

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