Jump to content




[Error] attempt to compare __le on nil and number


  • You cannot reply to this topic
6 replies to this topic

#1 denni199

  • Members
  • 14 posts
  • LocationDenmark

Posted 07 February 2013 - 12:22 AM

Hi.
As the title suggest i have an error called:
attempt to compare __le on nil and number

Heres the full code of the program:
-- Clears everything and wraps the peripheral
term.clear()
term.setCursorPos(1, 1)
m = peripheral.wrap("right")
m.clear()
m.setCursorPos(2, 2)
m.setTextColor(colors.white)
m.setBackgroundColor(colors.lime)

-- Writes the button
m.write("			")
m.setCursorPos(2, 3)
m.write("  Activate  ")
m.setCursorPos(2, 4)
m.write("			")
m.setBackgroundColor(colors.black)
m.setCursorPos(1, 1)

-- Checks if clicked within specified x and y
while true do
  event, button, x, y = os.pullEvent(monitor_touch)

  if x <= 12 and x >= 2 and y <= 4 and y >= 2 then
	rs.setOutput("back", true)
	sleep(1)
	rs.setOutput("back", false)
  end
end

Basically once i pressed the button i get the error.
It won't loop through it again and check if the button is pressed again.

Thanks.

#2 Willibilly19

  • Members
  • 48 posts
  • LocationColorado, USA

Posted 07 February 2013 - 12:35 AM

I don't know exactly what caused it, but adding in an additional sleep fixed it:P

while true do
  event, button, x, y = os.pullEvent(monitor_touch)

  if x <= 12 and x >= 2 and y <= 4 and y >= 2 then
        rs.setOutput("back", true)
        sleep(1)
        rs.setOutput("back", false)
  end
  sleep(0.5) --This sleep here
end

Just add that one in:P I'm sure someone would be able to explain the issue or something... But it's working xD

#3 denni199

  • Members
  • 14 posts
  • LocationDenmark

Posted 07 February 2013 - 12:37 AM

 Willibilly19, on 07 February 2013 - 12:35 AM, said:

I don't know exactly what caused it, but adding in an additional sleep fixed it:P

while true do
  event, button, x, y = os.pullEvent(monitor_touch)

  if x <= 12 and x >= 2 and y <= 4 and y >= 2 then
		rs.setOutput("back", true)
		sleep(1)
		rs.setOutput("back", false)
  end
  sleep(0.5) --This sleep here
end

Just add that one in:P I'm sure someone would be able to explain the issue or something... But it's working xD
Oh.
Your right!
Thanks :)

Now if anyone would care to explain why this works, i would be even happier :)

#4 1lann

  • Members
  • 516 posts
  • LocationSeattle

Posted 07 February 2013 - 01:12 AM

 denni199, on 07 February 2013 - 12:37 AM, said:

 Willibilly19, on 07 February 2013 - 12:35 AM, said:

I don't know exactly what caused it, but adding in an additional sleep fixed it:P

while true do
  event, button, x, y = os.pullEvent(monitor_touch)

  if x <= 12 and x >= 2 and y <= 4 and y >= 2 then
		rs.setOutput("back", true)
		sleep(1)
		rs.setOutput("back", false)
  end
  sleep(0.5) --This sleep here
end

Just add that one in:P I'm sure someone would be able to explain the issue or something... But it's working xD
Oh.
Your right!
Thanks :)

Now if anyone would care to explain why this works, i would be even happier :)
That actually technically didn't fix the true problem. The reason why adding a sleep worked is because when you do os.pullEvent(monitor_touch), you forgot to add quotes, so lua thinks that's nil. It being nil means it will wait for "any" event to occur. So an event occured but it didn't supply the parameters x and y, so the comparison failed, trying to compare nil with a number

Adding a sleep fixed it because sleep itself calls an event, and I think that event has enough parameters to fill x and y.

So really, your problem isnt solved, you might have noticed that if you triggered another event like keys or just a standard touchscreen event it may stuff up/not work as you expected. So to fix this just add quotes to monitor_touch: os.pullEvent("monitor_touch")

#5 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 07 February 2013 - 01:51 AM

 1lann, on 07 February 2013 - 01:12 AM, said:

Adding a sleep fixed it because sleep itself calls an event, and I think that event has enough parameters to fill x and y.

If I'm not mistaken, the event it uses is os.startTimer() and queues for the timer - which doesn't supply x and y?

#6 1lann

  • Members
  • 516 posts
  • LocationSeattle

Posted 07 February 2013 - 02:47 AM

 remiX, on 07 February 2013 - 01:51 AM, said:

 1lann, on 07 February 2013 - 01:12 AM, said:

Adding a sleep fixed it because sleep itself calls an event, and I think that event has enough parameters to fill x and y.

If I'm not mistaken, the event it uses is os.startTimer() and queues for the timer - which doesn't supply x and y?
Hmm yeah, I'm not so sure what's really happening. I suppose you'll need to use a event logger to see :P

#7 ChunLing

  • Members
  • 2,027 posts

Posted 08 February 2013 - 10:37 AM

The sleep function eats all events up to and including the timer it set.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users