This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Laurent's profile picture

Menu's and sub menus

Started by Laurent, 11 October 2012 - 11:51 PM
Laurent #1
Posted 12 October 2012 - 01:51 AM
Hey everyone, iv got a quick question which is probably simple but I'm having difficulties. how do I return from one section of code to a previous section or later… iv tried stating the function and returning to that but I can seem to make that work. please point out how I do this in LUA as Im more of a Java guy myself

here is an example:



local option = {}
option[1] = "1"
option[2] = "2"
option[3] = "3"

local actions = {}
actions[1] = function()
shell.run("1")
end
actions[2] = function()

[indent=2]–[]—[/indent]

[indent=2]term.clear()[/indent]
[indent=2]term.setCursorPos(1,1)[/indent]
[indent=2]local option = {}[/indent]
[indent=2]option[1] = "1"[/indent]
[indent=2]option[2] = "2"[/indent]
[indent=2]option[3] = "Exit"[/indent]
[indent=2]local actions = {}[/indent]
[indent=2]actions[1] = function()[/indent]

[indent=2]–[[Melon Menu]]–[/indent]

[indent=2]term.clear()[/indent]
[indent=2]term.setCursorPos(1,1)[/indent]
[indent=2]local option = {}[/indent]
[indent=2]option[1] = "1"[/indent]
[indent=2]option[2] = "2"[/indent]
[indent=2]option[3] = "3"[/indent]

[indent=2]local actions = {}[/indent]
[indent=2]actions[1] = function()[/indent]
[indent=2]shell.run(1)[/indent]
[indent=2]end[/indent]
[indent=2]actions[2] = function()[/indent]
[indent=2]shell.run(2)[/indent]
[indent=2]end[/indent]
[indent=2]actions[3] = function()[/indent]
[indent=2]THIS IS WHERE I WANT TO PLACE MY RETURN TO RETURN TO THE MAIN MENU FROM THIS SUBMENU[/indent]
[indent=2]end[/indent]


[indent=2]local currentY = 1[/indent]
[indent=2]local select = 1[/indent]
[indent=2]function drawCursor()[/indent]
[indent=2]term.setCursorPos(1, currentY)[/indent]
[indent=2]write " >"[/indent]
[indent=2]end[/indent]
[indent=2]function printMenu()[/indent]
[indent=2]while true do[/indent]
[indent=2]shell.run("clear")[/indent]
[indent=2]local num = table.maxn(option)[/indent]
[indent=2]a = 1[/indent]
[indent=2]for x=1, num do[/indent]
[indent=2]print (" "..option[a])[/indent]
[indent=2]a = a + 1[/indent]
[indent=2]end[/indent]
[indent=2]drawCursor()[/indent]
[indent=2]event, key = os.pullEvent()[/indent]
[indent=2]if event == "key" then[/indent]
[indent=2]if key == 200 and currentY > 1 then[/indent]
[indent=2]currentY = currentY - 1[/indent]
[indent=2]select = select -1[/indent]
[indent=2]elseif key == 208 and currentY < num then[/indent]
[indent=2]currentY = currentY + 1[/indent]
[indent=2]select = select + 1[/indent]
[indent=2]elseif key == 28 then[/indent]
[indent=2]return actions[select]()[/indent]
[indent=2]end[/indent]
[indent=2]end[/indent]
[indent=2]end[/indent]
[indent=2]end[/indent]
[indent=2]printMenu()[/indent]

end
actions[2] = function()
shell.run("3)
end
actions[3] = function()
shell.run(3)
end

local currentY = 1
local select = 1
function drawCursor()
term.setCursorPos(1, currentY)
write " >"
end
function printMenu()
while true do
shell.run("clear")
local num = table.maxn(option)
a = 1
for x=1, num do
print (" "..option[a])
a = a + 1
end
drawCursor()
event, key = os.pullEvent()
if event == "key" then
if key == 200 and currentY > 1 then
currentY = currentY - 1
select = select -1
elseif key == 208 and currentY < num then
currentY = currentY + 1
select = select + 1
elseif key == 28 then
return actions[select]()
end
end
end
end
printMenu()
end
actions[3] = function()
shell.run(3)
end

local currentY = 1
local select = 1
function drawCursor()
term.setCursorPos(1, currentY)
write " >"
end
function printMenu()
while true do
shell.run("clear")
local num = table.maxn(option)
a = 1
for x=1, num do
print (" "..option[a])
a = a + 1
end
drawCursor()
event, key = os.pullEvent()
if event == "key" then
if key == 200 and currentY > 1 then
currentY = currentY - 1
select = select -1
elseif key == 208 and currentY < num then
currentY = currentY + 1
select = select + 1
elseif key == 28 then
return actions[select]()
end
end
end
end
end
printMenu()
Orwell #2
Posted 12 October 2012 - 10:43 AM
Could you put your code between
 tags? I think the best way of doing this is by running a while loop around every menu. Make a loop for each menu, then make it draw at the beginning, then wait for input (you need to break the loop here when you want to close the menu), and then put code for cleaning up (end loop here). Now if you open another menu from within that loop and do that menu in the same way, when you break the inner loop, it will fall back to the outer loop and redraw that menu.
Laurent #3
Posted 12 October 2012 - 11:56 AM
Please give example as I'm still learning the structure.
Doyle3694 #4
Posted 12 October 2012 - 12:05 PM
 tags!
Laurent #5
Posted 12 October 2012 - 12:10 PM
Put it in the example please I really just struggling to see its placement
Orwell #6
Posted 12 October 2012 - 12:27 PM
I didn't do the cursor stuff, I just ask for the index of the item through read(). I did this because you got that allready and it makes thing more complicated than they are. : )


local function subMenu()
  local options = {'suboption1','suboption2', 'close'}
  while true do
	for i,item in ipairs(options) do
	  term.setCursorPos(3,i)
	  term.clearLine()
	  term.write(i .. ' : ' .. item)
	end
	local choice = tonumber(read())
	if (choice > 0 and choice <= #options) then
	  local choosedItem = options[choice]
	  if (choosedItem == 'suboption1') then
		-- do stuff
	  elseif (choosedItem == 'suboption2') then
		-- do other stuff
	  elseif (choosedItem == 'close') then
		return 'result from this menu' -- make it return to the mainMenu() function
	  end
	end
  end
end

local function mainMenu()
  local options = {'option1','option2', 'submenu'}
  while true do
	for i,item in ipairs(options) do
	  term.setCursorPos(3,i)
	  term.clearLine()
	  term.write(i .. ' : ' .. item)
	end
	local choice = tonumber(read())
	if (choice > 0 and choice <= #options) then
	  local choosedItem = options[choice]
	  if (choosedItem == 'option1') then
		-- do stuff
	  elseif (choosedItem == 'option2') then
		-- do other stuff
	  elseif (choosedItem == 'submenu') then
		local res = subMenu()
		-- you could do stuff with the result 'res'
	  end
	end
  end
end

mainMenu()
Laurent #7
Posted 12 October 2012 - 12:29 PM
I see thankyou!
Laurent #8
Posted 12 October 2012 - 12:41 PM
Can I ask what u mean by result from this menu?
Laurent #9
Posted 12 October 2012 - 01:04 PM
Also do I use this I replace my current actions or do I append?
Orwell #10
Posted 12 October 2012 - 01:37 PM
I mean that you can return a value. In that way, you could use the value in the menu at a higher level. For example with yes/no. You call it from the main menu and you get a return value of yes or no. Then you can use it there.

You can use the actions table instead of the options table, they are the same. I'm mainly just demonstrating the use of while loops for menus.