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

Creating A Menu?

Started by Avous, 23 January 2013 - 07:00 PM
Avous #1
Posted 23 January 2013 - 08:00 PM
I've been trying to make a menu for a quarry program I'm making but it it's telling me a variable on line 61 is nil.

"onKeyPressed("key", quarryMenu )" is line 61

here's the code so far.

local termWidth, termHeight = term.getSize()
local selectedOption = 1

function basicQuarry()
local quarryType = basic
end

function oreQuarry()
local quarryType = ore
end

function goldQuarry()
  local quarrytype = gold
end

function diamondQuarry()
  local quarrytype = diamond
end

quarryMenu = {
  [1] = { text = "Basic Quarry [Your standard Quarry!]", handler = basicQuarry },
  [2] = { text = "Ore Quarry [Gathers all Ore!]", handler = oreQuarry },
  [3] = { text = "Gold Quarry [Gathers valuable Ores!]", handler = goldQuarry },
  [4] = { text = "Diamond Quarry [Focuses on Diamonds/Redstone!]", handler = diamondQuarry }
}

function printMenu( menu )
  for i=1, #menu do
	if i == selectedOption then
print(">"..menu[i].text)
else
print(" "..menu[i].text)
end
  end
end

function onkeyPressed( key, menu )
  if key == keys.enter then
	onOptionSelected(menu)
  elseif key == keys.up then
	if selectedOption > 1 then
	  selectedOption = selectedOption - 1
end
  elseif key == keys.down then
	if selectedOption < #menu then
	  selectedOption = selectedOption + 1
	end  
  end
end

function onOptionSelected( menu )
  menu[selectedOption].handler()
end
while true do
   term.clear()
   term.setCursorPos(1,1)
   printMenu(quarryMenu)
   key = os.pullEvent("key")
   onKeyPressed("key", quarryMenu )
end 
Zoinky #2
Posted 23 January 2013 - 08:14 PM
Lua is case sensitive. You created a function called 'onkeyPressed' but you're calling 'onKeyPressed' which doesn't exist. Make the K in the while loop lowercase and it should fix your problem.
remiX #3
Posted 23 January 2013 - 09:57 PM
And your
key = os.pullEvent("key") 
needs to be
event, key =os.pullEvent("key")
because os.pullEvent returns the actual event and then the parameters, even if you restrict it to one eventAlso, change"key" to key because key is a variable.