Topic was automatically imported from the old Question2Answer platform.
Asked By
potatobanana
for example every time i get hit by mob, my hp will decrease by 1. and i need to update my hp bar in my player hpBar and character Sheet . which one should i use, or there other way to update?
1 - with long node path like this(signal also pretty much like this)
player script
func get_hit():
get_node(" character sheet path").hp -= 1
get_node(" player path").hp -= 1
2 - put it in process
player script
func get_hit():
autoload.hp - 1
character Sheet script
func _process(delta: float) -> void:
if Hp != autoload.hp:
Hp = autoload.hp
Hello,
I think it’s best practice to let the hud manage itselft rather than burdening the player with this.
The way I would do it would be:
give the player a stats_changed signal that the player scene emits when an ui update is needed (that is, only on hits and such and typically not in _process),
make the player accessible globally (see below),
make ui things setup themselves in their _ready by connecting the globally accessible player’s stats_changed to a function that puts the current player values in your ui.
This way when the player needs an update it emits the stats_changed signal and all your ui parts will update.
A convenient way of making the player globally accessible is to:
create a e.g. Game or Globals singleton and put a var player in it,
in the player’s _ready make it register itself as the player: Globals.player = self
to decouple even more, you could pass the useful parameters to the signal (that’s what you would do with an event bus) so that you don’t even need global access to the player for more than connecting the signal.
I think that the first way will be better.
Because if you put anything in _process(delta) function then it would be excecuted at every frame. And hence in my project I always try to reduce the length of process function.
Moreover both ways are equally effective and so saving excecution of codes will be wiser.