Jump to content




More problems with APIs...

api computer

3 replies to this topic

#1 Endergreen117

  • Members
  • 33 posts

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.

#2 Mr. Bateman

  • Members
  • 98 posts
  • LocationAustralia

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.

#3 Endergreen117

  • Members
  • 33 posts

Posted 04 January 2015 - 11:04 PM

Thanks. I've gotta stop staying up so late...

#4 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users