basically, i want to create a function which allows you to draw a button and then call a function if it is clicked.
what i thought would work:
example:
function drawButton(func)
-- code stuff happens before this
func
end
function drawButton(func)
-- code stuff happens before this
func
end
function drawButton(func)
func()
end
drawButton(term.clear) --#Lack of () on term.clear
Yes, it's as easy as that. functions are just variables, and in lua, you can pass them to functions as such. If you call a function variable, it will run them.Minor note: I'm meaning in lua for functions are variables. Do not try to pass functions as arguments in Java or other languages.
function drawButton(x1,y1,x2,y2,color,func,args)
paintutils.drawFilledBox(x1,y1,x2,y2,color)
xb1 = x1 - 1
yb1 = y1 - 1
xb2 = x2 + 1
yb2 = y2 + 1
event, button, x, y = os.pullEvent("mouse_click")
if button == 1 then
if x < xb1 then
if x > xb2 then
if y < yb1 then
if y > yb2 then
func(args)
end
end
end
end
end
end
drawButton(1,1,5,5,colors.yellow,print,"test")
if x < xb1 then -- if x is smaller than 0? Never gonna happen
if x > xb2 then -- if x is bigger than 6? Impossible at this point
if y < yb1 then
if y > yb2 then
Pretty sure you got your larger than less than signs messed up.that part worked for me fine. when i did what you suggested, it came up with the same results, the function happening before the button press :/ do you know a way of fixing?Pretty sure you got your larger than less than signs messed up.if x < xb1 then -- if x is smaller than 0? Never gonna happen if x > xb2 then -- if x is bigger than 6? Impossible at this point if y < yb1 then if y > yb2 then
it came up with the same results, the function happening before the button press
im confused, is it now printing "test"?I'm trying to do a test program to see if my program works, but it won't call the print function
local function doPrint(arg1, ...) --#The ... means an unlimited amount of args.
print(arg1, ...)
end
drawButton(1,1,5,5,colors.yellow,doPrint,"test")
if button == 1 then
if x < xb1 then
if x > xb2 then
if y < yb1 then
if y > yb2 then
func(args)
end
end
end
end
end
if button == 1 and x < xb1 and x > xb2 and y < yb1 and y > yb2 then
func()
end