function opt(m)
local n=1
local l=#m
while true do
for i=1, l, 1 do
if i==n then
local x, y = term.getSize()
local b = string.len(">"..m[i].."<")/2
local x = (x/2)-b
term.setCursorPos(x,i+7)
term.clearLine()
print(">"..m[i].."<")
else
local x, y = term.getSize()
local b = string.len(m[i])/2
local x = (x/2)-b
term.setCursorPos(x,i+7)
term.clearLine()
print(m[i]) end
end
local a, b= os.pullEventRaw()
if a == "key" then
if b==200 and n>1 then n=n-1 end
if b==208 and n<=l then n=n+1 end
if b==28 then break end
end
end
term.clear() term.setCursorPos(1,1)
return n
end
I have tried to have it reference any table I can think of, but it doesn't work. Like I said, it used to work but does not anymore. Any ideas?
This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
[Error] attempt to get length of nil
Started by Cranium, 16 August 2012 - 12:08 AMPosted 16 August 2012 - 02:08 AM
I have been using this function for a while, in several different programs, but now is the only time it seems to give me an error. It is supposed to print a menu, center screen, for however long the reference table is.
Posted 16 August 2012 - 07:20 AM
try so:
function opt(tabl)
tabl=m
Posted 16 August 2012 - 07:55 AM
use
m=m or {}
Posted 16 August 2012 - 01:41 PM
So i found out I was calling back to the function incorrectly. It was in a nested part of my program, and couldn't see it properly. By the way, modified the code slightly to keep it from running off of the page:
function opt(m)
local n=1
local l=#m
while true do
for i=1, l, 1 do
if i==n then
local x, y = term.getSize()
local b = string.len(">"..m[i].."<")/2
local x = (x/2)-b
term.setCursorPos(x,i+7)
term.clearLine()
print(">"..m[i].."<")
else
local x, y = term.getSize()
local b = string.len(m[i])/2
local x = (x/2)-b
term.setCursorPos(x,i+7)
term.clearLine()
print(m[i]) end
end
local a, b= os.pullEventRaw()
if a == "key" then
if b==200 and n>1 then n=n-1 end
if b==208 and n<l then n=n+1 end --slight modification from <=
if b==28 then break end
end
end
term.clear() term.setCursorPos(1,1)
return n
end
Posted 16 August 2012 - 01:49 PM
why don't you do the same thing but add
m=m or {}
at the beginning so if it doesn't get a first parameter it sets it to be an empty table rather than crashing?Posted 16 August 2012 - 01:55 PM
Oh, yeah, I will take that into consideration.
Posted 16 August 2012 - 02:02 PM
or you can have an if statement to check its type and return nil if it isn't a table
if type(m)~="table" then
print("incorrect usage")
if m then print(m) end
return nil
end