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

Button API return attempt to call nil

Started by macOScrazy, 29 March 2016 - 03:41 AM
macOScrazy #1
Posted 29 March 2016 - 05:41 AM
So Im trying to build a button API to use for my CC projects, I found one that Direwolf20 made and edited it to try and make it work but whenever I call the API and try to use it it returns nil.

Here's the API:

local Button = {}
local FillColor = colors.cyan
local ActiveColor = colors.blue
local TextColor = colors.white
function SetTable(name, func, x, y, width, height)
Button[name] = {}
Button[name]["Func"] = func
Button[name]["Active"] = false
Button[name]["X"] = x
Button[name]["Y"] = y
Button[name]["Width"] = width
Button[name]["Height"] = height
end
function ClearTable()
Button = {}
end
function SetFillColor(color)
if (colors.combine(color,color)) then
  FillColor = color
end
end
function SetTextColor(color)
if (colors.combine(color,color)) then
  TextColor = color
end
end
function DrawButton(text,color,bData)
term.setBackgroundColor(color)
term.setTextColor(TextColor)

local textCenter = string.len(text)/2
local yCenter = bData["Y"]+bData["Height"]/2+1
local xCenter = bData["X"]+bData["Width"]/2-textCenter-1
for y = bData["Y"],bData["Height"] do
  term.setCursorPos(bData["X"],y)
  if y == yCenter then
   for x = 0, bData["X"]+bData["Width"]-textCenter-1 do
	if x==xCenter then
	 term.write(text)
	else
	 term.write(" ")
	end
   end
  else
   for i = bData["X"],bData["Width"] do
	term.write(" ")
   end
  end
end
end
function DrawtoScreen()
for name,data in ipairs(Button) do
  local on = data["Active"]
  if on == true then
   FillColor = FillColor
  else
   FillColor = ActiveColor
  end
  DrawButton(name,FillColor,data)
end
end
function ToggleButton(name)
Button[name]["Active"] = not Button[name]["Active"]
DrawtoScreen()
end
function PressButton(name)
ToggleButton(name)
DrawtoScreen()
sleep(0.15)
ToggleButton(name)
DrawtoScreen()
end
function GetClick()
local event,side,x,y = os.pullEvent("mouse_click")
for name,data in ipairs(Button) do
  if x >= data["X"] and x <= data["Width"] and y >= data["Y"] and y <= data["Height"] then
   data["Func"]()
   return true
  end
end
end

and here is me trying to use it:

--Desktop
os.loadAPI("Button")
slc = 0
titleBarColor = 256
titleBarTextColor = 32768
function titleBar()
term.setCursorPos(1,1)
term.setBackgroundColor(titleBarColor)
term.setTextColor(titleBarTextColor)
term.clearLine()
titleBarButtons()
end
function start()
return true
end
function drawDesktop()
term.clear()
titleBar()
Button.SetTable("Name", start, 2, 2, 10, 4)
Button.DrawtoScreen()
end
drawDesktop()

Please Help!
Dragon53535 #2
Posted 29 March 2016 - 07:17 AM
Can you give the entire error message so we can see where it does that?
moTechPlz #3
Posted 29 March 2016 - 12:02 PM
You try to call titleBarButtons but you did not declare this function.
AgisTheTekky #4
Posted 29 March 2016 - 02:15 PM
As far as I can see, there is nothing wrong with your API. In your desktop program however, you try to call titleBarButtons(), a function you haven't defined. Also as a sidenot: next time, please indent your functions to, and indent with the same space on all places.
macOScrazy #5
Posted 30 March 2016 - 06:12 PM
As far as I can see, there is nothing wrong with your API. In your desktop program however, you try to call titleBarButtons(), a function you haven't defined. Also as a sidenot: next time, please indent your functions to, and indent with the same space on all places.

Okay Thank you so Much! :)/>