Use string to reference a variable which contains a GUI node? [SOLVED]

How do I use a built string to reference a variable which contains a GUI node? I’m sorry if this a basic Lua thing, but I haven’t been able to find a solution elsewhere.

I have 5 numbered panels on the GUI of a ‘Save/Load’ screen. Each panel has 3 buttons numbered accordingly and I’m wanting to iterate through them for each panel to check whether each button has been clicked and run the relevant code. The buttons nodes and state are all stored in a table.

The below code (edited for brevity) doesn’t work and the error spat out is pretty obvious:

bad argument #1 to ‘pick_node’ (NodeProxy expected, got string)

if action_id == hash("touch") and action.pressed then

	for panel = 1,5,1 do

		local new_button = "buttons.new_".. panel .. ".node"

		if gui.pick_node(new_button, action.x, action.y) then
			msg.post("main:/scenes#loader", "load_scene", { scene = "new_game" })		
		end
	end

end

As the error says, you’re passing a string. It should be a node proxy. You should also pass the ID of that button. The path you’re constructing with dots might not be correct.

local new_button = gui.get_node("buttons.new_".. panel .. ".node") 
2 Likes

Oh, I feel like such a fool - I completely forgot about gui.get_node()

Such a basic oversight, my brain was in completely the wrong place.

Thank you, selimanac.

1 Like

Oh, I’ve also realised the path I was trying to build in the string was the location in the button table that I had created where the node was stored, rather than the address in the collection file. Doh!