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

More problems with APIs...

Started by Endergreen117, 04 January 2015 - 09:53 PM
Endergreen117 #1
Posted 04 January 2015 - 10:53 PM
Hello once again, CC Forums. I have been working on something and while I was attempting to load a function from an API I was greeted with the following error:

"gen:44:attempt to perform arithmetic __sub on nil and number"

gen (the API):



mTime = true

function setColor(bc, tc)
if not bc then bc = 32768 end
if not tc then tc = 1 end
term.setBackgroundColor(bc)
term.setTextColor(tc)
end

function printLeft (y, str, bc, tc)
setColor (bc, tc)
term.setCursorPos (1, y)
term.write (str)
end

function printRight (y, str, bc, tc)
setColor (bc, tc)
term.setCursorPos (w-#str+1, y)
term.write (str)
end

function printCentred (y, str, bc, tc)
setColor (bc, tc)
term.setCursorPos (w/2-#str/2, y)
term.write (str)
end

function printHere (x, y, str, bc, tc)
setColor (bc, tc)
term.setCursorPos (x, y)
term.write (str)
end

function getTime ()
return textutils.formatTime (os.time (), mTime)
end

function drawError (str)
waiting = true

while waiting do
setColor (2048, 2)
term.clear ()
printCentered ((h-1)/2, str, 2048, 2)

printCentered (h-4, "Press any key to continue", 2048, 2)

local event = os.pullEventRaw ()
if event == "char" then
setColor (1, 256)
term.clear ()
sleep (1)
os.reboot ()

end

end

end

function printTime (x, y, bc, tc)
local tTime = getTime ()
if x == "default" then x = (w-#tTime)+1 end
printHere (x, y, tTime, bc, tc)
end


HASH.lua (the main file):


-- Variables
local osVer = "Alpha v1.0"
local w,h = term.getSize ()
local panelSide = "MahDick"
local panelStyle = 0local dBCol = 256
local dTCol = 32768
local dWall = "/Users/Admin/Images/Sample Images/computer.nfp"
local dWStyle = "centered"
local updateFreq = "auto"
local updates = "locked"local tgt = 0
-- Tables
-- Functions
local function drawPanel ()
if panelStyle == 0 then
  if panelSide == "top" then
   paintutils.drawLine (1, 1, w, 1, 128)
   gen.printHere (2, 1, "#", 128, 2)
   gen.printTime (w-4, 1, 128, 1)
  
  elseif panelSide == "bottom" then
   paintutils.drawLine (1, h, w, h, 128)
   gen.printHere (2, h, "#", 128, 2)
   gen.printTime (w-4, h, 128, 1)
  
  else
   gen.drawError ("Sorry, this is still a WIP.")
  
  end

elseif panelStyle == 1 then
  gen.drawError ("Sorry, this is still a WIP.")

else
  gen.drawError ("The requested 'panelStyle' does not exist.")

end
term.setCursorPos (1, h)

endlocal function drawDesktop ()
gen.setColor (1, 32768)
term.clear ()
term.setCursorPos (1, 2)
wallpaper = paintutils.loadImage (dWall)
if dWStyle == "centered" then
  paintutils.drawImage (wallpaper, math.ceil ( (w-#wallpaper[1])/2 ), math.ceil ( (h-#wallpaper)/2) )

elseif dWStyle == "normal" then
  paintutils.drawImage (wallpaper, 1, 2)

end
end-- Main
drawDesktop ()
drawPanel ()

Also, I believe that I was testing the gen.drawError function by putting in the wrong side for the drawPanel function.
Mr. Bateman #2
Posted 04 January 2015 - 11:00 PM
Your error would be in the gen API, as on line 44 you do this:

printCentered ((h-1)/2, str, 2048, 2)
But more specifically, the error is:

h-1
You haven't defined "h", so it's trying to subtract 1 from nothing, which is causing the error.
Endergreen117 #3
Posted 04 January 2015 - 11:04 PM
Thanks. I've gotta stop staying up so late…
Bomb Bloke #4
Posted 04 January 2015 - 11:04 PM
Note that defining variables in your main script won't allow your API to access them (not without a bit of hoop-jumping, but that'd be bad practise anyways).

Either define a function in the API that allows you to pass values to it, or have the API grab the screen dimensions itself.