0 votes

I put a variable to stay in a LABEL, but when I update this variable this LABEL does not update.

extends CanvasLayer
export var point = 0

func _ready():
 $Label.text = str (point)

Would I need to change the STR to something else?

Godot version latest
in Engine by (24 points)

2 Answers

0 votes

When you set some property or variable, it gets set to value at that point of time. If you want the value to be updated, you need to update it regularly. While Godot doesn't support such binding out of the box, it's not hard to make. Put this code into your label:

extends Label

var _target_object
var _target_property

# Use this method to set text
func bind_text(target_object, target_property):
    _target_object = target_object
    _target_property = target_property

func _process(_delta: float) -> void:
    text = str(_target_object.get(_target_property))

Put this instead of your current text setting:

$Label.bind_text(self, "point")

The text should now update every frame.

by (810 points)
0 votes

What you want to do is update the text property of the label, when the var point changes value. This is very easy to do with setters and getters (I recommend you read on them since they are super useful). Lets use a setter on the var point, which triggers stuff when it changes value. First you do this:

var point = 0 setget set_point

where setget is a keyword and set_point is just the name of the setter function you will define later, like this:

func set_point(value) -> void:
    point=value
    $Label.text=str(value)

And you are done! just remember that setters only trigger when another script modifies the value of point, and if you are modifying it in this same script just use self.point = new_value, to trigger the setter

by (418 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.