0 votes

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()
Godot version Godot 3.2.3
in Engine by (34 points)

1 Answer

+1 vote
Best answer

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?

by (1,261 points)
selected by

The code worked really well, thanks a lot!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.