Enter text using UI buttons?

Godot Version

4.4.1

Question

How would I implement a system where I can enter the frequency using the UI number buttons as shown below? With each digit pressed, one zero on the screen should be replaced by that digit. I tried using the line edit node, but it ended up being a bit of an immersion breaking mess.

Make a label with the default test “Enter freq: 00.000”. Then create a script for the label, the device and the buttons:

#label.gd
func update_label(number: float) -> void:
	text = "Enter freq: %6.3f" % [number]
#device.gd

var value = 0.0
@onready var multiplier = 1000.0 # So that the first place will actually be the 100s place

func update_value(number: float) -> void:
	if multiplier <= 0.0001: #If all the numbers are filled in
		return
	multiplier = multiplier / 10
	value += number * multiplier
	text = "Enter freq: %6.3f" % [value] #6.3f means six places total and 3 are after the decimal
#button.gd

@export var device: Control
@export var number: float  = 1.0


func _ready() -> void:
	pressed.connect(_on_button_pressed)


func _on_button_pressed() -> void:
	device.update_value(number)
5 Likes

it works lovely, thank you so much!! just one tiny correction in the update_label() function

text = “Enter freq: %6.3f” % [float] → produces an error.
text = “Enter freq: %6.3f” % [number] → this is what it should be

You are correct. I wrote all that code on here. I didn’t test it. I caught myself using float as a variable in one place but not the other. Apologies. I fixed my previous post. If that worked for you, please mark the post as the solution to help others find it in the future when googling.

1 Like