How would I check if a string has any letters.

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

I am attempting to make an “Enter Seed” button for a cave generator in 2d. I need it so that the seed is only a number when you enter it. If you enter ANY letters as a seed, it will crash the game when I tries to generate tiles with a tilemap. I need it to check if theres any letters so that I can warn players to change the seed to any number. How would I do this?

:bust_in_silhouette: Reply From: DaddyMonster

Here you go:

func has_letters(your_string):
	var regex = RegEx.new()
	regex.compile("[a-zA-Z]+")
	if regex.search(str(your_string)):
		return true
	else:
		return false

print(has_letters("123")) # false
print(has_letters("abc")) # true
print(has_letters("a1")) # true

And the docs:

Regex is a famously difficult, if you only need it for this then I’d suggest just copying and pasting the method above and not worrying about how it works - RegEx really isn’t used in game dev that much. Your game might be unusual though of course, in which case, buy some aspirin before you study it.

:bust_in_silhouette: Reply From: DaddyMonster

I forgot to say yesterday, if you don’t want to deal with the hassle of testing and rejecting seeds then you can convert them to unicode and make them all ints:

func seed_to_int(seed_old):
	var seed_str = ""
	for chr in str(seed_old):
		seed_str += str(ord(chr))
	return int(seed_str)

And call it like:

printt("output:", seed_to_int("23423f324")) # outputs 5051525051102515052