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

Problem With Menu Creation

Started by Nuczija, 09 April 2014 - 06:02 PM
Nuczija #1
Posted 09 April 2014 - 08:02 PM
Hey, I was trying to create a menu in ComputerCraft, something small and easy for me and my friends. Since I'm the only one who knows how to (even if not a lot) use ComputerCraft, I decided to make a menu to accommodate everyone.

This is the line of code (145 lines);

Spoiler
-- [[ Local Variables ]]--

local termWidth, termHeight = term.getSize()
local selectedItem = 1
local inMainMenu = true
local inReadme = false



--[[ Menu Methods ]]--

function Choice1()
term.clear()
term.setCursorPos(1,1)
term.write("Hello my name is"..os.getComputerLabel())
sleep(2)
end

function readme()
inReadme = true
selectedItem = 1

while inReadme do
  term.clear()
  term.setCursorPos(1,1)
  printMenu(readmeMenu)

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

function General()
term.clear()
term.setCursorPos(1,1)
term.write("Stuff")
sleep(2)
selectedItem = 1
end

function Credits()
term.clear()
term.setCursorPos(1,1)
term.write("Stuff")
sleep(2)
selectedItem = 1
end

function Changelog()
term.clear()
term.setCursorPos(1,1)
term.write("Stuff")
sleep(2)
selectedItem = 1
end

function Return()
term.clear()
term.setCursorPos(1,1)
inReadme = false
selectedItem = 1
end

function Exit()
inMainMenu = false
end



--[[ Menu Definitions ]]--

mainMenu = {
[1] = { text = "Choice 1", handler = Choice1 },
[2] = { text = "Readme", handler = Readme },
[3] = { text = "Exit", handler = Exit }
}

readmeMenu = {
[1] = { text = "General Information", handler = General },
[2] = { text = "Credits & Acknowledgements", handler = Credits },
[3] = { text = "Changelog", handler = Changelog },
[4] = { text = "Return", handler = Return }
}



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

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 inMainMenu do
  term.clear()
  term.setCursorPos(1,1)
  printMenu(mainMenu)

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

main()


The code works fine until line 127 when "nokhios:127: attempt to call nil" happens. However, this only happens when I choose the "readme" option in the menu. It works fine in the "main menu", and removing said code still has the same error on a blank line… And if anyone asks, yes I basically followed along a tutorial so I can learn by doing. Said tutorial can be found here

Here's the specific snippet;

Spoiler
function onItemSelected( menu )
 menu[selectedItem].handler()
end
CometWolf #2
Posted 09 April 2014 - 09:01 PM

function readme()
inReadme = true
selectedItem = 1
while inReadme do
  term.clear()
  term.setCursorPos(1,1)
  printMenu(readmeMenu)
  event, key = os.pullEvent("key")
  onKeyPressed(key, readmeMenu)
end
end

[2] = { text = "Readme", handler = Readme },
In Lua capitalization matter, therefore readme and Readme are not the same.
Edited on 09 April 2014 - 07:01 PM
Nuczija #3
Posted 09 April 2014 - 09:19 PM

function readme()
inReadme = true
selectedItem = 1
while inReadme do
  term.clear()
  term.setCursorPos(1,1)
  printMenu(readmeMenu)
  event, key = os.pullEvent("key")
  onKeyPressed(key, readmeMenu)
end
end

[2] = { text = "Readme", handler = Readme },
In Lua capitalization matter, therefore readme and Readme are not the same.

Every time! It's always my capitalization…

This time same error, but on line 114.

function onKeyPressed( key, menu)
if key == keys.enter then
  onItemSelected(menu) << Specifically this bit, this is 114
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

This, again, only appears when I select "Readme". It insinuates that "onItemSelected" is invalid, but it works fine in the main menu bit. Would be it be "(menu)"? I thought it'd encompass all menus, not just the Main?
Edited on 09 April 2014 - 07:20 PM
CometWolf #4
Posted 09 April 2014 - 09:42 PM
Passing the menu table to the function should work just fine. Does the error occur when you hit enter on Readme in the main menu, or when you select something in the readMe menu?
Nuczija #5
Posted 09 April 2014 - 10:08 PM
Passing the menu table to the function should work just fine. Does the error occur when you hit enter on Readme in the main menu, or when you select something in the readMe menu?

I didn't specify, sorry.

It occurs when I attempt to enter the "readme" from the main menu.
CometWolf #6
Posted 09 April 2014 - 10:20 PM
I can't find anything wrong to be honest, could you post the entire revised code just to be safe?
Nuczija #7
Posted 10 April 2014 - 02:25 AM
I can't find anything wrong to be honest, could you post the entire revised code just to be safe?

Spoiler
-- [[ Local Variables ]]--

local termWidth, termHeight = term.getSize()
local selectedItem = 1
local inMainMenu = true
local inReadme = false



--[[ Menu Methods ]]--

function Choice1()
term.clear()
term.setCursorPos(1,1)
term.write("Hello my name is"..os.getComputerLabel())
sleep(2)
end

function Readme()
inReadme = true
selectedItem = 1

while inReadme do
  term.clear()
  term.setCursorPos(1,1)
  printMenu(ReadmeMenu)

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

function General()
term.clear()
term.setCursorPos(1,1)
term.write("Stuff")
sleep(2)
selectedItem = 1
end

function Credits()
term.clear()
term.setCursorPos(1,1)
term.write("Stuff")
sleep(2)
selectedItem = 1
end

function Changelog()
term.clear()
term.setCursorPos(1,1)
term.write("Stuff")
sleep(2)
selectedItem = 1
end

function Return()
term.clear()
term.setCursorPos(1,1)
inReadme = false
selectedItem = 1
end

function Exit()
inMainMenu = false
end



--[[ Menu Definitions ]]--

mainMenu = {
[1] = { text = "Choice 1", handler = Choice1 },
[2] = { text = "Readme", handler = Readme },
[3] = { text = "Exit", handler = Exit }
}

ReadmeMenu = {
[1] = { text = "General Information", handler = General },
[2] = { text = "Credits &amp; Acknowledgements", handler = Credits },
[3] = { text = "Changelog", handler = Changelog },
[4] = { text = "Return", handler = Return }
}



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

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



--[[ Main Method ]]--

function main()
while inMainMenu do
  term.clear()
  term.setCursorPos(1,1)
  printMenu(mainMenu)

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

main()

Sorry for taking four hours to reply, I went off to play on a LAN server with some friends.

But yeah, I don't see the problem either. It's still calling a nil error on line 114, but line 114 is blank.
Edited on 10 April 2014 - 12:26 AM
Lyqyd #8
Posted 10 April 2014 - 02:35 AM
Where is onItemSelected defined?
Nuczija #9
Posted 10 April 2014 - 02:45 AM
Where is onItemSelected defined?

You mean this bit?

Spoilerfunction onItemSelected( menu )
menu[selectedItem].handler()
end

It's at line 114 under "handler methods", or was. So it's either at the blank line of code where it was, or at an "end" code of the "function onKeyPressed" that was above it.
Edited on 10 April 2014 - 12:46 AM
Lyqyd #10
Posted 10 April 2014 - 05:11 AM
That bit doesn't appear anywhere in the code, so attempting to call it will result in an attempt to call nil error.
Nuczija #11
Posted 10 April 2014 - 05:58 AM
That bit doesn't appear anywhere in the code, so attempting to call it will result in an attempt to call nil error.

Yes, the code has been removed after "CometWolf's" posts. But the error still appears at the same line, 114. Said line is either blank or the 'end' of the function above it.
Edited on 10 April 2014 - 03:59 AM
CometWolf #12
Posted 10 April 2014 - 07:23 AM
If it's the same error "attempt to index nil" then i'd suggest you try rebooting the cc computer. Your use of global variables might be causing some issues.
Nuczija #13
Posted 10 April 2014 - 08:32 AM
Apparently reverting to the original code+readme typo fix, has fixed all my problems!

What is this, I don't even… Lua sorceror has struck the newb peasant, and I am confused.

Maybe the changes from this thread fixed it and I mucked up the spelling somewhere, and by re-entering it I typed it in correctly? Or maybe CometWolf's suggest to reboot fixed it.

I always do "exit" or "shutdown" when I leave my computers, does "reboot" do something else entirely (besides being a restart)?
Edited on 10 April 2014 - 06:34 AM
CometWolf #14
Posted 10 April 2014 - 09:38 AM
It shouldn't, they all do pretty much the same. Although, why did you remove onItemSelect in the first place?
Nuczija #15
Posted 10 April 2014 - 07:07 PM
It shouldn't, they all do pretty much the same. Although, why did you remove onItemSelect in the first place?

I was dumb and thought it was the problem. So I removed it without even glancing at it, I thought the nil was due to an impossible function causing it to break.

So I guess retyping it I wrote it correctly, because it works now.

Thanks for the help, CometWolf and Lyqyd.
Edited on 10 April 2014 - 05:17 PM