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

attempt to perform arithmetic __add error

Started by Termanater13, 15 March 2014 - 05:55 AM
Termanater13 #1
Posted 15 March 2014 - 06:55 AM
I'm trying to make an argument in a metamethod a table for optional parts. but I just got an error I have not seen so I have no Idea how to solve.

test:36: attempt to perform arithmetic __add on number and nil
here is the function controlling the metamethod where the error is happening
Spoiler

new = function (self, id, text, xMin, Xmax, yMin, YMax, func, options) -- Add button to system
  self.Data[id] = {
	text = text,
	x = {
	  min = xMin,
	  max = xMax,
	},
	y = {
	  min = yMin,
	  max = yMax,
	},
	func = func,
	argPass = nil or options.argPass,
	color = {
	  text = {
		on = 0 or options.textOn,
		off = 0 or options.textOff,
	  },
	  back = {
		on = 0 or options.backOn,
		off = 0 or options.backOff,
	  },
	},
	click = true or options.click,
	display = true or options.display,
	hidden = false or options.hidden,
	status = false or options.status,
	user = nil or options.user,
  }
  self.Data[id].y.mid = math.floor((yMin+yMax)/2)  <-- where it says the error is at
  if ((xMax-xmin-string.len(text))%2) == 0 then
	self.Data[id].x.mid = ((xMax-xmin-string.len(text))/2)
  else
	self.Data[id].x.mid = (math.floor((xMax-xmin-string.len(text))/2)+1)
  end
end,
here is the full code
Spoiler

local button = {
  display = function (self) -- Draw the buttons on screen
	self.mon.clear()
	self.mon.setCursorPos(1,1)
	self.mon.write("X")
  end,
  new = function (self, id, text, xMin, Xmax, yMin, YMax, func, options) -- Add button to system
	self.Data[id] = {
	  text = text,
	  x = {
		min = xMin,
		max = xMax,
	  },
	  y = {
		min = yMin,
		max = yMax,
	  },
	  func = func,
	  argPass = nil or options.argPass,
	  color = {
		text = {
		  on = 0 or options.textOn,
		  off = 0 or options.textOff,
		},
		back = {
		  on = 0 or options.backOn,
		  off = 0 or options.backOff,
		},
	  },
	  click = true or options.click,
	  display = true or options.display,
	  hidden = false or options.hidden,
	  status = false or options.status,
	  user = nil or options.user,
	}
	self.Data[id].y.mid = math.floor((yMin+yMax)/2)   <-- where it says the error is at
	if ((xMax-xmin-string.len(text))%2) == 0 then
	  self.Data[id].x.mid = ((xMax-xmin-string.len(text))/2)
	else
	  self.Data[id].x.mid = (math.floor((xMax-xmin-string.len(text))/2)+1)
	end
  end,
  delete = function (self, id) -- removes button from system
	self.Data[id] = nil
  end,
  handleEvents = function (self) -- event handler for the API
	-- work later one understand more
  end,
  toggleStatus = function (self, id) -- toggles the status of the button
	self.Data[id].status = not self.Data[id].status
	self:display()
  end,
  toggleClick = function (self, id) -- toggles if the button can be clicked
	self.Data[id].click = not self.Data[id].click
	self:display()
  end,
  toggleDisplay = function (self, id) -- toggles the button is displayable
	self.Data[id].display = not self.Data[id].display
	self:display()
  end,
  toggleHidden = function (self, id) -- toggles if a button can be clicked if not displayed
	self.Data[id].hidden = not self.Data[id].hidden
	self:display()
  end,
  scale = function (self, scale)
	self.mon.setTextScale(scale)
	self.scale = scale
  end,
  update = function (self)
	self.mon = peripheral.wrap(self.side)
	self.mon.setTextScale(scale)
	for k,v in pairs(self.data) do
	  self.data[k].y.mid = math.floor((self.data[k].y.min+self.data[k].y.max)/2)
	  if ((self.Data[k].x.max-self.Data[k].x.min-string.len(self.Data[k].text))%2) == 0 then
		self.Data[k].x.mid = ((self.Data[k].x.max-self.Data[k].x.min-string.len(self.Data[k].text))/2)
	  else
		self.Data[k].x.mid = (math.floor((self.Data[k].x.max-self.Data[k].x.min-string.len(self.Data[k].text))/2)+1)
	  end
	end
	self:display()
  end,
}
function new(side)
  local newInstance = {
	side = side,
	mon = peripheral.wrap(side),
	Data = {},
	size = {},
	color = {
	  mon = {
		text = colors.white,
		back = colors.black,
	  },
	  off = {
		text = colors.white,
		back = colors.red,
	  },
	  on = {
		text = colors.white,
		back = colors.lime,
	  }
	},
	scale = 1,
  }
  newInstance.size.x, newInstance.size.y = newInstance.mon.getSize()
  setmetatable(newInstance, {__index = button})
  return newInstance
end
oeed #2
Posted 15 March 2014 - 07:00 AM
That error means your adding a number and nil as it states. This means that either yMax or yMin is nil. Trying printing both of them and see which one is nil.

Edit: after closer analysis I've noticed what's probably causing the issue. In the functions arguments yMax is Ymax, Lua is case sensitive, so you'll need to keep them the same, you've also got varying case on the Xmax name too.
Edited on 15 March 2014 - 06:03 AM
Termanater13 #3
Posted 15 March 2014 - 07:25 AM
That error means your adding a number and nil as it states. This means that either yMax or yMin is nil. Trying printing both of them and see which one is nil.

Edit: after closer analysis I've noticed what's probably causing the issue. In the functions arguments yMax is Ymax, Lua is case sensitive, so you'll need to keep them the same, you've also got varying case on the Xmax name too.
Ya, capitalization derp, fixed that and it worked. The issue was in a few spots.
CometWolf #4
Posted 15 March 2014 - 09:53 AM
Just for further reference, the order of number and nil in the error message isn't random. As such this error means it's the second number that's nil, which in this case would be yMax.
oeed #5
Posted 15 March 2014 - 10:09 AM
Just for further reference, the order of number and nil in the error message isn't random. As such this error means it's the second number that's nil, which in this case would be yMax.
Don't know why I hadn't seen that earlier, thanks!