Typing into a gui label when selected

I have looked over the selecting a label and typing tutorials however now i am trying to combine them. Im not sure exactly how to do this but im guessing its something as simple as where the code goes.

This is the code ive currently made:

if action_id == hash("touch") and action.pressed then 
		local button = gui.get_node("new_highScore") 
		local text = gui.get_node("new_highScore_label") 
		if gui.pick_node(button, action.x, action.y) then 
			print("ih")
			if action_id == hash("type") then
				self.message = self.message .. action.text
				gui.set_text(gui.get_node("new_highScore_label"), 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(gui.get_node("new_highScore_label"), self.message)
			end 
		end

You have some issues with your code flow where you have nested your conditional checks of action_id:

In this piece of code you start by checking if action_id is equal to hash("touch") and then later in the same flow of code you check if action_id is equal to hash("type)`. A variable can not change its value like that. What you need to do is to split this into multiple checks and keep track of some state as well:

-- check if the "new_highScore" button has been pressed
-- set a flag once this has happened
-- accept text input if the flag has been set
if action_id == hash("touch") and action.pressed then 
	local button = gui.get_node("new_highScore") 
	if gui.pick_node(button, action.x, action.y) then 
		self.new_highscore_button_pressed = true
	end
-- handle text input if the highscore button has been pressed
elseif self.new_highscore_button_pressed then
	if action_id == hash("type") and action.pressed then
		self.message = self.message .. action.text
		gui.set_text(gui.get_node("new_highScore_label"), 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(gui.get_node("new_highScore_label"), self.message)
	end
end
2 Likes

So sorry i didnt reply, thanks so much for the help it was very… well, helpful :slight_smile:

1 Like