0 votes

I am using a LineEdit node for a numerical input (have that figured out), but also need to restrict how high or low the number is. If they enter a number above the range, it should display the highest number possible, if lower, then the opposite. Kind of like what "clamp" does, but I can't seem to get clamp to work in my code.

I am using the following code:

extends LineEdit
var regex = RegEx.new()
var oldtext = ""
func _ready():
    regex.compile("^[0-9]*$")
func _on_LineEdit_QTY_text_changed(new_text):
    if regex.search(new_text):
        text = new_text   
        oldtext = text
    else:
        text = oldtext
        set_cursor_position(text.length())
func get_value():
    return(int(text))

Any help is greatly appreciated.

Godot version 3.2.3
in Engine by (29 points)

Can't you convert the string into an integer and clamp it using clamp(value, min, max)? I think it should work.

1 Answer

0 votes

Why not use a SpinBox? It has that functionality already.

But if you want to use a LineEdit yeah just use Bubu's answer.

by (8,546 points)

I am using a modified LineEdit because you cannot modify the font size in a SpinBox.

You can. If you check the Remote tab, you can see that the SpinBox has a LineEdit inside of it. Just change the font size of the LineEdit in the Theme.

Thanks for this tip. However, when I try to assign a font (switch to New dynamic font), it immediately switches back to [empty]. Same for any of the other custom styles.

Really? That's strange, it shouldn't do that

Was finally able to get the theme working, but now I have the issue that when a larger font is used, it expands the width of the SpinBox and you can't adjust it smaller.

You can try adjusting the top and bottom spacing of the font but yeah the font sets the min size

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 Frequently asked questions and 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 [email protected] with your username.