TemporarySolutn Posted June 26, 2019 Share Posted June 26, 2019 How would I go about giving a default character regeneration as long as condition is true? Link to comment https://forums.kleientertainment.com/forums/topic/107939-regeneration/ Share on other sites More sharing options...
Ultroman Posted June 26, 2019 Share Posted June 26, 2019 (edited) This is the easy way. Put this in your character LUA's master_postinit() or fn() function. -- DoPeriodicTask calls the given function every X seconds. -- I set it to 1.0. You can set it to e.g. 0.5 seconds if you want. inst:DoPeriodicTask(1.0, function(inst) -- Do nothing if the player is dead or doesn't have a health component for whatever reason. if not inst.components.health or inst.components.health:IsDead() or inst:HasTag("playerghost") then return end -- Do whatever checks you want, e.g., like the if-statement above. -- You can use whichever numbers you want here e.g. DoDelta(-2.5) to deal 2.5 damage. -- Or DoDelta(5) to heal 5 hp. The 'true' tells it to NOT pulse and make a sound every -- time the healing happens. It gets annoying. inst.components.health:DoDelta(1, true) end) But then the task always runs, and your checks are being done all the time. If your checks are simple, then it's alright. If they require a lot of data from different sources and measuring things, then perhaps it would be better to turn the task on and off at will. You can do that by adding this code to the character LUA, preferably at the top of the file because you will be using the functions at different spots in your code, so they need to be declared early. Here is the code: local myregentask = nil local function enableRegen(inst) -- Only do something if our task is not already running. if not myregentask then -- DoPeriodicTask calls the given function every X seconds. -- I set it to 1.0. You can set it to e.g. 0.5 seconds if you want. myregentask = inst:DoPeriodicTask(1.0, function(inst) -- Do nothing if the player is dead or doesn't have a health component for whatever reason. if not inst.components.health or inst.components.health:IsDead() or inst:HasTag("playerghost") then return end -- Do whatever checks you want, e.g., like the if-statement above. -- If you make sure to always disable the regen when the player dies and such, -- then you can skip checking a lot of things in here, making the task very light CPU-wise. -- You can use whichever numbers you want here e.g. DoDelta(-2.5) to deal 2.5 damage. -- Or DoDelta(5) to heal 5 hp. The 'true' tells it to NOT pulse and make a sound every -- time the healing happens. It gets annoying. inst.components.health:DoDelta(1, true) end) end end local function disableRegen() -- Only do something if our task is not already running. if myregentask then myregentask:Cancel() myregentask = nil end end Then you can call enableRegen(inst) or disableRegen() wherever you want, and since they take no parameters other than inst, you can hook them up to any event. UPDATE: Note that I have corrected a few things over the last couple of minutes. Edited June 26, 2019 by Ultroman Link to comment https://forums.kleientertainment.com/forums/topic/107939-regeneration/#findComment-1214184 Share on other sites More sharing options...
TemporarySolutn Posted July 23, 2019 Author Share Posted July 23, 2019 Either the first method doesnt work or im doing something wrong Link to comment https://forums.kleientertainment.com/forums/topic/107939-regeneration/#findComment-1230568 Share on other sites More sharing options...
Ultroman Posted July 23, 2019 Share Posted July 23, 2019 Well, the code looks solid, so you're gonna have to show me your code, and if you're getting a crash I'd like the log with the crash. Link to comment https://forums.kleientertainment.com/forums/topic/107939-regeneration/#findComment-1230788 Share on other sites More sharing options...
Serpens Posted July 25, 2019 Share Posted July 25, 2019 (edited) isnt it easier to use the health component with "StartRegen" and "StopRegen" ? So simply call inst.components.health.StartRegen(amount,period) when you want to start and inst.components.health.StopRegen() when you want to stop. Where to put this depends on your conditions, most often you will use ListenForEvent to listen for your condition. Edited July 25, 2019 by Serpens Link to comment https://forums.kleientertainment.com/forums/topic/107939-regeneration/#findComment-1232280 Share on other sites More sharing options...
Ultroman Posted July 25, 2019 Share Posted July 25, 2019 The problem with using StartRegen and StopRegen is that they don't stack, so any other regen, such as the one gained from certain foods, will be overwriting or get overwritten by your regen. Link to comment https://forums.kleientertainment.com/forums/topic/107939-regeneration/#findComment-1232305 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