I have a Hero class that is based off of an Actor class, the actor class has a variable called health_points
and it is set to 10. If an instance of the Hero loses 3 health from an Enemy, then picks up a power up that brings him to full health. Is there a way to set the Hero's health variable back to the Actor Class' initial 10? (Without creating a const that is called FULL_HEALTH
).
Get Parent Class' Original Value of A Variable
- Edited
I think the easiest way is to save the value to another variable in _ready().
var full_health
func _ready():
full_health = health_points
This is a good method, because if you change health_points on a child class in the editor, it will save that value (meaning you can have different classes that all extend Actor and have their own max health).
However, you can also add an accessor function in the parent class, that returns the value you want.
For example, in the Actor class, add this function:
var health_points = 10
func get_health():
return health_points
In the Hero class:
print("full health: ", .get_health())
Notice the dot before the function call. This allows you to access the script you are extending.
Hope that helps.
3 years later
Megalomaniak added the Godot Help tag .