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

calling a function within a function defined by an argument

Started by Hydrotronics, 21 December 2015 - 09:59 AM
Hydrotronics #1
Posted 21 December 2015 - 10:59 AM
i didn't know what to call the title so I just typed what i'm asking

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
Dragon53535 #2
Posted 21 December 2015 - 11:20 AM

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.
Edited on 21 December 2015 - 10:21 AM
Hydrotronics #3
Posted 21 December 2015 - 11:29 AM
Minor note: I'm meaning in lua for functions are variables. Do not try to pass functions as arguments in Java or other languages.

lol ok thx! I had obvisously forgot to add the ().
Hydrotronics #4
Posted 21 December 2015 - 11:38 AM
ok just one more thing which is similar to what I asked here.

I'm trying to do a test program to see if my program works, but it won't call the print function

code:


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

What I'm Calling in the test program:

drawButton(1,1,5,5,colors.yellow,print,"test")
Edited on 21 December 2015 - 10:39 AM
CometWolf #5
Posted 21 December 2015 - 04:14 PM

		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.
Hydrotronics #6
Posted 21 December 2015 - 04:25 PM

		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?
CometWolf #7
Posted 21 December 2015 - 04:35 PM
I didn't suggest anything, i merely pointed out it won't work.
it came up with the same results, the function happening before the button press
I'm trying to do a test program to see if my program works, but it won't call the print function
im confused, is it now printing "test"?
Hydrotronics #8
Posted 21 December 2015 - 06:29 PM
yes, it now prints test, I was refering to the same result as the one Dragon gave me. Sorry, I must admit I did make it a little unclear. It now prints but before the button gets pressed, when I want it to print after the button gets pressed
Dragon53535 #9
Posted 22 December 2015 - 12:49 AM
I think it's with print being a weirdo, try making another dummy function that all it does is call print


local function doPrint(arg1, ...) --#The ... means an unlimited amount of args.
  print(arg1, ...)
end


drawButton(1,1,5,5,colors.yellow,doPrint,"test")


Also, you can use and to create longer booleans and use less if statements

  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
Edited on 21 December 2015 - 11:51 PM