|
|
|
|
Reply From: |
Tato64 |
First of all i want to point out that using “!= 120” (Which means “Anything that is not exactly 120”) is a problem if the life regeneration has no max ammount scripted. (I.e. the var is not clamped
)
Because you could be regenerating PAST 120, and for example, 150 is in fact “!= 120”, which would mean you keep regenerating forever.
Second of all, im not sure how the damage is handled, but this code is using “Health” and not “Current health” maybe that’s the problem?
Finally, you’ll find the ways that work best for you, but for something like this i like to use two timers.
One timer is for the time it takes before life starts to regenerate, and another timer for the regeneration speed.
It’d be something like this:
if current_health < 120: #If health is less than 120...
health_regen.start() #Starts the healing cooldown
func _on_health_regen_timeout(): #Once the cooldown is finished...
$Regeneration.start #It starts the actual regeneration.
func _on_Regeneration_timeout(): #Every time the regeneration completes a cycle...
current_health += 1
if current_health == health:
$Regeneration.stop()
(Important: “Health_regen” is a one-shot timer, “regeneration” is not.)
You should also add a…
current_health = clamped(current_health, 0, 120)
somewhere in the process code to prevent health from going below min or above max values
Thanks ,I only gave everyone a small snippet of the code and I have clamped health it so it won’t pass 120 as too why I have two different health options is because health is the actual player health and current health smooths it out on the health bar instead of jumping to the health, it adds or takes away a number to reach health, and when I get home I will test your code, a little thing to add when I add the start timer code into say a button press it works but not when If health != 120 meaning it can be anything else, should make it true which it does but the timer just won’t start does having the timer in the physics process make a difference?
Dragon20C | 2020-12-12 19:33
Hmm, the fact that it’s not triggering can depend on a lot of things, but it’s likely just an oversight in the code. I’d “follow” the whole process with prints to try and find out where is it failing, lol.
Also, having the timer in the physics process shouldnt be a problem, but it depends on how the “damage taking” is handled.
For that i’d write a function like this:
func takedamage(damage):
health -= damage
and then have conditions in the physics process (or wherever you need them) that call this function, for example:
if [something that causes damage]:
call("takedamage", 50) #In here, 50 is what "damage" will be in the "takedamage" function
Tato64 | 2020-12-12 19:55