This is the beginning of a button api I'm writing. I want the user to be able to press a button and trigger a specific function that's stored inside the button object.
os.loadAPI("gui")
--Button Class
Button = {
text = "Default",
tColor = colors.white,
bColor = colors.black,
borderColor = colors.gray,
onPressedColor = colors.lightGray,
func = nil,
x1 = 1,
x2 = 10,
y1 = 1,
y2 = 10,
}
function Button.__init__(self)
return setmetatable(self or {},{__index = Button})
end
function Button:draw()
gui.borderBox(self.x1,self.y1,self.x2,self.y2,self.bColor,self.borderColor)
gui.writeCentered(self.text,((self.x2-self.x1)/2)+self.x1,((self.y2-self.y1)/2)+self.y1,self.tColor,self.bColor)
end
function Button:setText(newText)
self.text = newText
end
--Testing Objects
local testButton1 = Button.__init__({
text="Test",
tColor=colors.blue,
bColor=colors.white,
func = function x() return Button:setText("Hello") end,
x1=20,
x2=30,
y1=1,
y2=5,
})
local testButton2 = Button.__init__({
text="Test 2",
tColor=colors.green,
bColor=colors.red,
x1=31,
x2=50,
y1=1,
y2=5,
})
testButton1:draw()
testButton2:draw()
os.sleep(3)
testButton1.func()
The code doesn't work now, because it gives a BIOS:366: [string "button"]:34: '(' expected.
How would I store a function in testButton1.func, and activate it?
To test it, here's the pastebin for my gui api: QzBSawh6