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.
here is the full code
test:36: attempt to perform arithmetic __add on number and nil
here is the function controlling the metamethod where the error is happeningSpoiler
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,
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