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

Y is nil?

Started by SethShadowrider, 25 May 2013 - 12:17 PM
SethShadowrider #1
Posted 25 May 2013 - 02:17 PM
So I was working on a function to create a popup on the screen saying "Alert!" or such, and i'm using monitors with touchscreen to interact with it. Its worked relatively well exept for when checking the location of the touchscreen touch. To start off when trying to check where the touch was the y is apparently nil as if it was not given to the program. Also, even though the x supposedly gets sent to the program but when touching anywhere along the x coordinate it registers as a touch and ends the wait for the event even though it should clear the screen, and so does touching somewhere it shouldn't register even though it should do nothing. I'm also not very good at coding so its very messy. If you know whats going on could you show me what i'm doing wrong (and possibly how I could shorten and clean up my code?) Thanks in advance for the torrent of abuse i'm expecting about how messy my code is XD


local m = peripheral.wrap("back")
local w, h = m.getSize()
function getClick()
local event, side, x, y = os.pullEvent("monitor_touch")
return x, y
end
function printCenter(str, ypos)
m.setCursorPos(w/2 - #str/2, ypos)
m.write(str)
end
function popup(str)
m.setBackgroundColor(colors.lightBlue)
x = 5
while x < 15 do
printCenter("						    ", x)
x = x + 1
end
m.setBackgroundColor(colors.white)
x = 6
while x < 14 do
printCenter("						  ", x)
x = x + 1
end
m.setBackgroundColor(colors.lightBlue)
printCenter("Alert!", 5)
m.setBackgroundColor(colors.black)
m.setCursorPos(48, 5)
m.setBackgroundColor(colors.red)
m.write("X")
m.setBackgroundColor(colors.white)
m.setTextColor(colors.black)
printCenter(str, 7)
getClick()
if x == 38 and y == 5 then
m.setBackgroundColor(colors.black)
m.clear()
else
popup(str)
end
end
--Testing function
m.clear()
popup("Test")
m.setCursorPos(1, h)

Lyqyd #2
Posted 25 May 2013 - 03:03 PM
You aren't catching the return values from your getClick function, and you declare the values as local to that function. Try changing the usage to `local x, y = getClick()`.