0 votes

Hello, I am very new to Godot and I am trying to make it so when my player's health changes, it plays an animation. (for now im only worrying about any health change to play the animation).

My code is set so in my player script is a health variable

var playerHealth = 100 

And in the enemy script is a code that shoots a raycast and if it collides with the player, it changes the playerHealth to mins the damage

    if rayCast.is_colliding():
    var hit = rayCast.get_collider()
    if hit.is_in_group("Player"):
        target.playerHealth -= damage

While I could use a get_node command to make it play the animation from within the enemy script. it seems more ideal to make it play for when the health changes in the player script. and from some reading it seems setget is the best way to do this, But I am unsure of how to utilise it correctly still after reading up on it

Godot version 3.4 [206ba70f4]
in Engine by (17 points)

1 Answer

0 votes
Best answer
var playerHealth = 100 setget set_player_health
var max_health = 100

func set_player_health(value):
    playerHealth = clamp(value, 0, max_health)
    # alternatively: playerHealth = value

Note that if you want the setter to be called when you set the playerHealth variable from inside the script you need to use self.playerHealth = some_value. This will be changed in godot 4.0

by (3,707 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.