Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | LuminousNutria |
I have a label that I want to use to display the amount of happiness in the player’s tribe. I’m storing the happiness value as an int attribute in a global class/script called “Resources”.
I’m trying to use signals to update the Label’s text whenever Resources.happiness is modified, but I can’t figure it out.
How can I get the label to update it’s text whenever happiness is modified?
I have looked at tutorials and such for this, but I can’t seem to find anything that explains how to connect an attribute without using the 2d-editor. I don’t think I can use the 2d-editor if the script isn’t connected to a scene. I don’t want to arbitrarily connect the script to a scene just to do this.
resource_label class:
extends Panel
func _ready():
get_node("happiness_amount").connect("happiness", self, "updateHappiness")
func updateHappiness():
get_node("happiness_amount").text = str(Resources.getHappiness())
Resource class:
extends Node
var happiness = 25
func setHappiness(value):
happiness = value
emit_signal("happiness", str(value))
func getHappiness():
return happiness