![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | Naowut |
Im following along with a tutorial and succeeding in converting it to godot 4 so far. However, I’m not quite sure how to use the new setters with a connect. Below is my code.
var hearts = 100:
set(val):
hearts = setHearts(val);
get:
return hearts;
func _ready():
self.hearts = PlayerStats.health;
PlayerStats.connect("health_updated", setHearts);
func setHearts(val):
print(val);
print(hearts);
return clamp(val, 0, max_hearts);
print val = correct
print hearts = not correct
This is most likely because the setHearts function just returns a value which I dont assign to hearts. I’m not sure what the best way is to call the setter from connect. Setting hearts inside the function like I would expect throws an infinite recursion error.
Fixed it by adding a new function updateHearts but I think its a bandaid and would like to set it directly from the connect() function…
var hearts = 100:
set(val):
hearts = setHearts(val);
get:
return hearts;
func _ready():
self.hearts = PlayerStats.health;
PlayerStats.connect("health_updated", updateHearts);
func updateHearts(val):
hearts = val;
func setHearts(val):
return clamp(val, 0, max_hearts);
Naowut | 2023-01-09 19:27
Hello, thank you for the solution, I modified it a bit to not have 2 functions, clamp(val, 0, max_hearts) can be moved directly in the health’s setter (instead of another function) like in the initial tutorial (which I am also following) and then made updateHearts into set_hearts to look more like the tutorial’s code, this is the result and it works in Godot 4.0.2:
extends Control
@onready var label = $Label
var hearts = 4:
get:
return hearts
set(value):
hearts = clamp(value, 0, max_hearts)
func set_hearts(value):
hearts = value;
var max_hearts = 4:
get:
return max_hearts
set(value):
max_hearts = max(value, 1)
func _ready():
self.max_hearts = PlayerStats.max_health
self.hearts = PlayerStats.health
PlayerStats.health_changed.connect(set_hearts)
FIREHIVE | 2023-04-22 09:11