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

What's wrong with my code!?

Started by Floedekage, 24 October 2012 - 08:44 AM
Floedekage #1
Posted 24 October 2012 - 10: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?
robhol #2
Posted 24 October 2012 - 10: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.
Floedekage #3
Posted 24 October 2012 - 10: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"
ChunLing #4
Posted 24 October 2012 - 11:16 AM
Try print() or write() instead of term.write() and see what happens.
Floedekage #5
Posted 24 October 2012 - 11:31 AM
Nope, that doesn't work either. I really have no idea what is causing this… :/
Floedekage #6
Posted 24 October 2012 - 11:41 AM
Is there another way to use os.pullEvent and both have a filter for mouse and char?
Floedekage #7
Posted 24 October 2012 - 11: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.
ChunLing #8
Posted 24 October 2012 - 11: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.
Kolpa #9
Posted 24 October 2012 - 01:28 PM
to prevent too long without yielding errors put

sleep(0)
ChunLing #10
Posted 24 October 2012 - 08: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.
Lyqyd #11
Posted 24 October 2012 - 10:48 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.

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.
ChunLing #12
Posted 25 October 2012 - 12:59 AM
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.
Lyqyd #13
Posted 25 October 2012 - 04:03 AM
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.