Spoiler
attempt to index ? (a nil value)
Started by matt2887, Jun 19 2014 05:55 PM
9 replies to this topic
#1
Posted 19 June 2014 - 05:55 PM
So I'm helping a friend with a menu program. It prints the menu options just fine, it prints the >> to show the selected menu item just fine, however when you hit the up down or enter it gives an error 'menu:30: attempt to index ? (a nil value). I am completely stumped.
#2
Posted 19 June 2014 - 06:42 PM
Hey! 
First i had to take all those [*] things.. not hard but could be avoided...
Second you need to redraw the menu
Third functions under the caller will not run, so, if you want to use a function make sure you are calling it after having the function written above.
Fourth The handlers in you table... lead to nothing. I did the functions . Hope you get it.
Basically your hanlers will lead to a function. Each option calls the function.
Here you have the code i edited. Warning: its not organized, i messed up and changed things:
First i had to take all those [*] things.. not hard but could be avoided...
Second you need to redraw the menu
Third functions under the caller will not run, so, if you want to use a function make sure you are calling it after having the function written above.
Fourth The handlers in you table... lead to nothing. I did the functions . Hope you get it.
Basically your hanlers will lead to a function. Each option calls the function.
Here you have the code i edited. Warning: its not organized, i messed up and changed things:
--[[ Local Variables]]--
local termWidth, termHeight = term.getSize()
local selectedItem = 2
local running = true
--[[ Menu Definitions]]--
function choice1()
term.setCursorPos(10,10)
print("YOU HAVE CHOOSEN ME")
sleep(1)
end
function choice2()
term.setCursorPos(10,10)
print("YOU HAVE CHOOSEN THE SECOND")
sleep(1)
end
function exit()
term.setCursorPos(10,10)
print("YOU HAVE CHOOSEN TO EXIT")
sleep(1)
end
mainMenu = {
[1] = { text = "Choice 1", handler = choice1 },
[2] = { text = "Choice 2", handler = choice2 },
[3] = { text = "Exit", handler = exit }
}
--[[ 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
function onItemSelected( menu )
print(menu[selectedItem].handler())
sleep(1)
end
--[[ Handler Methods ]]--
function onKeyPressed( key, menu )
if key == keys.enter then --getting an attempt to index a nil value here
onItemSelected(menu)
elseif key == keys.up then
print("Up")
if selectedItem > 1 then
print("set")
selectedItem = selectedItem - 1
end
elseif key == keys.down then
if selectedItem < #menu then
selectedItem = selectedItem + 1
end
end
term.clear()
term.setCursorPos(1,1)
printMenu( menu )
end
--[[ Main Method ]]--
function main()
term.clear()
term.setCursorPos(1,1)
printMenu(mainMenu)
while true do
event, key = os.pullEvent("key")
onKeyPressed(key,mainMenu)
end
end
main()
#3
Posted 19 June 2014 - 09:10 PM
I copied and pasted your code above into a file in my single player world. Still getting the same error on the same line. the only difference now is the >> that shows the selected menu item starts off in the middle, pointing at choice 2 instead of at the top pointing at choice 1.
#4
Posted 19 June 2014 - 09:20 PM
yeas i did change the initial variable from 1 to 2 to test a thing. You can change it back again.
It works like a charm on mine... O.o
Have you tryed to use the keys and press enter? If nothing happens post your cc version here pls.
It works like a charm on mine... O.o
Have you tryed to use the keys and press enter? If nothing happens post your cc version here pls.
#5
Posted 20 June 2014 - 04:44 AM
It's saying that it doesn't like you attempting to refer to index "enter" inside of "keys", on the basis that "keys" is nil.
However, "keys" should not be nil - it should be a table. It seems that at some point you've overwritten that global "keys" table (bearing in mind that such a mistake would not be undone when the offending script ended, due to the way variable localisation/globalisation works in Lua).
Reboot your computer and test the script again - this should restore "keys" back to its original state, assuming you don't have a startup script messing with it.
However, "keys" should not be nil - it should be a table. It seems that at some point you've overwritten that global "keys" table (bearing in mind that such a mistake would not be undone when the offending script ended, due to the way variable localisation/globalisation works in Lua).
Reboot your computer and test the script again - this should restore "keys" back to its original state, assuming you don't have a startup script messing with it.
#6
Posted 20 June 2014 - 05:04 AM
CC mod version 1.33.
I have tried in fresh SSP world before and after I restarted my comp. no change. I also manually rewrote the code in the SMP world i play in tekkitnoodle. No changes.
I have tried in fresh SSP world before and after I restarted my comp. no change. I also manually rewrote the code in the SMP world i play in tekkitnoodle. No changes.
#8
Posted 20 June 2014 - 05:18 AM
Omg i fixed it 
by replacing keys.enter with the numeric value of 28, the keys.up with 200, and the keys.down with 208 it works perfectly
That's HILARIOUS, that's the page i found
"As of ComputerCraft 1.52, the following constants are defined:" quoted from the keys API page, i figured the version of CC was too old to have that API.
Thanks again everyone!
by replacing keys.enter with the numeric value of 28, the keys.up with 200, and the keys.down with 208 it works perfectly
That's HILARIOUS, that's the page i found
"As of ComputerCraft 1.52, the following constants are defined:" quoted from the keys API page, i figured the version of CC was too old to have that API.
Thanks again everyone!
Edited by matt2887, 20 June 2014 - 05:18 AM.
#9
Posted 20 June 2014 - 05:28 AM
Hrm. That line's probably a bit misleading - makes it sound like the "original" version was introduced then.
Well done in any case.
Well done in any case.
#10
Posted 20 June 2014 - 04:25 PM
I tested this in a version with the keys, and it idnt worked. I needed to changes a couple of things.
3 user(s) are reading this topic
0 members, 3 guests, 0 anonymous users












