Posted 08 March 2016 - 08:13 PM
Hello. This is my first post and I am fairly new to lua so pardon me if this fix is quite easy.
I was trying to create a menu for computercraft so i could use it in later programs. Like most newcomers, I took to tutorials for help. I basically created a menu with 3 options: option 1, option 2 and an exit. I have a good structure of the code, but when I try to run it, I get an error (title)
Here is my code:
Thank you in advance to whoever helps me. Hopefully i've provided enough information for the solution.
I was trying to create a menu for computercraft so i could use it in later programs. Like most newcomers, I took to tutorials for help. I basically created a menu with 3 options: option 1, option 2 and an exit. I have a good structure of the code, but when I try to run it, I get an error (title)
Here is my code:
--[[Local Variables]]--
local termWidth, termHeight = term.getSize()
local selectedItem = 1
local running true
--[[Menu Methods]]--
function choice1()
term.clear()
term.setCursorPos(1, 1)
term.write("Hello, my name is "..os.getComputerLabel())
sleep(3)
end
function choice2()
term.clear()
term.setCursorPos(1, 1)
term.write("This is just a test.")
sleep(3)
end
function Exit()
running = false
end
--[[Menu Definitions]]--
mainMenu{
[1] = {text = "Choice 1", handler = choice1 },
[2] = {text = "Choice 2", handler = choice2 },
[3] = {text = "Exit", handler = Exit }
}
--[[Printing Methods]]--
function printMenu( menu )
for i=1,#menu do
if i == selectedItem then
print(">> " ..menu[i].text)
else
print(" " ..menu[i].text)
end
end
end
--[[Handler Methods]]--
function onKeyPressed(key, menu)
if key == keys.enter then
onItemSelected( menu )
elseif key == keys.up then
if selectedItem > 1 then
selectedItem = selectedItem - 1
end
elseif key == keys.down then
if selectedItem < #menu then
selectedItem = selectedItem + 1
end
end
end
function onItemSelected( menu )
menu[selectedItem].handler()
end
--[[Main Method]]--
function main()
while running do
term.clear()
term.setCursorPos(1, 1)
printMenu(mainMenu)
event, key = os.pullEvent("key")
onKeyPressed(key, mainMenu)
end
end
Thank you in advance to whoever helps me. Hopefully i've provided enough information for the solution.