Godot Version
v4.6.1.stable.official [14d19694e]
Question
I’m making a key node for my game’s controls menu. It’s simple enough: a label with the key’s name and a 9-patch background, yet something goes wrong.
If I input a bigger key name first and then change it to something smaller, the background doesn’t update correctly (when it’s a single letter, it should be square):
If I delete the key name string entirely and write it anew, it displays as intended:
Relevant code:
@export var key_name: String:
set(value):
key_name = value
print_key_name(value)
func print_key_name(key: String) -> void:
match key.to_lower():
"up":
$KeyLabel.text = "↑"
"down":
$KeyLabel.text = "↓"
"left":
$KeyLabel.text = "←"
"right":
$KeyLabel.text = "→"
_:
$KeyLabel.text = key
var text_length: int = $KeyLabel.get_text_length_pixels()
var nearest_multiple: int = 8 * ((text_length / 8) + 1)
# my attempt to make it look pretty since my font isn't monospaced
if text_length % 8 < 3:
size.x = nearest_multiple
$KeyLabel.size.x = nearest_multiple
else:
size.x = nearest_multiple + 8
$KeyLabel.size.x = nearest_multiple + 8
# container-related stuff
custom_minimum_size.x = 16 if size.x < 16 else nearest_multiple
$KeyLabel.custom_minimum_size.x = 16 if size.x < 16 else nearest_multiple
Where the hell is something going wrong? I can’t figure it out and it’s driving me crazy.


