Have variable be called upon in label

Godot Version

4

Question

I created a measurement system that creates an arbitrary distance for inches. I want these inches to display in a label but am having trouble getting the variable to go into the RichTextLabel node.
Furthermore, how would I utilize @export? All the videos I’ve seen over it just says what you can use @export for.

You can use the @export keyword to get a reference for your RichTextLabel, see: GDScript exported properties — Godot Engine (stable) documentation in English

And you can simply set your RichTextLabel’s “text” property to set what it displays, see:

What would be the syntax to use that reference? I don’t know how to get the RichTextLabel to display the variable “inches” i got from my measuring script.

The syntax for a @export variable is much like any other variable definition, with @export at the back of it. You can use this variable like any other, though it must be assigned in the editor.

The annotation @export, keyword var, the name, then the type definition.

@export var rich_label: RichTextLabel

func set_inches_display(new_value: float) -> void:
    rich_label.text = "%d inches" % new_value

I meant to use it in the label. This code exports inches at a float. Im trying to use it in another script for a RichTextLabel to display that float


var is_dragging = false
var mouse_offset # on click
var delay = 5
var initial_pos
@export var inches : float

func _physics_process(delta):
	if is_dragging:
		var tween = get_tree().create_tween()
		tween.tween_property(self, "position", get_global_mouse_position() - mouse_offset, delay * delta)
		inches = initial_pos.distance_to(get_global_mouse_position())/100
		print(inches)
func _input(event):
	if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
		if event.pressed:
			if get_rect().has_point(to_local(event.position)):
				is_dragging = true
				mouse_offset = event.position - position
				initial_pos = self.position
		elif event.button_index == MOUSE_BUTTON_LEFT and not event.pressed:
			is_dragging = false

@export means you can edit the variable in the editor. Variables are already accessable to other scripts as long as you have a reference, that’s where @exporting nodes can help, @export for inches doesn’t help you since it’s overwritten every frame anyways, @export only sets a starting value, as defined in-editor.

You could either export to the rich text label like in my example

var initial_pos
var inches : float
@export var rich_label: RichTextLabel

func _physics_process(delta):
	if is_dragging:
		var tween = get_tree().create_tween()
		tween.tween_property(self, "position", get_global_mouse_position() - mouse_offset, delay * delta)
		inches = initial_pos.distance_to(get_global_mouse_position())/100
		rich_label.text = "%din" % inches

Or if you have a script on your rich text label you could export from that script to find this node with it’s inches variable

extends RichTextLabel

@export var inch_calculator_node: Node

func _process(delta: float) -> void:
	text = "%din" % inch_calculator_node.inches

So I did your 1st example, I’m getting “Invalid Assignment of Property or key ‘text’ with value of type ‘string’ on a base object of type ‘Nil’”
I apologize, I don’t understand @export that well.

You need to assign the exported variable in the editor. Click on your node in the scene tree, exported variables appear in the inspector like other properties.

Oh I understand. Thank you so much!