Jump to content




What's wrong with my code!?


12 replies to this topic

#1 Floedekage

  • Members
  • 11 posts

Posted 24 October 2012 - 09:44 AM

SOLVED, just delete this... ^_^/>

I'm trying to make a simple notepad-like program. That means that it would have to understand input from the keyboard and the mouse, but the code below doesn't work for me.

while true do
  local event, p1, p2, p3 = os.pullEvent()
  if event == "char" then
	term.write(p1)
  elseif event == "mouse_click" and p1 == 1 and p2 <= 3 and p3 <= 3 then
	break
  end
  sleep(0.01)
end


What's confusing me is that the mouse_click works fine, but the char doesn't.
Doe anyone have any idea how to make this run?

#2 robhol

  • Members
  • 182 posts

Posted 24 October 2012 - 09:51 AM

Well, you're not breaking anything if it's a char. It's a bit hard to tell if this is intentional, you might want to post more code.

#3 Floedekage

  • Members
  • 11 posts

Posted 24 October 2012 - 09:58 AM

What I wanted to do was, when a user inputs a char it would be written to the screen, and when a user clicks the top left corner it would stop the program. But when a key is pressed, nothing happens.
This also doesn't work if instead of the event = "char" there is a event = "key"

#4 ChunLing

  • Members
  • 2,027 posts

Posted 24 October 2012 - 10:16 AM

Try print() or write() instead of term.write() and see what happens.

#5 Floedekage

  • Members
  • 11 posts

Posted 24 October 2012 - 10:31 AM

Nope, that doesn't work either. I really have no idea what is causing this... :/

#6 Floedekage

  • Members
  • 11 posts

Posted 24 October 2012 - 10:41 AM

Is there another way to use os.pullEvent and both have a filter for mouse and char?

#7 Floedekage

  • Members
  • 11 posts

Posted 24 October 2012 - 10:54 AM

Okay, I've figured it out, the problem was somehow the sleep(0.01), I thought I would need to put that there to avoid the "too long without yielding" error. But it seems to work now.

#8 ChunLing

  • Members
  • 2,027 posts

Posted 24 October 2012 - 10:55 AM

Sure, use a table with keys of the events you want to pull and values of what you want to do in the case of that event. Not much of a savings to be had in this case, though.

Oh, I just realized your problem. You always, always get a key event right before a char event, sleep keeps you from getting the char event cause it's right after the key event. And you realized it too.

#9 Kolpa

  • New Members
  • 260 posts
  • LocationGermany

Posted 24 October 2012 - 12:28 PM

to prevent too long without yielding errors put
sleep(0)


#10 ChunLing

  • Members
  • 2,027 posts

Posted 24 October 2012 - 07:28 PM

A loop that uses "os.pullEvent" won't go too long without yielding anyway, it always pauses execution to await the events. Pretty much any function that pulls an event somehow or other won't go too long without yielding, unless the loop also generates instant events to be pulled.

#11 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,454 posts

Posted 24 October 2012 - 09:48 PM

View PostChunLing, on 24 October 2012 - 07:28 PM, said:

A loop that uses "os.pullEvent" won't go too long without yielding anyway, it always pauses execution to await the events. Pretty much any function that pulls an event somehow or other won't go too long without yielding, unless the loop also generates instant events to be pulled.

No, it doesn't matter how long it yields for, it only has to yield. Any loop that doesn't go more than ten seconds without yielding will be fine, no matter how short the time that it wants to yield is.

#12 ChunLing

  • Members
  • 2,027 posts

Posted 24 October 2012 - 11:59 PM

Oh, so it yields whenever checking for an event to pull, even if it generated the event instantly earlier in it's own loop? That's good to know.

#13 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,454 posts

Posted 25 October 2012 - 03:03 AM

View PostChunLing, on 24 October 2012 - 11:59 PM, said:

Oh, so it yields whenever checking for an event to pull, even if it generated the event instantly earlier in it's own loop? That's good to know.

Yes, it has to yield in order to be resumed with an event.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users