Invalid call. Nonexistant function 'is_valid_integer' in base 'int'.

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

I’ve tried multiple ways to handle invalid input from the user.
Not sure how to check for a valid int.

func _on_CalcResult_pressed():
		# get text from node as int
		var Quantity = int(get_node("../UI/QuantityLineEdit").get_text())
		if Quantity.is_valid_integer():
			print("Quantity")
			print(int(Quantity))
			print(" ")

		else:
			print("Please enter int above 0 for Quantity")
			print(" ")
:bust_in_silhouette: Reply From: kidscancode

is_valid_integer() is a String method:

Since you’ve already called int() it’s too late to check if it’s valid, you’ll just be getting a value of 0. (see int() ). You probably want to do something like:

var Quantity = get_node(../UI/QuantityLineEdit").text
if Quantity.is_valid_integer():
    Quantity = int(Quantity)

Then you can test the integer’s value as needed.

Thanks for pointing out the issue with is_valid_integer()
You answered the question I asked.

Here is what I ended up doing to solve my problem.

There were flaws in my error handling and I had previously tried to declare a new var within the function. Now I declare outside the function, then reassign to the get_node value. Included return on error.

var QTY

func _on_CalcResult_pressed():
		# get text from node as int
		#QTY = int(get_node("../UI/QtyLineEdit").get_text()) # method a works
		QTY = int(get_node("../UI/QtyLineEdit").text) # method b works
		# Handle 0 value input (before division)
		if QTY > 0:
   			print(int(QTY))
  
		# Handle any unwanted input other than an integer greater than 0
		else:
				print("Please enter a valid int above 0 for Quantity")
				return

GodotUser | 2019-07-10 07:01