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

Window api error, but I am not calling the window api at all?

Started by DannySMc, 04 July 2014 - 10:21 PM
DannySMc #1
Posted 05 July 2014 - 12:24 AM
Okay so this is my code:

function errorLogging()
cs()
drawBox(1, 51, 1, 1, " ", tc, bc)
printC(">> Error Logging for DHD Program <<", 1, true, tc, bc)
print("> This will display the most recent errors that the")
print("  program has picked up on and logged!")
term.setCursorPos(1,4)
errorDatabase = db.load(errorLog)
for _, v in ipairs(errorDataBase) do
  print(v)
end
drawBox(1, 51, 19, 1, " ", tc, bc)
printC(">> 'B' = Back <<", 19, false, tc, bc)
end

here is the drawBox function:

function drawBox(StartX, lengthX, StartY, lengthY, Text, Color, BkgColor) -- does what is says on the tin.
  local x, y = term.getSize()
  if Color then setCol(Color, BkgColor) end
  if not Text then Text = "*" end
  lengthX = lengthX - 1
  lengthY = lengthY - 1
  EndX = StartX + lengthX 
  EndY = StartY + lengthY
  term.setCursorPos(StartX, StartY)
  term.write(string.rep(Text, lengthX))
  term.setCursorPos(StartX, EndY)
  term.write(string.rep(Text, lengthX))
  for i = StartY, EndY do
    term.setCursorPos(StartX, i)
    term.write(Text)
    term.setCursorPos(EndX, i)   
    term.write(Text)
  end
  resetCol(Color, BkgColor)
  return true 
end

Here is the printC function:

function printC(Text, Line, NextLine, Color, BkgColor) -- print centered
  local x, y = term.getSize()
  x = x/2 - #Text/2
  term.setCursorPos(x, Line)
  if Color then setCol(Color, BkgColor) end
  term.write(Text)
  if NextLine then
    term.setCursorPos(1, NextLine)
  end
  if Color then resetCol(Color, BkgColor) end
  return true 
end

this is only a small part of it, but the error is:

window:248: bad argument: double expected, got boolean

Can't see the problem, and I am not even using the Window API….:/

It gets to the drawing part and sets up the background colour then stops….
Dog #2
Posted 05 July 2014 - 12:38 AM
The window api makes troubleshooting some problems unnecessarily difficult, imo - I understand your frustration. In the future, though, please post the entire code, not just snippets/individual functions - this often makes finding the problem easier.

That error, based on the code you posted, means that you are trying to set the cursor position with something *other than* two numbers. 'setCursorPos' expects 2 number values and one (or more) of the variables you are using is probably not being set to any value so it is nil/false.

Try adding some print statements that print the values you are passing to setCursorPos before you actually set the cursor position. Here's an example for your printC function…

print(tostring(x))
print(tostring(Line))
sleep(1)
term.setCursorPos(x, Line)

print(tostring(NextLine))
sleep(1)
term.setCursorPos(1, NextLine)

This will temporarily mess up any screen formatting you have, but it'll let you see what values you are passing and help you track down the troublemaker. Make sure to do this in all your functions.

Hope that helps :)/>
Edited on 04 July 2014 - 10:38 PM
Bomb Bloke #3
Posted 05 July 2014 - 02:24 AM
NextLine's the boolean.

Re the window API, as of CC1.6, all advanced systems load multishell on boot. Every tab handled by multishell gets its own window to render to - this is so that if you open more tabs and switch between them, the previous tab content can be restored by redrawing the relevant window when you switch back. Multishell is running even if you don't have more than one tab open (as this is the only way to be prepared for when you do open a second tab).

Thus, any code which renders to the default terminal is going through the window API to do it.
Edited on 05 July 2014 - 12:26 AM