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

Making a button

Started by Nic_Nacs, 02 September 2015 - 08:46 AM
Nic_Nacs #1
Posted 02 September 2015 - 10:46 AM
Currently I am trying to make a functioning button I used the code off http://www.computerc...utorial-part-i/ and followed the process all the way up to part III, however the person who made this tutorial has no longer posted any further tutorials on the subject so I am stumped on his last bit of coding


The setup:
Spoiler


The code http://pastebin.com/Vj6p0ASU

Everytime I run the script and test out the button I'll click a button that'll appear on the screen and then


firstButton.lua:173: attempt to call nil

I don't know why it keeps calling nil and if I replace the while loop with:

while true do
  event, side, xPos, yPos = os.pullEvent("[b]monitor_touch[/b]")
  print(event .. " => Side: " .. tostring(side) .. ", " ..
	"X: " .. tostring(xPos) .. ", " ..
	"Y: " .. tostring(yPos))
end

This shows me that there are values where I clicked, however for some reason the while loop

while true do
	event, side, xPos, yPos = os.pullEvent("monitor_touch")
	   for key, button in pairs(buttons) do
		  if button.click(xPos,yPos) then
			  redstone.setOutput(key, button.toggle())
			  button.draw(monitor1)
		  end
	 end
end

In this while loop 'xPos' and 'yPos' doesn't hold the value unless I covert it to a string character, but I can't use that but when I call the function hoping for a number it calls nil instead. *confused face*

I don't know where to look to find the answers I am looking for hence this post, I will be grateful for who ever can solve my problem or point me in the right direction
for me to solve my problem
Bomb Bloke #2
Posted 03 September 2015 - 12:34 AM
An "attempt to call nil" means that you're wanting to call a function, but the pointer you're using to do so is undefined.

Line 173, in the code you've linked, reads:

for key,button in pairs(buttons) do

The only function call there is of pairs(), but since that exists, that line won't throw that error. Seems you've changed things since getting it.

You're not calling xPos/yPos as functions, so their contents also won't lead to that error.

The line "if button.click(xPos,yPos) then" would throw an attempt to call nil (because button.click() doesn't exist; it's a typo of button.clicked())…