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

Menu error unable to identify

Started by aceyo369, 04 December 2013 - 02:07 AM
aceyo369 #1
Posted 04 December 2013 - 03:07 AM
Ok i am creating a menu interface for my program and the first menu looks ok until i enter into the 2nd option ("Lights Control")
Error is attempt to call nil on line 63.


--[[ This is my program ]]--

--[[ Local Variable ]]--

local termWidth, termHeight = term.getSize()
local selectedItem = 1
local inMainMenu = true
local inLightsMenu = false
--[[Menu Methods ]]--

function Choice1()

end

function Choice2()
  inLightsMenu = true
  selectedItem = 1
  while inLightsMenu do
	clear()
	printMenu(lightsMenu)

	event, key = os.pullEvent("key")
	OnKeyPressed(key,lightsMenu)
  end
end

function Exit()
inMainMenu = false  
end

function ON()
  rednet.open("back")
  while true do
	rednet.send(0, on)
  end
end

function OFF()
  while true do
	rednet.send(0, off)
  end
end

function Reboot()
  os.reboot()
end

--[[ Menu Definition ]]--

local mainMenu = {
[1] = {text = "Doors", handler = Choice1},
[2] = {text = "Lights Control", handler = Choice2},
[3] = {text = "Exit", handler = Exit}
}

local lightsMenu = {
[1] = {text = "Lights on", handler = ON},
[2] = {text = "Lights off", handler = OFF},
[3] = {text = "Back to Main Menu", handler = Reboot}
}

--[[ 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

function clear()
  term.clear()
  term.setCursorPos(1,1)
end
--[[Main Method ]]--

function main()
  while inMainMenu do
	clear()  
	printMenu(mainMenu)

	event, key = os.pullEvent("key")
	onKeyPressed(key,mainMenu)
  end  
end  

main()

–[[ Extra Info ]]–

line 63 – for i = 1, #menu do

Why does this not apply to my second option "Lights Control"?
Edited by
aceyo369 #2
Posted 05 December 2013 - 12:49 AM
This is a clearer one than my previous one and i will post my problem before the coding for clearer view..
line 113 –[[ for i=1, #menu.options do ]]–
This line affects my second options when i pressed select on it

[ Main Controls ]
Quit

This another Menu is what i expected after going in Main Controls:

[ Doors ]
Lights

However i got an error attempt to index ? ( a function value )
I have seen many times that my maincontrols.option does exist so now what is the problem?
Again i have highlighted and bold the errors that are found with the table existing but interpreted not found…


This is the coding:

–[[ Local Variables ]]–
local w,h = term.getSize()
local select = 1
local function printCentered(str, ypos)
term.setCursorPos(w/2 - #str/2, ypos)
term.write(str)
end
local inMainMenu = true
local inMainControls = false
local inDoors = false
local inLights = false

–[[ Function Methods ]]–
function maincontrols()
select = 1
inMainMenu = false
inMainControls = true
inDoors = false
inLights = false
while inMainControls do
term.clear()
printMenu( maincontrols )
event, key = os.pullEvent("key")
onKeyPressed( key, maincontrols )
end
end

function quit()
inMainMenu = false
end

function doors()
inMainMenu = false
inMainControls = false
inDoors = true
inLights = false
while inDoors do
term.clear()
printMenu( doors )
event, key = os.pullEvent("key")
onKeyPressed( key, doors )
end
end

function lights()
inMainMenu = false
inMainControls = false
inDoors = false
inLights = true
while inLights do
term.clear()
printMenu( lights )
event, key = os.pullEvent("key")
onKeyPressed( key, lights )
end
end

function open()
while true do

end
end

function close()
while true do

end
end

function on()
while true do

end
end

function off()

end

–[[ Menu Tables ]]–

local mainMenu = {
options = {
[1] = {text = "Main Controls", handler = maincontrols},
[2] = {text = "Quit", handler = quit}
}
}

local maincontrols = {
options = {
[1] = {text = "Doors", handler = doors},
[2] = {text = "Lights", handler = lights}
}
}

local doors = {
options = {
[1] = {text = "Open",handler = open},
[2] = {text = "Close",handler = close}
}
}

local lights = {
options = {
[1] = {text = "On",handler = on},
[2] = {text = "Off",handler = off}
}
}

–[[ Selection Print Method ]]–

function printMenu( menu )
for i=1, #menu.options do
if i == select then
printCentered("["..menu.options.text.."]", i + 8)
else
printCentered(" "..menu.options.text, i+8)
end
end
end

–[[ Handler Method ]]–

function onKeyPressed( key, menu )
if key == keys.enter then
onItemSelected( menu )
elseif key == keys.up then
if select > 1 then
select = select - 1
end
elseif key == keys.down then
if select < #menu.options then
select = select + 1
end
end
end

function onItemSelected( menu )
menu.options[select].handler()
end


–[[ Main Programme ]]–
function program()
while true do
term.clear()
printMenu( mainMenu )
event, key = os.pullEvent("key")
onKeyPressed( key, mainMenu )
end
end

program()
Lyqyd #3
Posted 05 December 2013 - 10:43 AM
Please stick to one topic for all questions about a given piece of code.
Bomb Bloke #4
Posted 05 December 2013 - 05:35 PM
You must define your variables before defining functions that want to use them. Move all your table declarations above your function declarations.

Ideally, your function declarations would also be localised.
aceyo369 #5
Posted 06 December 2013 - 02:26 AM
Ok now that i change the position with both the table and the function and i found out that putting an extra options in the table is redundant so i remove it, changing all ().options to juz ().
But after that i have an error with the key controls method in the line:

onItemSelected( menu )
menu[select].handler()
end

i have named "select" as a variable already.

But i still get an error of attempt to call nil, how is it so?