Created
September 1, 2020 13:53
-
-
Save me2beats/147344241631007877eee2b948f54931 to your computer and use it in GitHub Desktop.
Godot IntegerField (LineEdit filter text)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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