FYI: You can get rid of the _ready() function too (or maybe better not :-))

Not required, but makes the code just a tat bit cleaner in my opinion: You could get rid of the _ready() function if you assign the max_health as a default value for the current_health variable, e.g.:

var current_health: int = max_health:

All a matter of opinion of course, but I’d thought I’d share it here anyway :wink:

I was wrong, setting the default doesn’t trigger the setter, so you will run into problems if your initial text is not set correctly.

So, therefore, setting the initial value via the _ready() is indeed the better way.

Sorry for the confusion, still leaving it here as there might be something to learn from my mistake :slight_smile:

3 Likes

Good reason! Good enough to add to this. Your original thinking is fine actually, it’s just that there’s a bit of a quirk with setting export variables that has to do with “order of operations”. In this example, you can add an @onready annotation and the code will work =)

  • @onready var current_health: int = max_health:

Hey, that’s nice and clean. Thanks for the tip!

1 Like

Privacy & Terms