Value of Var won't update

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

I’ve been trying to make a system where the value of a Var would be Updated by the text that the user Inputs, but it isn’t working, i’ve already did some testing and turns out the main problem is that the value of the Var (NT) isn’t updating, why is this happening?

extends LineEdit

func _process(delta):
	update()

var GridSize_Range = range(1,99)

var test = 19

var NT = 4

onready var GridGenerator_Script = load("res://Scripts/Grid Generator.gd").new()

func _on_LineEdit_text_entered(new_text):
	if new_text.is_valid_integer():
			NT = str(new_text)
			for i in GridSize_Range:
				var i2 = str(i)
				if NT == i2:
					GridGenerator_Script.ValidGridSize = true
					NT = i2
			if NT.is_valid_integer():
				self.clear()
	else:
		GridGenerator_Script.ValidGridSize = false
		self.clear()
:bust_in_silhouette: Reply From: skysphr

First of all make sure the method is actually triggered. The text_entered signal is emitted once the user presses Enter on the LineEdit. Is that what you want?

For clarity, you could keep NT as an integer throughout the code:

if new_text.is_valid_integer():
    NT = int(new_text)
    for i in GridSize_Range:
        if NT == i:
            GridGenerator_Script.ValidGridSize = true
            break; # No need to keep running the loop
    self.clear()
else:
    GridGenerator_Script.ValidGridSize = false
    self.clear()

In fact if this is all the code, you could further simplify it to:

if new_text.is_valid_integer():
    NT = int(new_text)
    GridGenerator_Script.ValidGridSize = NT in GridSize_Range
else:
    GridGenerator_Script.ValidGridSize = false
self.clear()

This also sets ValidGridSize to false is NT is not within the range. I am not sure if this is your intended behavior or not. You can even go further and turn it into a one-liner:

GridGenerator_Script.ValidGridSize = new_text.is_valid_integer() and int(new_text) in GridSize_Range

In any case it sounds like you are attempting to have a number input box with a limited range of values. Perhaps a SpinBox node would be more appropriate than a LineEdit in this case?

The code worked really well, thanks a lot!

Megatt4 | 2021-10-24 19:58