A timer wont start even though the statement is true!

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Dragon20C

Im trying to make a health regen system and I thought of using a timer since I dont want to regen straight away, but for what ever reason the timer doesnt start no matter what even when the statement is true, Health starts at 120 but I take damage and it still doesnt start even though it becomes true when health isnt 120 anymore.

here is the code: Legobet88 🀄 Trustworthy Agen Situs Slot Online Bet88 Terpercaya Ter- Gacor Hari Ini 2024

:bust_in_silhouette: Reply From: Magso

So if health runs down to zero and regen is set to true this code,

if Health != 120:
    health_regen.start(0)
else:
    regen = false
if regen:
    Health += 2 * delta

Is basically saying: Is Heath 120? No it’s 0, goes to else statement and regen is now false. Is regen true? No it’s just been set to false.

To get the transition for the regen use a lerped value.

if regen:
    Health = 120
lerped_health = lerp(lerped_health, Health, 2 * delta)
#display lerped_health instead of Health

The problem is the timer for example if the health isn’t 120 anymore makes it something else other then 120 so it should become true which it does by using a print statement to see if it turns true and it does but the timer doesn’t even start, the issue is the timer not the else statement.

Dragon20C | 2020-12-12 19:17

:bust_in_silhouette: 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