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

Test for a event without waiting

Started by Wilma456, 03 November 2016 - 05:27 PM
Wilma456 #1
Posted 03 November 2016 - 06:27 PM
Is there a possiblity, to test for a event without waiting? os.pullEvent waits for a event and then the code continuing, but I want to ask for a event without stoping the code. If there no event at this time, it could be return nil or something else.
KingofGamesYami #2
Posted 03 November 2016 - 06:40 PM
Queue a dummy event first. Though I'm not sure why you would need to do this; remember that only one CC computer executes at a time.
Lyqyd #3
Posted 03 November 2016 - 06:47 PM
Why do you think you need to do this? There's probably a better way to accomplish what you're ultimately trying to do.
Wilma456 #4
Posted 04 November 2016 - 06:08 PM
I need this for a game engine, who every object has his own script. The game should not stop while one object wait for input.
apemanzilla #5
Posted 04 November 2016 - 10:20 PM
I need this for a game engine, who every object has his own script. The game should not stop while one object wait for input.

Using coroutines/the parallel API would be a much better choice here, but if you really want to do it this way you can use code like this:

os.queueEvent("dummy")
local e = {os.pullEvent()}
while e[1] ~= "dummy" do
    -- process events
    -- eg e[1] might be "char", e[2] might be "b", etc

    e = {os.pullEvent()}
end
Bomb Bloke #6
Posted 05 November 2016 - 12:46 AM
It sounds like os.startTimer() is much more suitable than os.queueEvent(), in this case. Ultimately, no game needs a frame rate of "unlimited".
jakejakey #7
Posted 13 November 2016 - 02:11 PM
You could use parallel or coroutine, for instance

function waitForKey()
  e,k = os.pullEvent("key")
  if k == 42 then
    print("Enter!")
  else
    print("Not enter!")
  end
end
function runCode()
  while true do
    term.setTextColor(colors.lime)
    print(math.random(1,10000000))
    term.setTextColor(colors.white)
  end
end
parallel.waitForAny(runCode,waitForKey)