Posted 15 August 2014 - 06:21 PM
I wanted to store a function in an array called menu here, and the function is called handler, so i don't know why when i call <menu[select].handler()> called out Nil… Maybe i used it wrongly, i need enlightenment for this error.
This is the code:
local w,h = term.getSize()
local select = 1
local function clear()
term.clear()
term.setCursorPos(1,1)
end
local function printCentered(word, h)
term.setCursorPos(w/2 - #word/2, h)
term.write(word)
end
local function runApp()
end
local function editApp()
end
local function seeApp()
end
local menu = {
[1] = {text = "Run a program", handler = runApp()},
[2] = {text = "Make or edit a program", handler = editApp()},
[3] = {text = "See existing program", handler = seeApp()}
}
local function printMenu()
clear()
for i = 1, #menu do
if i == select then
printCentered("["..menu.text.."]",h/2 + i)
else
printCentered(menu.text, h/2 + i)
end
end
end
local function keyHandler()
event, key = os.pullEvent("key")
if key == 200 and select > 1 then
select = select - 1
elseif key == 208 and select < #menu then
select = select + 1
elseif key == 28 then
menu[select].handler()
end
end
while true do
printMenu()
keyHandler()
end
This is the code:
local w,h = term.getSize()
local select = 1
local function clear()
term.clear()
term.setCursorPos(1,1)
end
local function printCentered(word, h)
term.setCursorPos(w/2 - #word/2, h)
term.write(word)
end
local function runApp()
end
local function editApp()
end
local function seeApp()
end
local menu = {
[1] = {text = "Run a program", handler = runApp()},
[2] = {text = "Make or edit a program", handler = editApp()},
[3] = {text = "See existing program", handler = seeApp()}
}
local function printMenu()
clear()
for i = 1, #menu do
if i == select then
printCentered("["..menu.text.."]",h/2 + i)
else
printCentered(menu.text, h/2 + i)
end
end
end
local function keyHandler()
event, key = os.pullEvent("key")
if key == 200 and select > 1 then
select = select - 1
elseif key == 208 and select < #menu then
select = select + 1
elseif key == 28 then
menu[select].handler()
end
end
while true do
printMenu()
keyHandler()
end