Seperate input text fields, is it possible?

Ok so I am trying to have quite a few input text fields in one gui file. but when I type into one of them it copies the values I sent into one of them into the other ones and when I remove it does the same to the others as well.

I followed a tutorial on text input but It was meant for just one input field.
How could I make two or more fields or really as many as I need without effecting each other?

this is my current input function

I am aware that I am using the same self.message and that is why it is effecting both of them. but how can I make separate messages for each field?

function on_input(self, action_id, action)
	if(action_id == hash("click")and action.released == true)then
		local loginBtn = gui.get_node("loginBtn")
		local userField = gui.get_node("userBg")
		local passField = gui.get_node("passBg")
		local userTxt = gui.get_text(userField)
		local passTxt = gui.get_text(passField)

		if(gui.pick_node(loginBtn, action.x, action.y))then
			print("i clicked log in")
		end
		if(gui.pick_node(userField, action.x, action.y))then
			userFieldSel = true
			passFieldSel = false
		end
		if(gui.pick_node(passField, action.x, action.y))then
			passFieldSel = true
			userFieldSel = false
		end
		
	end	
	if(userFieldSel == true)then
		if action_id == hash("type") then
			local userField = gui.get_node("userField")
			local userTxt = gui.get_text(userField)
			self.message = self.message .. action.text 
			gui.set_text(userField, self.message) 
		elseif action_id == hash("backspace") and action.repeated then
			local l = string.len(self.message)
			self.message = string.sub(self.message, 0, l-1) 
			label.set_text(userField, self.message) 
		end
	end
	if(passFieldSel == true)then
		if action_id == hash("type") then
			local passField = gui.get_node("passField")
			local passTxt = gui.get_text(passField)
			self.message = self.message .. action.text 
			gui.set_text(passField, self.message) 
		elseif action_id == hash("backspace") and action.repeated then
			local l = string.len(self.message)
			self.message = string.sub(self.message, 0, l-1) 
			gui.set_text(passField, self.message) 
		end
	end
end

The input system has no concept of “text field”, which is why you need to create a system for keeping track of selection and where input should go. You already have code that keeps track of which input field the user selects, but you do it with global variables, which may cause you trouble later on.

You can store the selection state in “self” instead, and also keep the input text separated between the two states:

		if(gui.pick_node(userField, action.x, action.y))then
			self.field_selected = "user"
		end
		if(gui.pick_node(passField, action.x, action.y))then
			self.field_selected = "pass"
		end

...

	if self.field_selected == "user" then
		if action_id == hash("type") then
			local userField = gui.get_node("userField")
			local userTxt = gui.get_text(userField)
			self.user_message = self.user_message .. action.text
			gui.set_text(userField, self.user_message) 
...

thanks soo much. sorry if my questions are kind of nooby just trying to get the hang of the engine is all :wink:

One more question is how would I go about user_message, because as of now it is just nil.

1 Like

Please ask anything. We are all beginners at some point.

Yes, that is because it has not been initialized. You should initialize it yourself at some point. The init() function is good for that:

function init(self)
    -- init text field variables
    self.pass_message = ""
    self.user_message = ""
end

This function is automatically called when the script starts up.

2 Likes

If you want to simplify text input significantly I’d recommend taking a look at Gooey from the Asset Portal.

omg I’m soo dumb … thanks again. man I’m making a good impression x)

1 Like