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

API not working or programming error I have done [solved]

Started by lebalusch, 24 May 2014 - 04:39 PM
lebalusch #1
Posted 24 May 2014 - 06:39 PM
Hi Guys and Girls,

I am a newbie to computercraft. I am getting a " button:9:attempt to index ? (a nil value) "
I understand this to mean its something wrong with the table but apart from that im guzupped. They seem to match to me.

filename: buttonAPI
http://pastebin.com/tCURBWrY

filename: button
http://pastebin.com/rCUANUNr

once i get this to work i can finally get to the job of gutting it and making my own version of it.
dont know if it makes any difference but both files are stored in the normal area of when you turn the pc on.

p.s yes this is a direwolf program that has just been retyped.

-edit was to mark as solved.
Edited on 26 May 2014 - 08:50 PM
CCJJSax #2
Posted 25 May 2014 - 02:00 AM
I haven't really tried to figure out how the button apis work, but I am told by much better programmers than I that lyqyd's Touchpoint API is much better than direwolfs.

I think your problem is you need to call buttonAPI.setTable(), because the api is buttonAPI.
KingofGamesYami #3
Posted 25 May 2014 - 04:00 AM
Button apis work through either monitor_touch or mouse_click, which return the x & y coordinates. Generally, the button api will define a "space" which is later compared to the x & y of the event. for instance:

local minx, miny = 3, 4
local maxx, maxy = 6, 5
would mean the button is triggered by an event in X area

000000
000000
000000
00XXXX
00XXXX
They compare it similar to this:

if eventx >= minx and eventx <= maxx and eventy >= miny and eventy <= maxy then
 --button was clicked
end
They also include a function of what the button actually does, which is called when clicked, and can include many other features, such as flashing the button a different color, automatically drawing the button for you, etc.
Personally, I prefer to define a "clickmap"

local clickmap = {}
clickmap.shutdown = {
 minx = 1,
 maxx = 5,
 miny = 3,
 maxy = 3,
 toggle = function() os.shutdown() end,
}
which is checked by my programs main loop, using for k, v in pairs.
lebalusch #4
Posted 26 May 2014 - 02:06 PM
Thank you guys for your reply much appreciated :)/>

@king of games yami
Thanks I am currently doing the 3 part button tutorial and a click map does seem more straight forwards I will have to look into it. Do you have any mini scripts I can view to see if I can follow it?

@CCJJsax
That at least works and is what I am aiming for thank you. But I am stumped as to where I can type in a function call in it.
(line numbers only been put in for explanation purposes)
Lines 6 and 7 are where I am adding a button spec. would I use a function name in there somehow so they can all be programmed with a function. Or 17 and 18. but that's just for toggling red stone on and off depending on the sides. Something I don't need to do as going to be using red net cable.
want to be able to stick say function left() and then type up all that button does in there.




1.--# load the touchpoint API
2.os.loadAPI("touchpoint")

3.--# intialize a new button set on the top monitor
4.local t = touchpoint.new("monitor_0")

5.--# add two buttons
6.t:add("left", nil, 2, 2, 14, 11, colors.red, colors.lime)
7.t:add("right", nil, 16, 2, 28, 11, colors.red, colors.lime)

8.--# draw the buttons
9.t:draw()

10.while true do
11.	   --# handleEvents will convert monitor_touch events to button_click if it was on a button
12.	  local event, p1 = t:handleEvents(os.pullEvent())
13.		if event == "button_click" then
14.			   --# p1 will be "left" or "right", since those are the button labels
15.				--# toggle the button that was clicked.
16.			   t:toggleButton(p1)
17.				--# and toggle the redstone output on that side.
18.				rs.setOutput(p1, not rs.getOutput(p1))
		end
end
Lyqyd #5
Posted 26 May 2014 - 06:58 PM
You'd just declare your functions before you add your buttons,


local function left()
  --do stuff
end

t:add("left", left, 2, 2, 14, 11, colors.red, colors.lime)

And then use the t:run() instead of the handleEvents construct with a while loop.
KingofGamesYami #6
Posted 26 May 2014 - 07:28 PM
Thank you guys for your reply much appreciated :)/>

@king of games yami
Thanks I am currently doing the 3 part button tutorial and a click map does seem more straight forwards I will have to look into it. Do you have any mini scripts I can view to see if I can follow it?
-snip-
Unfortunately, I don't have any "small" scripts readily available, but it would be no trouble to write one real quick :)/>

local clickmap = {}
clickmap.desktop = {}
local desktop = clickmap.desktop --#create a pointer

--#insert random UI code
slc = "desktop"  --#slc is just a random variable I use to determine where my program is.  eg. on the desktop, on a menu, on a submenu, etc.  It allows for "pages" of clickmaps, or buttons.
term.setCursorPos(1, 1)
term.write("[shutdown]")
desktop.shutdown = {
  maxx = 10,
  minx = 1,
  miny = 1,
  maxy = 1,
  toggle = function() os.shutdown() end,  --#wrap your code in a function so that it is not called immediately.  Also, the function should not have any arguments, because they will not be passed any during the main loop.  This is similar to using the parallel api, in that you cannot/shouldn't pass arguments.
}

main = function() --#main loop of the program
  event = os.pullEvent()
  if event[1] == "mouse_click" then
   for k, v in pairs( clickmap[slc] ) do
    local click = clickmap[slc]
    if click.minx <= event[3] and click.maxx >= event[3] and click.miny <= event[4] and click.maxy >= event[4] then
     click.toggle()
    end
   end
  elseif true then --#other event checks dependant on the program
  end
end
Ask if you have any questions on what this code is doing :D/>/>/>
Edited on 26 May 2014 - 05:34 PM
lebalusch #7
Posted 26 May 2014 - 07:38 PM
@Lyqyd Thank you. Might give this one ago also as its a smaller program.

If anybody is interested I got the dierwolf20 one to work there was a few mistakes in it not sure if it was myself or the download of it. but working now.

APIbutton: http://pastebin.com/tCURBWrY

button:http://pastebin.com/rCUANUNr

Admin you can marked this as solved.