Super confused by label.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Footurist

I’ll boil this problem down to the minimum:

tool

extends Node2D

export (Font) var label_font setget _set_label_font
export (String) var label_text setget _set_label_text
var label_size
var ready = false
	
func _ready():
	ready = true
	_set_label_font(label_font)
	_set_label_text(label_text)
	print($Label.rect_size)

func _process(delta):
	print($Label.rect_size)
	
func _set_label_font(value):
	label_font = value
	if ready and value != null:
		$Label.add_font_override("font", label_font)
		
func _set_label_text(value):
	label_text = value
	if ready:
		$Label.set_text(label_text)

The tool has a label as its child. Whyyy has the label not updatet it’s rectangle at the END of _ready(), but at the first value that _process(delta) prints. I have trouble understanding this and it ruins my code.

:bust_in_silhouette: Reply From: Footurist

Solved. Seems like you have to update the position of it to be having the rect_size updating aswell.

:bust_in_silhouette: Reply From: volzhs

for performance reason,
some of properties are not updated immediately.

extends Label

func _ready():
	text = "test string"
	printt("size =", rect_size) # (40,14)

it’s just default size of Label

you can get the actual size of Label in several ways.

Wait for 1 frame

extends Label

func _ready():
	text = "test string"
	yield(get_tree(), "idle_frame")
	printt("size =", rect_size) # (65, 14)

Force to get size

extends Label

func _ready():
	text = "test string"
	printt("size =", get_combined_minimum_size())  # (65, 14)

Get size with signal

extends Label

func _ready():
	connect("item_rect_changed", self, "on_item_rect_changed")
	text = "test string"

func on_item_rect_changed():
	printt("size =", rect_size) # (65,14)