Setget in subclass

Godot Version

4.x

Question

There is a class for Character

class_name Character
extends Resource
//....more..
@export var hp:float 
@export var hp_max:float
@export var atk:float
@export var def:float

after that , I made a new subclass

class_name PlayerDM
extends Character

@export var hp_max: float = 100.0: 
	get:
		return hp_max
	set(value):
		hp_max = value
		UI_EVENT.hp_info_changed.emit(hp, hp_max)
		
@export var hp: float = 100.0 :
	get:
		return hp
	set(value):
		hp = value
		UI_EVENT.hp_info_changed.emit(hp, hp_max)


I know there are some error in this code, redefine I mean.
But what Can I do for override the subclass’s set and get ?

I believe the function would be set_hp_max(x) and get_hp_max(x) in the subclass, but you would have to define the set/get in the parent class first

1 Like

Try using the alternative syntax for getters and setters

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.