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

[LUA] Code Problem

Started by minecommunityhd, 16 March 2012 - 08:43 PM
minecommunityhd #1
Posted 16 March 2012 - 09:43 PM
local tChoices = {} --Array
tChoices[0] = 'Choice1'  -- Choices
tChoices[1] = 'Choice2'
table.insert(tChoices, 'Shutdown') --Shutdown Will always be at the bottom
local nTermX, nTermY = term.getSize()
local sSeperator = ("-"):rep(nTermX) -- Creates a seperator
local tActions = {} -- Array addon for tChoices with the functions

tActions[0] = function()
term.clear()
term.setCursorPos(1,1)
print(sSeperator)
print("You chose Choice1!")
term.setCursorPos(1, nTermY) -- Move Cursor to bottom
write("	 [Press enter to returm to mainmenu]")
read()
end
tActions[1] = function()
term.clear()
term.setCursorPos(1,1)
print(sSeperator)
print("You chose Choice2!")
term.setCursorPos(1, nTermY) -- Move Cursor to bottom
write("	 [Press enter to returm to mainmenu]")
read()
end
table.insert(tActions, os.shutdown)
local nSelection = 0 -- Sets default selection to 0
repeat
term.setCursorPos(1,1)
term.clear()
print(sSeperator)
print("SDfalbsflkajsdflajsdklfjasdlkfjaldsjfas")
print(sSeperator)


for nLine = 0, #tChoices do -- Print all possible actions and mark the chosen one

   local sLine = " "
   if nSelection == nLine then
    sLine = ">"
 
  end
  local sLineNum = tostring(nLine)
  if #sLineNum < 2 then
   sLineNum = "0" .. sLineNum
  
  end
  sLine = sLine .. "[" .. sLineNum .. "]" .. " " .. tChoices[nLine] -- Construct the string were printing
  print(sLine) -- Print it
end
-- os.pullEvent keys up - 200, down - 208, enter - 28
local sEvent, nKey = os.pullEvent("key") -- Using the 1.3 filtering; this will mean only "key" events will pass

if nKey == 200 or nKey == 17 then -- Up/w key: Move up the menu
   if tChoices[nSelection - 1] then -- Check if we can move down
    nSelction = nSelection - 1
  end
elseif nKey == 208 or nKey == 31 then
   if tChoices[nSelection + 1] then
    nSelection = nSelection + 1
  end
elseif nKey == 28 then
   tActions[nSelection]()
else
   print("Error: Selection out of bounds: ", nSelection)
   read()
  end
  until false

This only lets me go down, not up what did I do wrong? Thanks in advance!
Casper7526 #2
Posted 17 March 2012 - 01:20 AM
nSelction = nSelection - 1
minecommunityhd #3
Posted 19 March 2012 - 06:11 PM
nSelction = nSelection - 1

Holy crap thanks hahaha