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

Attempt to perform arithmetic: Add on numer and nil.

Started by MatazaNz, 30 June 2013 - 06:27 PM
MatazaNz #1
Posted 30 June 2013 - 08:27 PM
Before anyone says anything, yes, this is Direwolf 20's button api code.
My problem is that when I type it out myself, and even when I use the downloaded code, it gives me the same error. I know what is causing the error, but I don't know why, because it shouldn't be nil.

This is the code, I have commented where it is giving me the error, and where the variables should be defined.
Spoiler

local mon = peripheral.wrap("top")
mon.setTextScale(1)
mon.setTextColor(colors.white)
local button={}
mon.setBackgroundColor(colors.black)

function clearTable()
   button = {}
   mon.clear()
end
			  
function setTable(name, func, xmin, xmax, ymin, ymax)
   button[name] = {}
   button[name]["func"] = func
   button[name]["active"] = false
   button[name]["xmin"] = xmin
   button[name]["ymin"] = ymin
   button[name]["xmax"] = xmax
   button[name]["ymax"] = ymax
end

function funcName()
   print("You clicked buttonText")
end
		
function fillTable()
   setTable("ButtonText", funcName, 5, 25, 4, 8)
end	

function fill(text, color, bData)		   --This is where bData should be defined
   mon.setBackgroundColor(color)
   local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)			 --This is where I get the error
   local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1	--Here also
   for j = bData["ymin"], bData["ymax"] do
	  mon.setCursorPos(bData["xmin"], j)
	  if j == yspot then
		 for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
			if k == xspot then
			   mon.write(text)
			else
			   mon.write(" ")
			end
		 end
	  else
		 for i = bData["xmin"], bData["xmax"] do
			mon.write(" ")
		 end
	  end
   end
   mon.setBackgroundColor(colors.black)
end
	
function screen()
   local currColor
   for name,data in pairs(button) do
	  local on = data["active"]
	  if on == true then currColor = colors.lime else currColor = colors.red end
	  fill(name, currColor, data)			--This is where the fill function is called, thus where bData is given a value.
   end
end

function toggleButton(name)
   button[name]["active"] = not button[name]["active"]
   screen()
end	

function flash(name)
   toggleButton(name)
   screen()
   sleep(0.15)
   toggleButton(name)
   screen()
end
											
function checkxy(x, y)
   for name, data in pairs(button) do
	  if y>=data["ymin"] and  y <= data["ymax"] then
		 if x>=data["xmin"] and x<= data["xmax"] then
			data["func"]()
			return true
			--data["active"] = not data["active"]
			--print(name)
		 end
	  end
   end
   return false
end
	
function heading(text)
   w, h = mon.getSize()
   mon.setCursorPos((w-string.len(text))/2+1, 1)
   mon.write(text)
end
	
function label(w, h, text)
   mon.setCursorPos(w, h)
   mon.write(text)
end

Can anyone give me a clue as to what's going on? I doesn't work for me, but it works for Direwolf 20 on his tutorial video, and it's the exact same code, downloaded from pastebin.

Any help is greatly, greatly appreciated :D/>
Grim Reaper #2
Posted 30 June 2013 - 09:12 PM
How exactly are you using this API? It might be something in another file which is causing this problem because this code looks fine to me.
MatazaNz #3
Posted 30 June 2013 - 09:29 PM
How exactly are you using this API? It might be something in another file which is causing this problem because this code looks fine to me.
That's exactly my problem, it looks like it should work to me as well. Here's the program that's using it:

os.loadAPI("button")
m = peripheral.wrap("top")
m.clear()

function fillTable()
  button.setTable("Test1", test1, 10,20,3,5)
  button.setTable("Test2", test2, 22,32,3,5)
  button.setTable("Test3", test3, 10,20,8.10)
  button.setTable("Test4", test4, 22,32,8,10)
  button.screen()
end

function getClick()
  event, side, x, y = os.pullEvent("monitor_touch")
  button.checkxy(x,y)
end

function test1()
  button.flash("Test1")
  print("Test1")
end

function test2()
  button.toggleButton("Test2")
  print("Test2")
end

function test3()
  print("Test3")
end

function test4()
  print("Test4")
end

fillTable()
button.heading("Demo Button Prog")
button.label(1,5, "Demo!")
while true do
  getClick()
end
 

Does anything in this program look like it may be causing a problem?
MR_nesquick #4
Posted 30 June 2013 - 09:37 PM
did you name the API correctly? or is your monitor on top of the computer? because that looks identical to dire's code
MatazaNz #5
Posted 30 June 2013 - 09:39 PM
did you name the API correctly? or is your monitor on top of the computer? because that looks identical to dire's code
As stated in the OP, it is DW20's pastebinned code. And I am using it as identically to DW20 as possible to make it work.
MR_nesquick #6
Posted 30 June 2013 - 09:52 PM

function fill(text, color, bData)				  --This is where bData should be defined
   mon.setBackgroundColor(color) --< maybe here..    mon.setBackgroundColor(currColor)
   local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)				  --This is where I get the error
   local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1   --Here also



i think i found it. maybe a bugger from dire?
MatazaNz #7
Posted 30 June 2013 - 09:57 PM

function fill(text, color, bData)				  --This is where bData should be defined
   mon.setBackgroundColor(color) --< maybe here..	mon.setBackgroundColor(currColor)
   local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)				  --This is where I get the error
   local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1   --Here also



i think i found it. maybe a bugger from dire?

That shouldn't be it, because currColor is being called in the function arguments as color, so it wouldn't make a difference.
MR_nesquick #8
Posted 30 June 2013 - 10:01 PM
currColor = <colors.red> or <colors.lime>
color = <nothing>

pleas try it

if not you may need to to set a color < mon.setBackgroundColor(colors.lime) > because mon.setBackgroundColor is going to ask for a number and (color) isn't a number.
MatazaNz #9
Posted 30 June 2013 - 10:20 PM
currColor = <colors.red> or <colors.lime>
color = <nothing>

pleas try it

if not you may need to to set a color < mon.setBackgroundColor(colors.lime) > because mon.setBackgroundColor is going to ask for a number and (color) isn't a number.
Note this function:

function screen()
   local currColor
   for name,data in pairs(button) do
		  local on = data["active"]
		  if on == true then currColor = colors.lime else currColor = colors.red end
		  fill(name, currColor, data)				   --This is where the fill function is called, thus where bData is given a value.
   end
end

currColor is given a colors.<color> value, and is called in the fill function.
MR_nesquick #10
Posted 30 June 2013 - 10:32 PM
just downloaded it and it is working for me…


pastebin get HRbMF1Eg button
pastebin get xVmKfn2Y demo
MatazaNz #11
Posted 30 June 2013 - 11:17 PM
just downloaded it and it is working for me…


pastebin get HRbMF1Eg button
pastebin get xVmKfn2Y demo

I will try it soon. I will get back to you afterwards.
MatazaNz #12
Posted 30 June 2013 - 11:23 PM
just downloaded it and it is working for me…


pastebin get HRbMF1Eg button
pastebin get xVmKfn2Y demo

Thank you sir, it works. But I am completely dumbfounded as to how mine didn't. It was the same code…