Problem with setget for health of a character

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

Hi ! I got a little trouble when making this turn based project : Whenever my projectile is hitting the target, hp of said target will go down (did it with a double print(hp) before and after the impact), but the progress bar and the label will not actualize themselves. I think that the setget isn’t working somehow and can’t figure out why.

Will gladly take any help !

Here is a miniature project

:bust_in_silhouette: Reply From: p7f

The problem is that you are setting the hp from inside the script with the setter defined. Setters and getters only works from ouside. If not, when you try to set the hp inside the setter, it will call again the setter and again and again (stack overflow). So, if you want to call the setter from inside the same script, you just have to call it as any other method, or use self. In your, simply change:

hp -= Global.damage
or to this if you want something more similar to what you’ve done:
self.hp -= Global.damage

To make it clear: the setter will only be called if you set the variable from other script, like if in your player scene call enemy.hp -= damage or something similar.

Thanks a lot ! I understand now :slight_smile:
Edit : It seems to work, but the value of my progress bar doesn’t seem to change, am I misstyping something ?

Rensae | 2020-06-29 18:10

I got this whit the code i gave you

Isn’t that bar the one which you needed to update?

p7f | 2020-06-29 18:21

Oh it worked for you ? Strange… I did it with a texture progress bar, but it doesn’t work

Rensae | 2020-06-29 20:56

Whith that very same project you shared me it doesnt work for you?

p7f | 2020-06-29 21:15

Yeah, I tried to replace progress bar by a Texture progress bar to see, but it doesn’t seem to work

Rensae | 2020-06-29 22:43