4.4
Here’s the code
func take_damage(damage_to_take): if Playerglobals.health <= 0: Animationplayer.play("hurt") Playerglobals.health = 2 await Animationplayer.animation_finished get_tree().reload_current_scene.call_deferred()
Here’s the pic of my code because i don’t know why my code has no indents in the text version
And here’s what’s happening
So the usual way to d!e is when you get hit one more time at zero, but in the video, you can clearly see that it became -1 instead of me dy!ng, does <= not mean ‘lower or equal to’?
Thanks for listening
Oh yeah by the way the slime enemy deals 1 damage and the among us deals 2 damage
Hi Isabelle,
You pass the damage_to_take
to your function but do nothing with it in this function?
Where are you subtracting it from your player’s health?
PS: you can put three ``` in a line before and after your code block to make it look nice in the forum.
1 Like
There’s a “damage_to_take” in the take damage function because i want different enemies to do different damage, and i forgot to mention i use “playglobals.health -= damage_to_take” to decrease the hp.
Thx
Oh, sorry I just had a better look at your screenshot. You do that check for the player’s health on the Boss
instead in your player’s script. Though, I must admit, it sounds like a fun mechanic, you can take indefinite damage but die as soon as you hit the boss 
1 Like
Thanks! But… i dont d!e from the boss if i get hit by the slime-o THEN get hit by the boss, i do d!e when i get hit twice from the boss, so for example: since my hp is 2.0 and the boss deals 2 damage, after the boss deals 2 damage, i have 0 hp (i still dont die when i reach 0, i die when i reach below 0) then if i get hit by the boss again… then i die! But… if i get hit by Slimey instead of the Boss first, then get hit by the Boss… then BOOM, I get -1 Hp! Why?? Like if slimey dealt one damage… then the boss dealt 2 damage… thrn that should mean im dead right? NOPE, I have -1 HP, Go check the video i sent if you dont get it!
Your function is weird imho.
func take_damage(damage_to_take):
if Playerglobals.health <= 0:
Animationplayer.play("hurt")
Playerglobals.health = 2
await Animationplayer.animation_finished
get_tree().reload_current_scene.call_deferred()
So you have: a damage_to_take
param that’s not used at all (as @ananasblau said). Then, you start by checking if health is <= 0, meaning that you want to execute the damage function, only if player is dead? That does not make any sense to me, you want to damage a player that’s not dead already.
Also, the only time in your code when the health is updated is the Playerglobals.health = 2
part, which is also very strange and is definitely not the reason the value goes at -1 (since, well, you assign it to 2).
What I’m thinking is that the issue you’ve got must be related to something else we don’t see. Here’s what you can do to debug :
- Look for every functions changing the value of
Playerglobals.health
and add a print, so that you can easily track what’s happening to your player health.
- When reducing the health, ensuring you clamp it to 0 so that is does not go to negative values. Except if that’s by design for some specific reason, you never want health points to go below 0.
Finally, just about that question:
does <= not mean ‘lower or equal to’
It does. That’s why I’m guessing the issue is somewhere else and not related to the code you shared.
2 Likes
@isabelle_alejar where is the function that actually reduces the player’s health? Can you post that here?
I’m 100% sure that the boss’ take_damage
function is called before you subtract the damage from the player.
1 Like
The issue is likely that you’re checking the player’s health before applying the damage. If this is the case subtract the Playerglobals.health by damage_to_take before the code does a health check. Hope this helps!
1 Like
Hi, did you check my reply? I did “playerglobals.health -= damage_to_take”
I added the take damage fubction to the player script
And since everyone’s confused… i’ll show the whole code of the function:
func take_damage(damage_to_take):
if Playerglobals.health <= 0:
Animationplayer.play("hurt")
Playerglobals.health = 2
await Animationplayer.animation_finished
get_tree().reload_current_scene.call_deferred()
if doDash == false:
#animated_sprite.stop()
ouchy = true
Animationplayer.play("hurt")
HitDurationRimer.start()
#await get_tree().process_frame
await Animationplayer.animation_finished
#invincibilitytime.start()
#$CollisionShape2D.disabled = true
Playerglobals.health -= damage_to_take
I added the code on the player script.
Thanks!
1 Like
Thanks for pasting the script! I agree with what @sixrobin said, checking player health before subtracting from it will lead to negative health values without dying.
Imagine your function line-by-line with 1
player health
- before anything, check if the player has less than or zero health (dead)
a. if they are already dead, play hurt
b. set health back to 2
c. wait for animation to finish
d. reload the scene (on frame end)
- if doDash is false
a. play hurt
b. wait for hurt to end
c. reduce health by 2 (thus we are at -1 health)
- function ends!
If you swap operations 1 and 2 then you will be checking for death after taking damage, which makes more sense than checking for death before taking damage.
3 Likes
I did! But depending on where in the function the health is decreased, it’s hard to know what’s wrong exactly. Also, I was trying to give an explanation of what was wrong in your logic more than fixing the code directly (which, I believe, is more interesting to learn
)
Anyway, I think @gertkeno gave you a neat answer now that you shared the full function!
1 Like
Hi, it works! It solved my -1 issue! But… there’s a new problem… when i get hit by slimey, it decreases by 1, that’s normal… but when i get hit by slimey the 2ND time… It damages me TWICE, TWICE, It should be ONCE not twice…
See here’s the new code
func take_damage(damage_to_take):
if doDash == false:
#animated_sprite.stop()
ouchy = true
Animationplayer.play("hurt")
HitDurationRimer.start()
#await get_tree().process_frame
await Animationplayer.animation_finished
#invincibilitytime.start()
#$CollisionShape2D.disabled = true
Playerglobals.health -= damage_to_take
if Playerglobals.health <= 0:
Animationplayer.play("hurt")
Playerglobals.health = 2
await Animationplayer.animation_finished
get_tree().reload_current_scene.call_deferred()
And here’s the clip
Sorry for bad editing
Let’s go line-by-line and look at animations then
- if doDash is false
a. play “hurt”
b. wait for animation to finish…
c. deal damage
- if dead
a. play “hurt”
b. set health to 2
c. wait for animation to finish…
d. reload scene
- done
Notice how you play and wait for the animation when taking damage, and play the animation when dying?
2 Likes
So do i remove the waiting for the animation in the dy!ng part?
I would remove 2a and 2c, playing the animation (for the second time) and waiting for the animation_finished as well.
1 Like
Playerglobals.health -= damage_to_take
if Playerglobals.health <= 0:
Animationplayer.play("hurt")
Playerglobals.health = 2
await Animationplayer.animation_finished
get_tree().reload_current_scene.call_deferred()
return
elif doDash == false:
ouchy = true
Animationplayer.play("hurt")
HitDurationRimer.start()
await Animationplayer.animation_finished
1 Like
i added the play animation when dy!ng because i was planning to add a dea+h animation, so should i still remove it?
Thanks