Integer Data Validation

Godot Version

v4.4.1

Question

Hi all,

Quick question about data validation, I have a LineEdit node to input a integer for seed generation. Here’s a few screenshots:


The field gets colored green if its a valid int above 0 and below the max integer value (9223372036854775807), yellow if it’s a valid number but not set and red if its not valid.
For some reason, going slightly above the max value, say just by 1 or 2 above, the validation works fine, but when I go well above it such as putting an extra 0 on the end, it goes back to yellow and I start getting Debugger errors:

Here’s my code:

# Ready function
func _ready() -> void:
	# Seed buttons
	set_seed_button.pressed.connect(func() -> void:
		if seed_textbox.text != "":
			if is_numeric_string(seed_textbox.text):
				if int_within_bounds(int(seed_textbox.text)):
					dungeon_seed = int(seed_textbox.text)
					rng.seed = dungeon_seed
				else:
					error_text_label.text = error_text_03
					error_window.visible = true
			else:
				error_text_label.text = error_text_02
				error_window.visible = true
		else:
			error_text_label.text = error_text_01
			error_window.visible = true
	)

# Process function
func _process(delta: float) -> void:
	if seed_textbox.text == str(dungeon_seed):
		seed_textbox.self_modulate = Color.GREEN
	elif !is_numeric_string(seed_textbox.text) or !int_within_bounds(int(seed_textbox.text)):
		seed_textbox.self_modulate = Color.RED
	else:
		seed_textbox.self_modulate = Color.YELLOW

# Check if a string is made of numbers only
func is_numeric_string(text: String) -> bool:
	if text.is_empty():
		return false
	for chars: String in text:
		if not (chars >= "0" and chars <= "9"):
			return false
	return true

# Check whether seed is within integer bounds and is positive
func int_within_bounds(int_check: int) -> bool:
	if int_check <= 9223372036854775807 and int_check >= 0:
		return true
	return false

Does anyone know a better integer validation technique?

You could use String’s .is_valid_int() for the first part, but this also doesn’t catch nubers past the signed 2^63-1 limit. A quick extra check could be the length of the string, in decimal it can’t be over 19 characters long, this at least fixes the “adding an extra zero” case. To be totally accurate you may have to implement your own int from string converter that can assert INT_MAX - new_number > current_number

Ahh you’re right, I can do any number up until I hit the 20th character.
Surely there’s something built in to check that though right?

Doesn’t look like there’s a built-in function, the engine uses this to_int conversion very often and must be as fast as possible so it doesn’t return or throw error values, instead defaulting to INT_MAX (or INT_MIN when negative) and printing that error message. If you’re OK with using that one value as a sentinel you could check equality, it’s only one less seed, and you’ve already chopped off the negative values.

func is_valid_int(text_intput: String) -> bool:
    const INT_MAX = 9223372036854775807
    var as_value: int = text_input.to_int()
    return as_value != INT_MAX

I ended up just making a function to count the length of the String like you suggested, wasn’t too hard to implement. Thank you for your help mate, I appreciate it.
For anyone interested, here’s how I fixed it:

func check_character_limit(string_check: String) -> bool:
	if string_check.length() <= 19:
		return true
	return false

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.