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

Trouble With Buttons Again

Started by Alexander0507, 18 September 2013 - 11:10 AM
Alexander0507 #1
Posted 18 September 2013 - 01:10 PM
I'm using Direwolf20's ButtonAPI : http://pastebin.com/S8x0K3ui
And his demo-programm: http://pastebin.com/xVmKfn2Y

Monitors are on the top. I get the Error :
demo:6: attempt to index ? (a nil value)

can someone help me ?
MKlegoman357 #2
Posted 18 September 2013 - 01:49 PM
You have to first define functions that you use in other functions:


function fillTable()
   button.setTable("Test1", test1, 10,20,3,5)
   button.setTable("Test2", test2, 22,32,3,5)
   button.setTable("Test3", test3, 10,20,8,10)
   button.setTable("Test4", test4, 22,32,8,10)
   button.screen()
end

--snip--

function test1()
   button.flash("Test1")
   print("Test1")
end

function test2()
   button.toggleButton("Test2")
   print("Test2")
end

function test3()
   print("Test3")
end

function test4()
   print("Test4")
end

should be:


function test1()
   button.flash("Test1")
   print("Test1")
end

function test2()
   button.toggleButton("Test2")
   print("Test2")
end

function test3()
   print("Test3")
end

function test4()
   print("Test4")
end

function fillTable()
   button.setTable("Test1", test1, 10,20,3,5)
   button.setTable("Test2", test2, 22,32,3,5)
   button.setTable("Test3", test3, 10,20,8,10)
   button.setTable("Test4", test4, 22,32,8,10)
   button.screen()
end

What you should really do is start using other people button apis. I have heard that Direwolf20's ButtonAPI is buggy, hard to use, etc. Try Lyqyd's button API. I think it's a good, easy to use API.
Edited on 18 September 2013 - 11:58 AM
theoriginalbit #3
Posted 18 September 2013 - 02:03 PM
-snip-
Yes that is a problem, but not the problem at hand.

The error that occurs is that the table button does not exist when attempting to call button.setTable. The reason this error is occurring is most likely because OP has failed to save direwolf's API into a file called `button`. When calling os.loadAPI("button") on line 1 this is getting the file `button` and loading it into a table `button`, you're missing step 1, the file named `button`.
Alexander0507 #4
Posted 18 September 2013 - 02:48 PM
I didn't thought that something like this could produce such errors :D/>
Thx @
theoriginalbit
BigTwisty #5
Posted 18 September 2013 - 04:14 PM
Notice that the error is on the first line in the demo program that tried to access a member of the button table. That particular error crops up when you try to index (access a table member of) a variable that doesn't contain a table reference. Common sense would then lead one to look at what might cause the variable in question to NOT contain the expected table. In this case, the obvious answer is that os.loadAPI("button") didn't return a table reference. Quod erat demonstrandum…
H4X0RZ #6
Posted 18 September 2013 - 04:19 PM
Notice that the error is on the first line in the demo program that tried to access a member of the button table. That particular error crops up when you try to index (access a table member of) a variable that doesn't contain a table reference. Common sense would then lead one to look at what might cause the variable in question to NOT contain the expected table. In this case, the obvious answer is that os.loadAPI("button") didn't return a table reference. Quod erat demonstrandum…
I think bit has said that above… But: errare humanum esse :D/>
BigTwisty #7
Posted 18 September 2013 - 04:32 PM
Don't know how I missed that post… damn. You are partially correct: To err IS human, but to forgive is not company policy!
theoriginalbit #8
Posted 18 September 2013 - 04:39 PM
Notice that the error is on the first line in the demo program that tried to access a member of the button table. That particular error crops up when you try to index (access a table member of) a variable that doesn't contain a table reference. Common sense would then lead one to look at what might cause the variable in question to NOT contain the expected table. In this case, the obvious answer is that os.loadAPI("button") didn't return a table reference. Quod erat demonstrandum…
Actually you don't win (I'm assuming you used Q.E.D. in the form of "I win"). You just vomited a bunch of text on to the page that any newbie would not even understand, sure quite a lot on the forums can read it and understand, and I have the common sense you speak of to look at the errors and variables, but not everyone here can (actually I would hazard saying >80%). You have to understand that a lot of people on these forums do not understand how to read the error messages (or code and its documentation it seems), and do not understand the amount of useful information that can be contained in error messages, let alone understand debugging! The point of Ask a Pro is not for us to feed them answers with high level concepts or technical words (unless of course thats what they want), this just gets them coming back with more questions because they're confused, the point is for us to help them and give explanations in such a way that helps them, not confuses them more.

EDIT:
Don't know how I missed that post… damn.
How did you miss that?! It was literally the last comment on the page, I'd understand if it was like higher up and hidden by other lower ones, but how?!
Edited on 18 September 2013 - 02:46 PM
Alexander0507 #9
Posted 19 September 2013 - 01:25 PM
I'm now using Lyqyd's button API
but is there a way to set the side of the redstone signal

I'm using his test-program but if i change the side how i would do it normaly it says invalid side.
program: (http://www.computercraft.info/forums2/index.php?/topic/14784-touchpoint-api/)
BigTwisty #10
Posted 19 September 2013 - 02:30 PM
-snip
  1. Q.E.D. doesn't mean "I win." It means "that which was to be demonstrated." I wasn't aware this was a contest.
  2. The OP said he didn't realize that error could occur because of this, so I tried to walk him through the logical process of bug hunting, which is a difficult skill to learn. I'm sorry you see that as "vomit".
  3. How did I miss it? It was not the last post on the page. Even so, when skimming through the thread, all I saw was "Yes that is a problem, but not the problem at hand." For some reason my visual filters removed the whole next paragraph, and I didn't want the OP left hanging. Mistakes happen, and I already apologized for this one. There's no need to be a douche nozzle about it.
Lyqyd #11
Posted 19 September 2013 - 03:12 PM
Let's play nice, gentlemen. BigTwisty's post may have been worded densely, but it definitely didn't deserve the rant. The rant shouldn't have been replied to either. Let's move on.

I'm now using Lyqyd's button API
but is there a way to set the side of the redstone signal

I'm using his test-program but if i change the side how i would do it normaly it says invalid side.
program: (http://www.computercraft.info/forums2/index.php?/topic/14784-touchpoint-api/)

The example program uses the names of the buttons as the sides. To use other names, you simply add an if tree to check the name given in the event and control the relevant output sides. If that doesn't make sense, I can provide some example code later.
BigTwisty #12
Posted 19 September 2013 - 03:29 PM
@Lyqyd: Sorry, Dad… ;)/>

@Alex: Are you talking about the line where you set the side for the monitor?

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

If so, are you surrounding the side string with quotes? Are you pointing at a valid side? With all the possibilities, it would help if we could see your code.
Alexander0507 #13
Posted 20 September 2013 - 09:05 AM
Lyqyd which variable is the button name ?
(sorry i'm not the best :D/>)
BigTwisty #14
Posted 20 September 2013 - 10:13 AM

t:add( [button name], [function], [xMin], [yMin], [xMax], [yMax] )

The first parameter in t:add is the button name. The second is the function you want called when you click on the button.
BigTwisty #15
Posted 20 September 2013 - 10:20 AM

t:add(name, func, xMin, yMin, xMax, yMax, inactiveColor, activeColor)

The first parameter in t:add is the name (and the displayed text) for the button. The second is the function you want to run when the user clicks the button.

Example:


local function click()
  print("Do you always click buttons?")
end

local t = touchpoint.new("top")  -- for a monitor on top of the computer

local t:add("Click me!", click, 3, 3, 15, 5, colors.red, colors.lime) -- Adds a button

t:run() -- Runs the program
Lyqyd #16
Posted 20 September 2013 - 01:24 PM
So, an example of changing the sample program to use non-side names would be:


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

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

--# add two buttons
t:add("blaze", nil, 2, 2, 14, 11, colors.red, colors.lime)
t:add("witch", nil, 16, 2, 28, 11, colors.red, colors.lime)

--# draw the buttons
t:draw()

while true do
  --# handleEvents will convert monitor_touch events to button_click if it was on a button
  local event, p1 = t:handleEvents(os.pullEvent())
  if event == "button_click" then
    --# p1 will be "blaze" or "witch", since those are the button labels
    --# toggle the button that was clicked.
    t:toggleButton(p1)
    --# and toggle the redstone output on the relevant side.
    if p1 == "blaze" then
      rs.setOutput("left", not rs.getOutput("left"))
    elseif p1 == "witch" then
      rs.setOutput("right", not rs.getOutput("right"))
    end
  end
end

Or, if you wanted to use :run() instead, it's pretty easy still:


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

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

--# create a helper function to toggle redstone
local function toggleSide(side)
  rs.setOutput(side, not rs.getOutput(side))
end

--# create functions for button clicks
local function blazeClick()
  t:toggleButton("blaze")
  toggleSide("left")
end

local function witchClick()
  t:toggleButton("witch")
  toggleSide("right")
end

--# add two buttons
t:add("blaze", blazeClick, 2, 2, 14, 11, colors.red, colors.lime)
t:add("witch", witchClick, 16, 2, 28, 11, colors.red, colors.lime)

--# let the button API take care of the rest.
t:run()
Alexander0507 #17
Posted 20 September 2013 - 05:55 PM
Now i get button out of bounds ?
Lyqyd #18
Posted 20 September 2013 - 09:05 PM
Try adjusting the coordinate pairs to ensure the buttons fit on the screen. If I recall correctly, the example was intended to be used on a 3x2 monitor.