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

OS UI HELP

Started by cmurtheepic, 23 November 2012 - 10:44 AM
cmurtheepic #1
Posted 23 November 2012 - 11:44 AM
i am trying to make my OS'S setup UI but It wont scroll down. it says attempt to index a nil value
here is the code
:

local w,h = term.getSize()
local select = 1
--this is specifying printing positions
local function printCentered(str, ypos)
  term.setCursorPos(w/2 - #str/2, ypos)
  term.write(str)
end
local function printTopCentered(str, ypos)
  term.setCursorPos(w/2 - #str/2, ypos)
  term.write(str)
end
local function printBottomCentered(str)
  term.setCursorPos(h - #str, w/2 - #str/2)
  term.write(str)
end
local function printRight(str, ypos)
  term.setCursorPos(w - #str, ypos)
  term.write(str)
end
local function printRightcentered(str)
  term.setCursorPos(w - #str)
  term.setCursorPos(h/2 - #str/2)
  term.write(str)
end
local function printRightTop(str, ypos)
  term.setCursorPos(w - #str, ypos)
  term.write(str)
end
local function printRightBottom(str)
  term.setCursorPos(h - #str)
  term.setCursorPos(w - #str)
  term.write(str)
end
local function printLeft(str, ypos)
  term.setCursorPos(w/4 - #str/4, ypos)
  term.write(str)
end
local function printLeftTop(str, ypos)
  term.setCursorPos(w/4 - #str/4, ypos)
  term.write(str)
end
local function printLeftBottom(str)
  term.setCursorPos(h - #str)
  term.setCursorPos(w/4 - #str/4)
  term.write(str)
end
--setup menu options
function drawSetupMenu()
  printCentered("register new", 8)
  printCentered("non-locked", 12)
  printCentered("Quit", h-2)

  local ypos = 9
  if select == 2 then ypos = 13
  elseif select == 3 then ypos = h-1 end
  printCentered("---------", ypos)
end
function drawSetupHeader()
  printTopCentered("Cnet OS Setup", 1)
  printCentered(string.rep("-", w), 2)
  printBottomCentered("coded by: connor murry")
end
--menu state
local menustate = "setup"
local mopt = {
  ["main"] = {
	options = {"register new", "non-locked", "quit"}
}
}

--run setup function
function runSetup()
  while true do
	term.clear()
	drawSetupHeader()
	drawSetupMenu()
  
	local id, key = os.pullEvent("key")
	--UP = 200, DOWN = 208, ENTER = 28
  
	if key == 200 and select > 1 then select = select-1
	elseif key == 208 and select < #mopt[menustate].options then select = select-1
	elseif key == 28 then
	  if #mopt[menustate].options[select] == "quit" then break end
	  menustate = mopt[menustate].options[select]
	end
  end
end
runSetup()
--this was created in part of the NitrogenFingers GUI tutorial
KillaVanilla #2
Posted 23 November 2012 - 11:49 AM
Could we have line numbers?
Grim Reaper #3
Posted 23 November 2012 - 12:07 PM
I don't exactly know where the error you're looking for exists exactly, but one flaw I do see is close to the bottom of your code:

#mopt[menustate].options[select] == "quit"
The '#' sizeof operator doesn't return a string; only a number that is the size of the table being operated upon.

If you lose the '#', it might help your situation:

if mopt[menustate].options[select] == "quit" then
  break
end
What line does the error insist that it is occurring on?
cmurtheepic #4
Posted 23 November 2012 - 12:45 PM
its the 7th line from the very end of the coding: or line 99
this is going on whenever i tap the down key to scroll
Orwell #5
Posted 23 November 2012 - 12:53 PM

local menustate = "setup"
local mopt = {
  ["main"] = {
        options = {"register new", "non-locked", "quit"}
}
}

You have that, and the next time you access mopt is on that line 99:

elseif key == 208 and select < #mopt[menustate].options then select = select-1

So it tries to index mopt[menustate] which is atm mopt["setup"]. But you defined mopt as a table with "main" as the only key. So the "setup" key cannot be found in mopt.
Lyqyd #6
Posted 23 November 2012 - 12:54 PM
You're trying to subtract one from the current selection to go down. It's then setting the selection to zero, which there is no index in the menu table for, hence the error. Change it to add one instead.

Edit: What Orwell said too.
Orwell #7
Posted 23 November 2012 - 01:02 PM
If I may, I'd like to give you a good advice. Look up on 'incremental programming', it could really help you for larger programs like this. -_-/>/>
Edit: Hehe, I can't find any info on it myself (learned about it in some course at university), so hear is a short explanation:
Basically you start out with a tiny base for your program and test it whenever you can so you can debug it. Only then you expand it with more code to debug it again whenever you're able of running it. While this technique would seem rather obvious, most don't think about it this way. And that can lead to a dozen of errors in hundreds of lines of code. ;)/>/>
cmurtheepic #8
Posted 23 November 2012 - 02:08 PM
ok but could someone please post a solution
Lyqyd #9
Posted 23 November 2012 - 02:37 PM
Please post the updated code with Orwell's correction and my correction made, with whatever new error or undesired behavior it's exhibiting. If you haven't done these yet, then don't ask for "a solution", when you've already got two changes to make.
Orwell #10
Posted 23 November 2012 - 02:41 PM
First solution that was posted:
Change it to add one instead.
So change that line 99 from:

elseif key == 208 and select < #mopt[menustate].options then select = select-1
to:

elseif key == 208 and select < #mopt[menustate].options then select = select+1

Second solution posted:


local menustate = "setup"
local mopt = {
  ["main"] = {
		options = {"register new", "non-locked", "quit"}
}
}

You have that, and the next time you access mopt is on that line 99:

elseif key == 208 and select < #mopt[menustate].options then select = select-1

So it tries to index mopt[menustate] which is atm mopt["setup"]. But you defined mopt as a table with "main" as the only key. So the "setup" key cannot be found in mopt.
So make the definition of mopt something like this:

local mopt = {
  ["main"] = {
		options = {"register new", "non-locked", "quit"}
}
["setup"] = {
		options = {"some option", "another", "quit"}
}
}

I see that I'm ninja'd by Lyqyd and I agree with him ;)/>/>.
cmurtheepic #11
Posted 24 November 2012 - 08:51 AM
ok now i am getting an error?

it says:
bios:338: [string "cnet"]:84: '}' expected (to close '{' at line 80)
help?

oh and i added the changes to it here:


local w,h = term.getSize()
local select = 1
--this is specifying printing positions
local function printCentered(str, ypos)
  term.setCursorPos(w/2 - #str/2, ypos)
  term.write(str)
end
local function printTopCentered(str, ypos)
  term.setCursorPos(w/2 - #str/2, ypos)
  term.write(str)
end
local function printBottomCentered(str)
  term.setCursorPos(h - #str, w/2 - #str/2)
  term.write(str)
end
local function printRight(str, ypos)
  term.setCursorPos(w - #str, ypos)
  term.write(str)
end
local function printRightcentered(str)
  term.setCursorPos(w - #str)
  term.setCursorPos(h/2 - #str/2)
  term.write(str)
end
local function printRightTop(str, ypos)
  term.setCursorPos(w - #str, ypos)
  term.write(str)
end
local function printRightBottom(str)
  term.setCursorPos(h - #str)
  term.setCursorPos(w - #str)
  term.write(str)
end
local function printLeft(str, ypos)
  term.setCursorPos(w/4 - #str/4, ypos)
  term.write(str)
end
local function printLeftTop(str, ypos)
  term.setCursorPos(w/4 - #str/4, ypos)
  term.write(str)
end
local function printLeftBottom(str)
  term.setCursorPos(h - #str)
  term.setCursorPos(w/4 - #str/4)
  term.write(str)
end
--setup menu options
function drawSetupMenu()
  printCentered("register new", 8)
  printCentered("non-locked", 12)
  printCentered("Quit", h-2)
  local ypos = 9
  if select == 2 then ypos = 13
  elseif select == 3 then ypos = h-1 end
  printCentered("---------", ypos)
end
function drawSetupHeader()
  printTopCentered("Cnet OS Setup", 1)
  printCentered(string.rep("-", w), 2)
  printBottomCentered("coded by: connor murry")
end
--menu state
local menustate = "setup"
local mopt = {
  ["main"] = {
			    options = {"register new", "non-locked", "quit"}
}
["setup"] = {
			    options = {"some option", "another", "quit"}
}
}
--run setup function
function runSetup()
  while true do
    term.clear()
    drawSetupHeader()
    drawSetupMenu()
    local id, key = os.pullEvent("key")
    --UP = 200, DOWN = 208, ENTER = 28
    if key == 200 and select > 1 then select = select-1
    elseif key == 208 and select < #mopt[menustate].options then select = select+1
    elseif key == 28 then
	  if mopt[menustate].options[select] == "quit" then break end
	  menustate = mopt[menustate].options[select]
    end
  end
end
runSetup()
--this was created in part of the NitrogenFingers GUI tutorial
Lyqyd #12
Posted 24 November 2012 - 09:18 AM
You need a comma.


local mopt = {
  ["main"] = {
						    options = {"register new", "non-locked", "quit"}
},
["setup"] = {
						    options = {"some option", "another", "quit"}
}
}