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

im making a New API... and i Need HELP with the Button function...

Started by jimisdam, 30 July 2014 - 07:59 PM
jimisdam #1
Posted 30 July 2014 - 09:59 PM
That's a part of My API

function Button(x1, y, x2, text)
  term.setCursorPos(x, y)
  term.clear()
  term.setBackgroundColor(colors.blue)
  term.write(text)
  getPressed = false
  event, button, X, Y = os.pullEvent()
  if event == "mouse_click" and button == "1" and Y <= y and Y >= y and X >= x1 and X <= x2 then
	getPressed = true
  end
  return getPressed
end

func = term.write("WellDone!!")

btnA = Button(3, 2, 6, 2, "Lele!")

if btnA == true then
  term.write("WellDone!!")
end

and i need to Know what's Wrong…
  1. What's wrong Generaly ???
  2. iThink that the Wrong thing is this with the Click Cords :/ (i can't find what)
​please Help me…

4Fun:

if myEnglish == "bad" then
  term.write("Sorry 4My bad English...")
else
  term.write("I don't need 2Say Sorry")
:P/>
KingofGamesYami #2
Posted 30 July 2014 - 10:21 PM
Well, there are a lot of problems with your button.
1) You never define x before using it in term.setCursorPos( x, y)
2) You only filter a single event, which will mess up pretty much everything.
3) func = term.write( "WellDone!!" ) What's with this? func will store nil. If you want it to be a function, declare it like this:

function func()
  term.write( "WellDone!!" )
end

4) none of you're variables are local. Declare a local variables by placing the keyword local before them. ( EG. "local x = 1" )
Edited on 30 July 2014 - 08:22 PM
Lignum #3
Posted 30 July 2014 - 10:50 PM
Your problem is here:

if event == "mouse_click" and button == "1" and Y <= y and Y >= y and X >= x1 and X <= x2 then

"button" is a number, not a string, meaning that the quotes around the number 1 in your comparison must be removed.

if event == "mouse_click" and button == 1 and Y <= y and Y >= y and X >= x1 and X <= x2 then
jimisdam #4
Posted 31 July 2014 - 11:34 AM
Well, there are a lot of problems with your button.
1) You never define x before using it in term.setCursorPos( x, y)
2) You only filter a single event, which will mess up pretty much everything.
3) func = term.write( "WellDone!!" ) What's with this? func will store nil. If you want it to be a function, declare it like this:

function func()
  term.write( "WellDone!!" )
end

4) none of you're variables are local. Declare a local variables by placing the keyword local before them. ( EG. "local x = 1" )

Im sorry i forgot 2 Remove the:
function func()
term.write( "WellDone!!" )
end
Nothing to do with ti..
jimisdam #5
Posted 31 July 2014 - 12:10 PM
Thank you
KingofGamesYami :D/> Im so Carefulless :\

Now i have another Problem…

if i want to set a Color… int the Params???
eg.

function Button(x1, y, x2, text, color)
  term.setCursorPos(x1, y)
  term.clear()
  term.setBackgroundColor(color)
  term.write(text)
  getPressed = false
  event, button, X, Y = os.pullEvent()
  if event == "mouse_click" and button == 1 and Y <= y and Y >= y and X >= x1 and X <= x2 then
    getPressed = true
  end
  return getPressed
end
btnExit = Button(50, 1, 50, "X", colors.red)
Whats wrong here ????
jimisdam #6
Posted 31 July 2014 - 12:11 PM
Your problem is here:

if event == "mouse_click" and button == "1" and Y <= y and Y >= y and X >= x1 and X <= x2 then

"button" is a number, not a string, meaning that the quotes around the number 1 in your comparison must be removed.

if event == "mouse_click" and button == 1 and Y <= y and Y >= y and X >= x1 and X <= x2 then
Thank you 2!!:D/>
amtra5 #7
Posted 31 July 2014 - 01:15 PM
Thank you
KingofGamesYami :D/>/> Im so Carefulless :\

Now i have another Problem…

if i want to set a Color… int the Params???
eg.

function Button(x1, y, x2, text, color)
  term.setCursorPos(x1, y)
  term.clear()
  term.setBackgroundColor(color)
  term.write(text)
  getPressed = false
  event, button, X, Y = os.pullEvent()
  if event == "mouse_click" and button == 1 and Y <= y and Y >= y and X >= x1 and X <= x2 then
	getPressed = true
  end
  return getPressed
end
btnExit = Button(50, 1, 50, "X", colors.red)
Whats wrong here ????
Color is an inbuilt API. Try using another variable name such as
writeColor
to avoid overwriting it!

See below.
Edited by
theoriginalbit #8
Posted 31 July 2014 - 01:36 PM
Color is an inbuilt API. Try using another variable name such as
writeColor
to avoid overwriting it!
no it's not. it's colors and colours
jimisdam #9
Posted 31 July 2014 - 02:52 PM
Color is an inbuilt API. Try using another variable name such as
writeColor
to avoid overwriting it!
no it's not. it's colors and colours
So… what i have to white in the color Variable to work right ????
Lyqyd #10
Posted 31 July 2014 - 03:25 PM
Um, no. That's not what he said. He said you shouldn't use the same variable name as an existing API (which you already weren't), then suggested the name writeColor (wRite, not wHite) to prevent conflicts. Please carefully read the whole post when someone provides help.
jimisdam #11
Posted 31 July 2014 - 04:38 PM
Um, no. That's not what he said. He said you shouldn't use the same variable name as an existing API (which you already weren't), then suggested the name writeColor (wRite, not wHite) to prevent conflicts. Please carefully read the whole post when someone provides help.
Can you give me an Example… please ?
i didn't get it :/
Bubba #12
Posted 31 July 2014 - 04:58 PM
Um, no. That's not what he said. He said you shouldn't use the same variable name as an existing API (which you already weren't), then suggested the name writeColor (wRite, not wHite) to prevent conflicts. Please carefully read the whole post when someone provides help.
Can you give me an Example… please ?
i didn't get it :/

amtra5 was wrong, you do not have any API conflicts. He thought that 'color' was an api already in used, and that your variable 'color' in the Button function would conflict with it. However, the actual API is 'colors' (or 'colours'), meaning that your code was in that instance just fine.

However, because you ask for an example, I've written one which is the WRONG way to do it: do not take this code, it is just an example of what amtra5 was describing.


function printText(text, colors)
  term.setTextColor(colors.white)
  print(text)
end

This code will error, with something along the lines of "attempt to index global 'colors' (a nil value)", because you overwrote the colors API with your own variable.
Edited on 31 July 2014 - 02:59 PM
jimisdam #13
Posted 31 July 2014 - 05:21 PM
Um, no. That's not what he said. He said you shouldn't use the same variable name as an existing API (which you already weren't), then suggested the name writeColor (wRite, not wHite) to prevent conflicts. Please carefully read the whole post when someone provides help.
Can you give me an Example… please ?
i didn't get it :/

amtra5 was wrong, you do not have any API conflicts. He thought that 'color' was an api already in used, and that your variable 'color' in the Button function would conflict with it. However, the actual API is 'colors' (or 'colours'), meaning that your code was in that instance just fine.

However, because you ask for an example, I've written one which is the WRONG way to do it: do not take this code, it is just an example of what amtra5 was describing.


function printText(text, colors)
  term.setTextColor(colors.white)
  print(text)
end

This code will error, with something along the lines of "attempt to index global 'colors' (a nil value)", because you overwrote the colors API with your own variable.
…I Didn't get it again… Can someone show me how can i do this RIGHT in my Button API ??? i want to set a Param called Colorr or color or col or ANYTHING… and with this Param/Variable do something like Dis…btn = Button(5, 3, 10, "Press", colors.blue)
Saldor010 #14
Posted 31 July 2014 - 08:17 PM
Well, if you're wanting to color the inside of your button (which I believe is what you're trying to do), you could always add this into the drawing code of your button.


-- If it is only on 1 Y plane, which I believe it is in your code.
paintutils.drawLine(x1,y,x2,y,color)
-- Or if you want your button to be on multiple Y planes.
for i=1,y2-y do
   paintutils.drawLine(x1,y+i-1,x2,y+i-1,color)
end

Also, I suggest you change your code a bit to support multiple buttons, because with the way it is now, it wouldn't work very well if you had more then 1.

For instance, you could register all of your buttons to a table like this,


local Buttons = {}
function RegisterButton(x1,y1,x2,y2,color,func,name)
   Buttons[name] = {x1,y1,x2,y2,color,func}
end

Then you could just have a main function that handles the drawing and mouse events for the buttons.


function Handler()
   for k,v in pairs(Buttons) do
		  for i=1,Buttons[k][4]-Buttons[k][2] do
				 paintutils.drawLine(Buttons[k][1],Buttons[k][2]+i-1,Buttons[k][3],Buttons[k][2]+i-1,Buttons[k][5])
		  end
   end
   local ev,p1,p2,p3,p4,p5 = os.pullEvent() -- This setup allows for additional events to be handled here, not just mouse events.
   if ev == "mouse_click" then
		  for k,v in pairs(Buttons) do
				 if p2 >= Buttons[k][1] and p2 <= Buttons[k][3] and p3 >= Buttons[k][2] and p3 <= Buttons[k][4] then
						Buttons[k][6]() -- Calls the Button's function.
				 end
		  end
   end
end

Here is some example code to go with the functions provided.


RegisterButton(1,1,4,4,colors.blue,function() print("Hi!") end,"Test")
Handler()

And that code should be ready to run right away. Of course, it would certainly need a bit more sprucing up and additions to it before you could call it a button API, but this should get you on your way :)/>

EDIT: You should probably add a loop for the handler function, but I have to go, so I can't change it right now.
Edited on 31 July 2014 - 06:35 PM
jimisdam #15
Posted 01 August 2014 - 05:31 PM
Well, if you're wanting to color the inside of your button (which I believe is what you're trying to do), you could always add this into the drawing code of your button.


-- If it is only on 1 Y plane, which I believe it is in your code.
paintutils.drawLine(x1,y,x2,y,color)
-- Or if you want your button to be on multiple Y planes.
for i=1,y2-y do
   paintutils.drawLine(x1,y+i-1,x2,y+i-1,color)
end

Also, I suggest you change your code a bit to support multiple buttons, because with the way it is now, it wouldn't work very well if you had more then 1.

For instance, you could register all of your buttons to a table like this,


local Buttons = {}
function RegisterButton(x1,y1,x2,y2,color,func,name)
   Buttons[name] = {x1,y1,x2,y2,color,func}
end

Then you could just have a main function that handles the drawing and mouse events for the buttons.


function Handler()
   for k,v in pairs(Buttons) do
		  for i=1,Buttons[k][4]-Buttons[k][2] do
				 paintutils.drawLine(Buttons[k][1],Buttons[k][2]+i-1,Buttons[k][3],Buttons[k][2]+i-1,Buttons[k][5])
		  end
   end
   local ev,p1,p2,p3,p4,p5 = os.pullEvent() -- This setup allows for additional events to be handled here, not just mouse events.
   if ev == "mouse_click" then
		  for k,v in pairs(Buttons) do
				 if p2 >= Buttons[k][1] and p2 <= Buttons[k][3] and p3 >= Buttons[k][2] and p3 <= Buttons[k][4] then
						Buttons[k][6]() -- Calls the Button's function.
				 end
		  end
   end
end

Here is some example code to go with the functions provided.


RegisterButton(1,1,4,4,colors.blue,function() print("Hi!") end,"Test")
Handler()

And that code should be ready to run right away. Of course, it would certainly need a bit more sprucing up and additions to it before you could call it a button API, but this should get you on your way :)/>

EDIT: You should probably add a loop for the handler function, but I have to go, so I can't change it right now.
Thank You Very MUCH !!!! :D/>
Saldor010 #16
Posted 01 August 2014 - 10:12 PM
Thank You Very MUCH !!!! :D/>

Glad I could be of assistance ^_^/>