- Edited
DaveTheCoder
After getting stuck i cam up with this
and it says "null instanace" when i run the code. to the text_changed.connect(_on_username_input_text_changed) line why is that?
DaveTheCoder
After getting stuck i cam up with this
and it says "null instanace" when i run the code. to the text_changed.connect(_on_username_input_text_changed) line why is that?
Place ~~~ on lines before and after the code in your post.
Also, explain what you want the code to do.
DaveTheCoder
Sorry idk how to make all code stuff :/
I am using a LineEdit to make a person write a username (email & password later) then add boundary's like a word limit and a unexpected like special characters when typing the username.
This code i have i think, takes an input from the user converts it into a var(current1) then checks from the compile list (RegEx) each word input then spits it back out with the usernameEntry piece of code i think.
TheRedPandaKing Sorry idk how to make all code stuff :/
To post code here:
A. Type ~~~
on a line by itself, before the code.
B. Copy/paste the code from the Godot editor.
C. Type ~~~
on a line by itself, after the code.
TheRedPandaKing add boundary's like a word limit and a unexpected like special characters when typing the username
By "word limit" do you mean character limit? Do you want to limit the length of the username that the user can type? You can do that by setting the LineEdit's max_length property.
What do you want to do if the user types special characters? Discard them? Display an error message to the user?
TheRedPandaKing it says "null instanace" when i run the code. to the text_changed.connect(_on_username_input_text_changed) line why is that?
extends Control
@onready var usernameEntry: LineEdit = %usernameEntry
func _ready():
usernameEntry.text_changed.connect(_on_username_input_text_changed)
usernameEntry.grab_focus()
usernameEntry.text = ""
func _process(delta):
pass
func _on_create_button_pressed():
print("create pressed")
func _on_username_input_text_changed(new_text):
var current1:String = ""
var regex1: RegEx = RegEx.new()
regex1.compile("[A-Za-z0-9]")
for valid_character in regex1.search_all(new_text):
current1 += valid_character.get_string()
usernameEntry.set_text(current1)
print(new_text)
Idk it wont copy pate correctly, this is the best i can do :/
You need three (3) tilde's ~~~,
not two ~~.
This is your last chance. One more mistake and I'll transfer you to xyz.
The max length piece of code at the end is so when they type over the limit of 5, i want all new inputs to be discarded expect if they press backspace. and if they were to press a special character i want it to be discarded and also send a signal, so i can make a pop up visible saying incorrect input--. further on after i understand it how to do this i want to add in more unexpected inputs to then cancel them ie they press create with nothing inside the username Another pop next to the username will say "you need a text to create a login".
i used This as a source for the code
If the script you posted is attached to the node register_screen, then this is how to reference its LineEdit child node UsernameInput:
@onready var usernameEntry: LineEdit = $UsernameInput
Try that, and add a print statement in _on_username_input_text_changed
to verify that it's called when the user types in the LineEdit.
DaveTheCoder
Thank you holy, it works great!
how would i make (label.visible = true) when it detects an input thats not apart of the RegEx compile list. would i need to add another for loop that checks their inputs?
it would be great
According to the documentation, that signal would be connected the same way as the text_changed signal.
DaveTheCoder
What do you mean im not sure i understand, i also don't now how to connect a signal to the for loop that's adding in the valid characters.
And also how would i turn the current1 into a variable outside of the filed so that i can check if it's empty or is their a better way?
TheRedPandaKing how to connect a signal to the for loop that's adding in the valid characters
Why are you using a loop?
You need to explain clearly and completely what you want to do. Pretend that you've hired a programmer to write the code that handles the UsernameInput, and you're giving him the specification for how it will behave from the user's perspective. The programmer will use the specification to write the code without any further communication with you, so it must be complete.
Ill do my best
extends Control
@onready var ULT = $UsernameInput/UsernameLengthTag
#@onready
@onready var UNIT = $UsernameInput/UsernameNoInputTag
@onready var usernameEntry: LineEdit = $UsernameInput #sets whatever is in the Username lineedit into a var
const i: int = 1
var username: String = ""
func _ready():
usernameEntry.text_changed.connect(_on_username_input_text_changed) # i think it connects the current lineedit text to whats being changed below
usernameEntry.grab_focus()
usernameEntry.text = ""
func _process(delta):
pass
func _on_create_button_pressed():
print("create pressed")
print(username)
func _on_username_input_text_changed(new_text):
var old_caret_position: int = usernameEntry.caret_column
var regex1: RegEx = RegEx.new() #this is the spesfici list of compiled RegEx letters
regex1.compile("[A-Za-z0-9]")
var current1:String = ""
var diff: int = regex1.search_all(new_text).size() - new_text.length()
for valid_character in regex1.search_all(new_text): #searchs the inputs that are being taken from a list in (regex1.compile)
current1 += valid_character.get_string() #adds what ever is being input after being checked into the (current1 string)
#add a way to add a signal-that is played when you input somthing not in the compile list
usernameEntry.set_text(current1) #set's what the (current1) is to the usernameEntry which is being displayed
usernameEntry.caret_column = old_caret_position + diff
print(current1)
if current1.length() >= 12:
ULT.visible = true
elif current1.length() <= 12:
ULT.visible = false
I want to have a user be able to create a Username that saves to a file on their computer. The username must not allow space's, special characters and cant be longer then 12. If the user inputs a special characters/space, goes over the word limit, or inputs nothing. I want to display a pop up that tells them they can't do that respective thing.
I also want to be able to do this with a Email input where it checks if you have a @ and a .com at the end. But also a password that wants you to input at least 1 number.
How i do this im not sure, im very new to godot and coding in general.
TheRedPandaKing cant be longer then 12
TheRedPandaKing must not allow space's, special characters
Instead of looping through text, after the user presses the submission button, you can use a single RegEx check, or a String method call, to determine whether the user's input contains any of those characters. If it does, you can display a message that explains that the username must not contain those characters. Then the user would have to make the corrections himself and resubmit.
Another choice is to silently remove spaces and special characters as the user types them. That can be done with a single RegEx replacement, or with the String method call I posted above. After the user presses the submission button, you could optionally display a message that spaces and/or special characters were removed; but that's probably not needed, since the user can see the username that's displayed.
In any case, there could be a static note near the input field that explains the restrictions.
The email and password could be handled in a similar way.
Does the above seem reasonable?
DaveTheCoder
The reason i am creating this is for a school submission, that is why i am looking to only have the message pop up after they come into a restriction.
The exact way you are explaining in 2. looks great the only problem is how do i learn or code it, as i said i am new to coding but i don't mind learning if their is a beginner friendly video which has all of this information that would be great since i have already looked on how to do this but i cant find a concrete tutorial on checking it with RegEx. If you could show me how that would be great.
I can provide an example of using a RegEx for that.
Which special characters do you want to exclude?
DaveTheCoder
I somewhat under stand RegEx at this point in time, i want to under stand how to check i variable for things like if it has words inside that are apart of the RegEx. if i were to use a RegEx i would use "[A-Z0-9a-z]+@[A-Z0-9a-z]+.com" for the email and "[A-Z] for the password and "\w".
But i don't know how make a If statement that checks one of these variables at the same time as checking for no input or other things?
Here's an example:
var username_regex: RegEx
func _ready() -> void:
# Compile RegEx pattern for parsing username input.
username_regex = RegEx.new()
var regex_err: Error = username_regex.compile("\\s")
assert(regex_err == OK)
func some_other_function(username_input: String) -> void:
# Check if username input contains whitespace.
var regex_match: RegExMatch = username_regex.search(username_input)
if regex_match != null:
print_debug("username contains whitespace")
else:
print_debug("username does not contain whitespace")
Ask if you need an explanation for the "\\s".
DaveTheCoder
I more want an explanation on the code ingeneral like what are the (asserts) and why is it (... == OK) and is RegExMatch used to search the var? im kinda lost with that. and what is (Error: )