Creating input boxes

I am trying to create a simple login screen that consists of 2 input boxes, one for a username and another for a password but I am unable to get it to fully work. Here is my project, if someone could take a look that would be great.
Creating a login interface.zip (39.1 KB)

It’s good that you share your projects, that’s really helpful. However, I think it would be even better if you describe:

a) What you want (you have kind of done this)
b) What you have tried (you haven’t done this)
c) What goes wrong (you haven’t done this)

As it stands, there is extra work for any helpers to try and guess your approach and any issues.

Here is an example where I ask a question. I try to clearly describe my issue, I share the code I have and explain what it does / what I think it should do, I show what I want to happen, I show what happens instead, and finally I summarise with clear questions. My issue is probably more complicated than yours, but nevertheless I think it’s a good example of how to best ask questions.

1 Like

In another similar thread I shared the example of input boxes: Stopping text input (solved) - #4 by Insality

2 Likes

No error message comes up. What should happen is when I click on either of the nodes that I have created they should be taking in input until the enter key is pressed. But when I click the nodes, they don’t take in any input. I am unsure as to why. I wish to create a GUI that has 2 input boxes, 1 for a username and another for a password. The boxes would only take input if they are clicked on and then they stop taking input if the enter key is pressed.

Here’s your input function:

function on_input(self, action_id, action)
	if action_id == hash("click") and action.released then
		if gui.pick_node(gui.get_node("username"), action.x, action.y) then
			msg.post(".", "release_input_focus")
			msg.post("main:/go#main_username", "acquire_input_focus")
			if action_id == hash("type") then
				self.username = self.username .. action.text
				gui.set_text(gui.get_node("username"), self.username)
			elseif action_id == hash("backspace") and action.repeated then
				local length = string.len(self.username)
				self.username = string.sub(self.username, 0, length-1)
				gui.set_text(gui.get_node("username"), self.username)
			end
		elseif gui.pick_node(gui.get_node("password"), action.x, action.y) then
			msg.post(".", "release_input_focus")
			msg.post("main:/go#main_password", "acquire_input_focus")
			if action_id == hash("type") then
				self.password = self.password .. action.text
				gui.set_text(gui.get_node("password"), self.username)
			elseif action_id == hash("backspace") and action.repeated then
				local length1 = string.len(self.password)
				self.password = string.sub(self.password, 0, length1-1)
				gui.set_text(gui.get_node("password"), self.password)
			end
		end	
	end
end

You are checking for two different values of action_id in a single call:

function on_input(self, action_id, action)
	if action_id == hash("click") and action.released then
		if gui.pick_node(gui.get_node("username"), action.x, action.y) then
			msg.post(".", "release_input_focus")
			msg.post("main:/go#main_username", "acquire_input_focus")
			if action_id == hash("type") then

You are both checking if action_id is “click” and if it is “type”. You need to separate those if-checks.

4 Likes

How can I do this?

Is it possible to retake in input, as in reset action_id so that it changes input?

I’m not really sure what you are asking? Are you still struggling with creating an input field?

Yes I am struggling, I was wondering if once an input has been received by the on_input function, can it then receive another input?

The on_input function may be called multiple times in a frame, once per input action.

But I guess what you’re asking is something else? You need to track some kind of state in your script to keep track of which input field is selected and if one is selected you add text input to that field. Something like this should work (untested!):


function on_input(self, action_id, action)
	if action_id == hash("click") and action.released then
		if gui.pick_node(gui.get_node("username"), action.x, action.y) then
			self.selected_node = gui.get_node("username")
		elseif gui.pick_node(gui.get_node("password"), action.x, action.y) then
			self.selected_node = gui.get_node("password")
		else
			self.selected_node = nil
		end
	elseif action_id == hash("type") then
		if self.selected_node then
			local text = gui.get_text(self.selected_node)
			text = text .. action.text
			gui.set_text(self.selected_node, text)
		end
	elseif action_id == hash("backspace") and action.repeated then
		if self.selected_node then
			local text = gui.get_text(self.selected_node)
			text = string.sub(text, 0, #text - 1)
			gui.set_text(self.selected_node, text)
		end
	end
end
1 Like

I have tried this but it doesn’t work.

@DyleniumFalcon when you ask for help and someone provides a suggested solution (although untested) you really need to provide more info than “it doesn’t work”. What happens? Do you get an error?

4 Likes

No error occurs but when I click on a node, it doesn’t then take in any input.