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

[Lua][ERROR] attempt to index ? (a nil value) in my Menu program

Started by MatazaNz, 03 October 2012 - 11:11 PM
MatazaNz #1
Posted 04 October 2012 - 01:11 AM
EDIT: I have partially abandoned this, as I have found a new menu script that works, but it doesn't look as nice, so I would still like help on this one. Thank you.

This is the code for a menu that I have drawn up. It is the startup file for my main control terminal, and I have had no problems, until now. To begin with, I only had the lockdown program linked to it, but when I added the opengate program, it broke. It still works when I select the lockdown function, but when I use the opengate option, it has this error: startup:50: attempt to index ? (a nil value)

This is my startup program

local w,h = term.getSize()
local select = 1
--[[Menu Functions]]--
local function printCentered(str, ypos)
  term.setCursorPos(w/2 - #str/2, ypos)
  term.write(str)
end
local function printRight(str, ypos)
  term.setCursorPos(w - #str, ypos)
  term.write(str)
end

function drawMain()
  printCentered("Open the gate", 8)
  printCentered("Lockdown", 12)
  printCentered("Quit", h-2)

  local ypos = 9

  if select == 2 then ypos = 13
  elseif select == 3 then ypos = h-1 end
  printCentered("-x-x-x-x-x-x-x-x-", ypos)
end
function drawHeader()
  printCentered("HEAD CONTROL", 1)
  printCentered(string.rep("-x-", w), 2)
end
--[[Menu functions end]]--
local menustate = "main"
local mopt = {
  ["main"] = {
	options = {"open", "lock", "quit"},
	draw = drawMain
  }
}
--Run function

function runMenu()
  while true do
	term.clear()
	drawHeader()
	mopt[menustate].draw() --This is line 50. This shouldnt be giving me an error isolated to just the opengate program--
  
	local id, key = os.pullEvent("key")
	--UP = 200, DOWN = 208, ENTER = 28
  
	if key == 200 and select > 1 then select = select-1
	elseif key == 208 and select < #mopt[menustate].options then select = select+1
	elseif key == 28 then
	  if mopt[menustate].options[select] == "open" then
		shell.run("opengate")
	  elseif mopt[menustate].options[select] == "lock" then
		shell.run("lockdown")
	  elseif mopt[menustate].options[select] == "quit" then
		os.shutdown()
	  end
	  menustate = mopt[menustate].options[select]
	end
  end
end
--run function end
rednet.open("left")
runMenu()

I cannot for the life of me figure out why it should be giving me an error, as it is working for one program, but not the other. I know it's not the opengate program itself, because I debugged and ran it by itself, and it worked. Any help will be greatly appreciated. Thank you in advance.
Maome #2
Posted 04 October 2012 - 03:11 AM
Just a guess but on line

menustate = mopt[menustate].options[select]
menustate becomes "open" or "lock" so your error line

mopt[menustate].draw()
would probably try to call

mopt["open"].draw() or mopt["lock"].draw()
which is nil.
MatazaNz #3
Posted 04 October 2012 - 03:29 AM
Just a guess but on line

menustate = mopt[menustate].options[select]
menustate becomes "open" or "lock" so your error line

mopt[menustate].draw()
would probably try to call

mopt["open"].draw() or mopt["lock"].draw()
which is nil.
Just a guess but on line

menustate = mopt[menustate].options[select]
menustate becomes "open" or "lock" so your error line

mopt[menustate].draw()
would probably try to call

mopt["open"].draw() or mopt["lock"].draw()
which is nil.

Hmm, that makes sense. I might change that line actually. I only used

mopt[menustate].draw()
because I simply transfered it form another program, and that had multiple menus, requiring a general reference rather than specifics. May be I could just change it to my

drawMain()
function. Thank you for poitnting that out, really helped.
MatazaNz #4
Posted 04 October 2012 - 03:41 AM
Just a guess but on line

menustate = mopt[menustate].options[select]
menustate becomes "open" or "lock" so your error line

mopt[menustate].draw()
would probably try to call

mopt["open"].draw() or mopt["lock"].draw()
which is nil.
Just a guess but on line

menustate = mopt[menustate].options[select]
menustate becomes "open" or "lock" so your error line

mopt[menustate].draw()
would probably try to call

mopt["open"].draw() or mopt["lock"].draw()
which is nil.

Hmm, that makes sense. I might change that line actually. I only used

mopt[menustate].draw()
because I simply transfered it form another program, and that had multiple menus, requiring a general reference rather than specifics. May be I could just change it to my

drawMain()
function. Thank you for poitnting that out, really helped.

Uh, nevermind, it just brings up more errors. I might just forget about the menu, it's not really neccessary. All my other menus work fine, it's just this one.