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

I need help with an API code

Started by CaosTECH, 20 September 2015 - 02:09 AM
CaosTECH #1
Posted 20 September 2015 - 04:09 AM
I know I have posted something kinda like this but I am creating a EXTREMELY simple API for buttons, but whenever I run my test program creating a simple button on the screen, it doesnt work. I don't know if its the test program or the API itself, i will post the code and test program below…

[size=6]Test Program[/size]
[size=6]os.loadAPI("btn")
btn.draw(3,6,3,6,colors.blue)
btn.make(3,6,3,6,1,reboot)[/size]

Simple API Code
draw = function(x,xx,y,yy,bcol)
paintutils.drawBox(x,xx,y,yy,bcol)
end

make = function(x1,xx1,y1,yy1,oot,prog)
slc = 0

if (slc = 0) then
if (event == "mouse_click") then
if (x >= x1 and x <= xx1 and y >= y1 and y <= yy1 and button == oot) then
shell.run(prog)
end
end
end
end


If there is something wrong with what my code is or what im trying to do PLZ TELL ME!
Edited on 20 September 2015 - 02:11 AM
Waitdev_ #2
Posted 20 September 2015 - 04:16 AM
does it give an error? therefore what error is it?
by looking at it, when your making the button i think you have to do something like this:

btn.make(3,6,3,6,1,"reboot")
CaosTECH #3
Posted 20 September 2015 - 04:29 AM
I tried your suggestion, "reboot", but the same thing happened, heres a pic
[attachment=2381:Screen Shot 2015-09-19 at 11.23.44 PM.png]

Heres what I did,
Spoiler>edit btn
(checked the code for mistakes with the button)
Save and Exit
>edit button (The test program)
replaced ,reboot) with ,"reboot")
Saved And Exit
>button (Ran the test program)
THEN THAT CAME UP (The image I posted)




Another spoiler??????
SpoilerWhats this?
SpoilerAnother??[
SpoilerOk, whats going on…
SpoilerFine, im done :D/>
/spoiler]



- Edit
I failed on multiple spoilers :o/>
Edited on 20 September 2015 - 02:34 AM
valithor #4
Posted 20 September 2015 - 04:31 AM
Test Program
os.loadAPI("btn")
btn.draw(3,6,3,6,colors.blue)
btn.make(3,6,3,6,1,reboot)

Simple API Code
draw = function(x,xx,y,yy,bcol)
  paintutils.drawBox(x,xx,y,yy,bcol)
end

make = function(x1,xx1,y1,yy1,oot,prog)
  slc = 0
  if (slc = 0) then
	if (event == "mouse_click") then
	  if (x >= x1 and x <= xx1 and y >= y1 and y <= yy1 and button == oot) then
		shell.run(prog)
	  end
	end
  end
end

Fixed the formatting for you.

Your problem is actually in your "make" function. From what I can tell, it is expecting to either be passed the results of a event, or the results of a event to be available, which neither are true in your test program. My suggestion would be to add the following code.

local event, button, x, y = os.pullEvent("mouse_click")

Right before the first if statement in your make function.
Edited on 20 September 2015 - 02:38 AM
CaosTECH #5
Posted 20 September 2015 - 04:50 AM
Omg, thanks!!!!!!!!!!!!!!
It works now, But whenever I click the point it makes this error
btn:12: attempt to index ? (a nil value)

And the point is supposed to be a 3x3 pixel, but only prints a 1x1 pixel in the coords given.
CaosTECH #6
Posted 20 September 2015 - 05:12 AM
I changed the code, it no longer tries to run "reboot" when it is clicked, it runs a program called "hi" which its contents are:
print("hi")

and now whenever I run the test program I get the error: 216
valithor #7
Posted 20 September 2015 - 05:37 AM
Your drawing problem is due to you not supplying the arguments to paintutils.draw in the order it wants. It expects them in the order of: xStart, yStart, xEnd, yEnd.

One thing to note. Whatever you pass as the program to run must be a string, or it will error.
CaosTECH #8
Posted 20 September 2015 - 06:17 AM
What do you mean by a string? I want it so that the user can make it run a program when the button is clicked, could you explain on how I could make it use a string?
Bomb Bloke #9
Posted 20 September 2015 - 06:26 AM
A string is generally a representation of text - you take a bunch of characters, "string" them together, and you can store words or phrases. See here for more info on data types.
CaosTECH #10
Posted 20 September 2015 - 06:45 AM
Ok, but this doesnt display how to use a button function using a string, just text. And what it displayed on the website is what I did
valithor #11
Posted 20 September 2015 - 08:06 AM
Ok, but this doesnt display how to use a button function using a string, just text. And what it displayed on the website is what I did

I pointed out that you needed to pass a string because in your test program:

Test Program
os.loadAPI("btn")
btn.draw(3,6,3,6,colors.blue)
btn.make(3,6,3,6,1,reboot)

You pass the variable reboot instead of the string "reboot" to the function btn.make. The variable reboot is nil because it is never defined in your code, and shell.run requires you to pass it a string. So it would error.
Edited on 20 September 2015 - 06:07 AM
CaosTECH #12
Posted 16 October 2015 - 05:33 PM
Sorry, I have not replied in 2 months. I have went off and learned alot about lua. I understand now, I would have to use:
--Test Program
os.loadAPI("btn")
reboot = "reboot"
btn.draw(3,6,3,6,colors.blue)
btn.make(3,6,3,6,1,reboot)
--End

Thx for the help
TYKUHN2 #13
Posted 16 October 2015 - 08:25 PM
Try not to use reboot = "reboot" btn.make(reboot)
Instead just do btn.make('reboot")
HPWebcamAble #14
Posted 22 October 2015 - 03:55 AM
Try not to use reboot = "reboot" btn.make(reboot)
Instead just do btn.make('reboot")

Translation:

Use this:

btn.make(3,6,3,6,1, "reboot" )
(Directly passing the name as a string)

Instead of this:

reboot = "reboot"

btn.make(3,6,3,6,1,reboot)
(Storing the name, then passing it)


It's really fine to use either method. The first is more efficient (only slightly),
but you may need to use the second in some situations (eg you want to use the same name for another button)
Edited on 22 October 2015 - 01:55 AM