Jump to content




Test for a event without waiting


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

#1 Wilma456

  • Members
  • 187 posts
  • LocationGermany

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.

#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

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.

#3 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

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.

#4 Wilma456

  • Members
  • 187 posts
  • LocationGermany

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.

#5 apemanzilla

  • Members
  • 1,421 posts

Posted 04 November 2016 - 10:20 PM

View PostWilma456, on 04 November 2016 - 06:08 PM, said:

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


#6 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

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".

#7 jakejakey

  • Members
  • 98 posts

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)






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users