Skip to content

Instantly share code, notes, and snippets.

@me2beats
Created September 1, 2020 13:53
Show Gist options
  • Save me2beats/147344241631007877eee2b948f54931 to your computer and use it in GitHub Desktop.
Save me2beats/147344241631007877eee2b948f54931 to your computer and use it in GitHub Desktop.
Godot IntegerField (LineEdit filter text)
extends LineEdit
var regex: = RegEx.new()
var oldtext: = ""
func _ready():
regex.compile("^[0-9]*$")
connect("text_changed", self, "_on_LineEdit_text_changed")
func _on_LineEdit_text_changed(new_text:String):
var old_cur_pos = caret_position
if regex.search(new_text):
text = new_text
oldtext = text
set_cursor_position(old_cur_pos)
else:
var caret_offset = new_text.length()-oldtext.length()
text = oldtext
set_cursor_position(old_cur_pos-caret_offset)
func get_value():
return(int(text))
@me2beats
Copy link
Author

me2beats commented Sep 1, 2020

allow user to type/paste only digits

this is my attept to fix this solution https://godotengine.org/qa/39647/doing-a-integerfield
It had a buggy caret (edit cursor) positioning

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment