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()
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()