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

[Error] attempt to get length of function.

Started by Klintos8, 10 January 2013 - 01:46 AM
Klintos8 #1
Posted 10 January 2013 - 02:46 AM
Im getting and error with my code,
menu:5: attempt to get length of function
I dont know what I'm doing wrong, can someone please help me.



local w, h = term.getSize()
local select = 1

local function printCentered(str, ypos)
  term.setCursorPos(w/2 - #str/2, ypos)
  term.write(str)
end

local function printRight(str, ypos)
  term.setCursorPos(w - #str, ypos)
  term.write(str)
end

local function printKlintos(str, ypos)
  print(" ____ _______    ___ ____  ___ ___________ _________ __________")
  print("|   |//   //   |  |   |    \\|   |           |    _    |         //")
  print("|       //|   |__|   |         |___     ___|   | |   |    ____//")
  print("|       \\|      |   |         |   |   |   |   |_|   |____   //")
  print("|___|\\___\\______|___|___|\\____|   |___|   |_________|______//")
end

function drawMain()
  printCentered("UP", 8)
  printCentered("DOWN", 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 drawPicOne()
  local upList = {
"   /|\   ",
"  / | \  ",
" /  |  \ ",
"    |    ",
"    |    ",
"    |    ",
"    |    "
}

for i=1,#upList do
printCentered(upList[i], 3 + i)
end

printCentered("DOWN", h-4)
printCentered("Back", h-1)
local ypos = h-3;
if select == 2 then ypos = h end
printCentered("---------", ypos)
end

function drawPicTwo()
  local downList = {
"    |    ",
"    |    ",
"    |    ",
"    |    ",
" \  |  / ",
"  \ | /  ",
"   \|/   "
}

for i=1,#downList do
printCentered(downList[i], 2 + i)
end

printCentered("UP", h-4)
printCentered("Back", h-1)
local ypos = h-3;
if select == 2 then ypos = h end
printCentered("---------", ypos)
end

function drawHeader()
  printCentered((printKlintos), 1)
  printCentered(string.rep("-", w), 6)  
  printRight("by Klintos8", h)
end

local menustate = "main"

local mopt = {
  ["main"] = {
    options = {"pic1", "pic2", "quit"},
    draw = drawMain
  },
  ["pic1"] = {
    options = {"pic2", "main"},
draw = drawPicOne
  },
  ["pic2"] = {
    options = {"pic1", "main"},
draw = drawPicTwo
  }
}

function runMenu()
  while true do
    term.clear()
    drawHeader()
    mopt[menustate].draw()

    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

runMenu()
term.clear()
term.setCursorPos(1,1)
Orwell #2
Posted 10 January 2013 - 02:51 AM
Please post all your code, the error is where you call printCentered.
Klintos8 #3
Posted 10 January 2013 - 02:53 AM
Edited post.
I was just trying to make some simple gui using some tutorials i found for the first time.

Im fairly new to Lua aswell.
theoriginalbit #4
Posted 10 January 2013 - 03:00 AM
here is the problem



function drawHeader()
  printCentered((printKlintos), 1)

you are trying to print a function, which doesn't work…

did you mean


function drawHeader()
  printKlintos()
Klintos8 #5
Posted 10 January 2013 - 03:15 AM
Thankyou, i changed it and now it all is working thanks a lot :)/>