0 votes

I.e. the limit is 12 and the user introduces 13, the 3 is erased correctly in my code but the cursor goes in front of the 1 (to the left).
I want it to be behind the 1 (otherwise, the code could be broken).

func _on_bill_box_text_changed(new_text):
    bill_box = $bill_box.text
    billNum = int(bill_box)
    if billNum > cent1:
        bill_box.erase(bill_box.length() - 1, 1)
        $bill_box.text = bill_box
in Engine by (122 points)
edited by

1 Answer

+1 vote
Best answer

You can manually set the carat_position, so adding something like this to the end of the above script should work:

$bill_box.caret_position = bill_box.length()

That said, I'm not sure it's very user friendly, but it should do what you want...

Note, since you didn't format your code for the forum, it's difficult to tell where you're using "_" chars in the var names as they get "eaten" by the forum as italics markup. So, my use of underscores in the above may or may not be correct. Adjust as necessary.

To prevent that confusion, format your posted code via the "{ }" button in the forum editor's toolbar.

by (21,800 points)
selected by

Looking at the LineEdit docs, there's a better way than what I posted above. Just call delete_char_at_cursor() if the value is > than your max value (12 I think).

So...

if billNum > cent1:
    $bill_box.delete_char_at_cursor()

Additionally, depending on your needs, you might take a look at the SpinBox control, which might be more suitable as it's designed for numeric input and will allow you to set a min and max value as needed.

It worked, thanks! I fixed the description now.

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.