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

Attempt to Index ?

Started by darkroom, 02 January 2013 - 04:26 AM
darkroom #1
Posted 02 January 2013 - 05:26 AM
Hello i am working on a simple menu program but i ran into a problem that makes no sense. On line 53 it says Attempt to Index ? (a number value) but as you can see line 53 is a simple if statement. Anyway thanks for the help and time link below :)/>
http://pastebin.com/8nxyDSUG
Orwell #2
Posted 02 January 2013 - 05:39 AM
You can still make errors in if statements. :P/> there is no key api, only a keys api. So change every occurrence of 'key' to 'keys'. E.g. line 53:

if key == key.enter then
Change that to:

if key == keys.enter then
(Keep your key variable as is, i.e. the result from os.pullEvent). Also, do you understand why you couldn't ever use 'key' as a variable and as an api at the same time? (like when you do 'key == key.enter')
darkroom #3
Posted 02 January 2013 - 05:41 AM
Oh gosh i use my autofilling text editor too much :P/> such a dumb mistake :P/>
Edit: Now i have a new error in line 45 it says attempt to index ? (a nil value)
remiX #4
Posted 02 January 2013 - 06:06 AM
Yes, I tested your code because I could see quite a few problems, but I do like how you're using your tables.

That error is because of this line:
centerPrint("[ "..menu[selectedItem].text.." ]",i+5)
--and
centerPrint("  "..menu[selectedItem].text.."  ",i+5)
It should be:
centerPrint("[ "..menu[i].text.." ]",i+5) -- Should print menu[i].text because you're looping through a table
--and
centerPrint("  "..menu[i].text.."  ",i+5)

Another one would be the "selectedMenu" variable. You made it a string:
local selectedMenu = "MainMenu"
This will not work, you need to make it the table, which is defined only later so you will have to set this variable after defining the table.
local selectedMenu = MainMenu

Another one, last one (i think) is using the handler, when clicking enter. Just before it calls the function, you change the selectedItem to 1, but why?

Also, for the handlers within the MainMenu table, instead of calling the function which only has one, do this to reduce lines significantly:
local MainMenu = {
    [1] = {text = "Programs", handler = function() selectedMenu = "Programs" end},
    [2] = {text = "Control Options", handler = function() selectedMenu = "Control" end},
    [3] = {text = "Redstone Options", handler = function() selectedMenu = "Redstone" end},
    [4] = {text = "Exit", handler = function() running = false end}
}

Fixed code?
darkroom #5
Posted 02 January 2013 - 06:11 AM
Ahhh thanks a lot and i like your way of making the tables a lot more. On the topic of why set selected item to 1 because when you hit enter you usually go into a new menu so i want to selection in the new menu to be on the first menu item
remiX #6
Posted 02 January 2013 - 06:15 AM
Oh yeah, then put selectedItem = 1 after menu[selectedItem].handler().

Also, with the MainMenu, change it to this:

local MainMenu = {
    [1] = {text = "Programs", handler = function() selectedMenu = Programs end},
    [2] = {text = "Control Options", handler = function() selectedMenu = Control end},
    [3] = {text = "Redstone Options", handler = function() selectedMenu = Redstone end},
    [4] = {text = "Exit", handler = function() running = false end}
}

I was telling you about not making selectedMenu not a string and I did right there because I copied it from your functions :P/>
darkroom #7
Posted 02 January 2013 - 06:31 AM
Yes i saw that :)/> but now i have a very strange problem when you go into a submenu it should call the handler back but it stays on the submenu here is the new code it semi works now :)/>
I think the problem comes from the way i am initializing my tables any ideas?
http://pastebin.com/czt2M23U
Edit: Forget everything i just posted…. i forgot to put selectedItem = 1 after calling the handler….. so dumb BUT IT WORKS NOW YAAAA!!!!