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

Monitor Touch Screen Program Getting X and Y Help.

Started by applesauce10189, 11 December 2013 - 02:37 PM
applesauce10189 #1
Posted 11 December 2013 - 03:37 PM
I'm basically making random programs, playing around with functions and such slowly trying to learn as much as I can about ComputerCraft (I'm new) the top half of my program is perfectly fine, but I'm trying to add the X and Y coords of where you click on the screen to the monitor, which I've never even attempted before, I thought I had the just of it but apparently that isn't the case. Here's the code, I'll put a comment on where I started adding and show what I'm trying to do and where.


monitor = peripheral.wrap("right")
monitor.setCursorPos(1,1)
monitor.clear()
monitor.setTextScale(1)
while true do
  if rs.getInput("left") == true then
    monitor.write("Redstone signal is on.")
    break
  else
    monitor.write("Please turn me on. (Not in that way)")
    break
  end
end
monitor.setCursorPos(1,2)
monitor.write("This monitor is for testing")
monitor.setCursorPos(1,3)
monitor.write("If you want to see what I'm")
monitor.setCursorPos(1,4)
monitor.write("testing right now hit the lever")
monitor.setCursorPos(1,5)
monitor.write("and type 'test' into the computer")
monitor.setCursorPos(1,7)
cursorX, cursorY = monitor.getCursorPos() --This is where I started adding, this works fine though,
monitor.write("X:"..cursorX.." Y:"..cursorY)
print("X:"..cursorX.." Y:"..cursorY)
while true do --Around here is where things begin to go a bit downhill,
  x,y = os.pullEvent(monitor_touch) --This is (I believe) the center of the problem,
  if os.pullEvent == monitor_touch then --I was messing around after I couldn't get it right,
    monitor.setCursorPos(1,9)
    monitor.write("X:"..x.." Y:"..y) --Before I started screwing around this said "Monitor_touch" and "Right" if I remember correctly, that's a big if because I make mistakes,
  else
  monitor.setTextColor(colors.red) --I don't understand why but this changes the text above apparently?
  monitor.write("FAIL") --Again, was messing around after I couldn't get it right,
  end
end
Kingdaro #2
Posted 11 December 2013 - 05:23 PM
Yes, that is the incorrect usage of os.pullEvent. I believe this is what you'll want:

event, side, x, y = os.pullEvent("monitor_touch")

os.pullEvent always returns the event first before anything, even if you're using a filter. The monitor_touch event returns the side (or name, if you're using cables) of the monitor that was touched before it returns the x and y. After that, you'll also want quotes around the event you want to filter, because the function accepts a string.

When you are using a filter, there's no need to check if the event you want went through. Regardless, when checking for events, you'll want to do this:
if event == "monitor_touch" then
Edited on 12 December 2013 - 06:53 PM
applesauce10189 #3
Posted 12 December 2013 - 01:08 PM
Thank you I was ripping my hair out wondering what to do, one weird bug though that I noticed, not sure if it's my code's side or ComputerCraft side, I'm on FTB Unleashed 1.1.3 and for some reason towards the bottom the Y is about 20, around the top the Y is about 80 or so, going up from there is a bit random and going down from the bottom increases the Y, This confuses me, and in the middle theres a random jump from about 20 to 80, but it's still pretty random like in the 70's and 60's, it's a 5x4 monitor, here's an updated version of the code,

Ignore this, I'm almost 100% sure the problem is after the Y isn't being deleted and its just pushing it aside or something, or writting over it, fixed it with a string made of spaces after the Y,


monitor = peripheral.wrap("right")
monitor.setCursorPos(1,1)
monitor.clear()
monitor.setTextScale(1)
while true do
  if rs.getInput("left") == true then
	monitor.setTextColor(colors.white)
	monitor.write("Redstone signal is on.")
	break
  else
	monitor.setTextColor(colors.white)
	monitor.write("Please turn me on. (Not in that way)")
	break
  end
end
monitor.setTextColor(colors.white)
monitor.setCursorPos(1,2)
monitor.write("This monitor is for testing")
monitor.setCursorPos(1,3)
monitor.write("If you want to see what I'm")
monitor.setCursorPos(1,4)
monitor.write("testing right now hit the lever")
monitor.setCursorPos(1,5)
monitor.write("and type 'test' into the computer")
monitor.setCursorPos(1,7)
cursorX, cursorY = monitor.getCursorPos()
monitor.write("X:"..cursorX.." Y:"..cursorY)
print("X:"..cursorX.." Y:"..cursorY)
while true do
  event,side,X,Y = os.pullEvent("monitor_touch")
  if right == monitor_touch then
	monitor.setCursorPos(1,9)
	monitor.write("X:"..X.." Y:"..Y)
  else
  monitor.setCursorPos(1,9)
  monitor.setTextColor(colors.red)
  monitor.write("FAIL")
  end
end
Edited on 12 December 2013 - 12:17 PM
Kingdaro #4
Posted 12 December 2013 - 07:56 PM
That's still wrong. I noticed I made a mistake in my original post, so my bad on that. I meant to say that you check if the event is monitor_touch like so:
if event == "monitor_touch" then

Because the "event" variable returned by os.pullEvent is a string. However, you don't need to do that because you're using a filter. The bottom portion of the code could be this:

while true do
  event,side,X,Y = os.pullEvent("monitor_touch")
  monitor.clear()
  monitor.setCursorPos(1,9)
  monitor.write("X:"..X.." Y:"..Y)
end

I added in the monitor.clear(), so that it clears the previous text before writing anything new.