0

I'm trying to create a while true do loop, that reacts to clicks, using os.pullEvent, and also updates a monitor.

Problem being, it only updates the screen when I press one of the on screen buttons, and I've found out that's because pullEvent stops the script, until an event is fired.

Is it possible to make it so pullEvent doesn't stop me updating the monitor?

function getClick()
    event,side,x,y = os.pullEvent("monitor_touch")
    button.checkxy(x,y)
end

local tmp = 0;

while true do
    button.label(2, 2, "Test "..tmp)
    button.screen()
    tmp++
    getClick()
end
Unihedron
  • 10,902
  • 13
  • 62
  • 72
TMH
  • 6,096
  • 7
  • 51
  • 88

2 Answers2

4

You can easily use the parallel api to run both codes essentially at the same time. How it works is it runs them in sequence until it hits something that uses os.pullEvent and then swaps over and does the other side, and if both stop at something that does os.pullEvent then it keeps swapping between until one yields and continues from there.

local function getClick()
  local event,side,x,y = os.pullEvent("monitor_touch")
  buttoncheckxy(x,y)
end

local tmp = 0
local function makeButtons()
  while true do
    button.label(2,2,"Test "..tmp)
    button.screen()
    tmp++
    sleep(0)
  end
end
parallel.waitForAny(getClick,makeButtons)

Now if you notice, first thing, I've made your while loop into a function and added a sleep inside it, so that it yields and allows the program to swap. At the end you see parallel.waitForAny() which runs the two functions that are specified and when one of them finishes, which in this case whenever you click on a button, then it ends. Notice however inside the arguments that I'm not calling the functions, I'm just passing them.

Colonel Thirty Two
  • 23,953
  • 8
  • 45
  • 85
Flamanis
  • 156
  • 5
  • I'll give this a try when I get, thanks :). I was looking at coroutine but I couldn't figure it out at all. – TMH Oct 31 '14 at 10:00
0

I don't have computercraft handy right now or look up the functions but i know that you can use the function os.startTimer(t) that will cause an event in t seconds (I think it is seconds)

usage:

update_rate = 1

local _timer = os.startTimer(update_rate)

while true do
    local event = os.pullEvent()

    if event == _timer then
        --updte_screen()
        _timer = os.startTimer(update_rate)
    elseif event == --some oter events you want to take action for
        --action()
    end
end

note: the code is not tested and I didn't use computercraft in quite a while so pleas correct me if i did a mistake.

CHlM3RA
  • 118
  • 6