Jump to content




Non-stop os.pullEvent()


13 replies to this topic

#1 AlkamlBan

  • Members
  • 64 posts
  • LocationGreece

Posted 20 June 2014 - 06:04 PM

Hi again! I have come again to ask a question that really bugs me. Is there A piece of code or a command that will make os.pullEvent/Raw not "freeze" the screen. I mean imagine mario, you need to click the keys for him to move but if you don't that doesn't stop the monsters from moving. But as you all know os.pullEvent halts everything until it gets something so I would be really intrested in reading any suggestions or helpful comments.

#2 Lignum

  • Members
  • 558 posts

Posted 20 June 2014 - 06:14 PM

You could use the parallel API.
E.g:
local function pullSomeEvents()
      while true do
            print(({os.pullEvent("char")})[2])
      end
end

local function printALotOfThings()
      while true do
            sleep(0)
            print("amessage")
      end
en

parallel.waitForAll(pullSomeEvents, printALotOfThings)

While this will do what you said, it's always a better idea to try to find a workaround to your problem. Keep in mind that you can pull all events by not passing a filter to os.pullEvent.

#3 AlkamlBan

  • Members
  • 64 posts
  • LocationGreece

Posted 20 June 2014 - 06:22 PM

View PostLignum, on 20 June 2014 - 06:14 PM, said:

While this will do what you said, it's always a better idea to try to find a workaround to your problem. Keep in mind that you can pull all events by not passing a filter to os.pullEvent.

I know and I have tried doing it but the point is that it "freezes" and thats what I don't like about it. It NEEDS some input before it goes on so... :unsure:

#4 Lignum

  • Members
  • 558 posts

Posted 20 June 2014 - 06:27 PM

View PostAlkamlBan, on 20 June 2014 - 06:22 PM, said:

I know and I have tried doing it but the point is that it "freezes" and thats what I don't like about it. It NEEDS some input before it goes on so... :unsure:
What exactly are you trying to do? If you needed to receive input and, say, run a piece of code every 0.5 seconds, you could make a timer.

#5 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 20 June 2014 - 06:36 PM

The parallel API is the wrong solution here. Check out the way the worm program is coded.

#6 JackCarterSmith

  • Members
  • 68 posts
  • LocationTrain factory

Posted 20 June 2014 - 06:50 PM

And the 'os.pullEventRaw()' function isn't the solution ?

#7 Lignum

  • Members
  • 558 posts

Posted 20 June 2014 - 06:58 PM

View PostJackCarterSmith, on 20 June 2014 - 06:50 PM, said:

And the 'os.pullEventRaw()' function isn't the solution ?
Nope. os.pullEventRaw is the same as os.pullEvent but it doesn't handle termination.

#8 JackCarterSmith

  • Members
  • 68 posts
  • LocationTrain factory

Posted 20 June 2014 - 08:19 PM

Yes I see... The coroutines API remains possible solution.
Where is the worm game program ? I can see it more closely...

#9 Lignum

  • Members
  • 558 posts

Posted 20 June 2014 - 09:01 PM

View PostJackCarterSmith, on 20 June 2014 - 08:19 PM, said:

Yes I see... The coroutines API remains possible solution.
Where is the worm game program ? I can see it more closely...
It's in rom/programs/fun/worm.

#10 JackCarterSmith

  • Members
  • 68 posts
  • LocationTrain factory

Posted 20 June 2014 - 09:34 PM

Thank, I haven't look this :$

Edited by JackCarterSmith, 20 June 2014 - 09:34 PM.


#11 AlkamlBan

  • Members
  • 64 posts
  • LocationGreece

Posted 20 June 2014 - 09:42 PM

Hello again, :D and MAN the help was fast ;) . The os.startTimer() is probably what I am looking for but I just can't understand how it works :( . So if someone could explain it would be really helpful. Thnx again for the help guys :rolleyes: :)

#12 gr8pefish

  • Members
  • 7 posts

Posted 21 June 2014 - 12:39 AM

I recently had the same issue as you.

os.startTimer(x) will trigger a "timer" event after x seconds have gone by. Use os.pullEvent() and check for a timer event to move on.

#13 Bomb Bloke

    Hobbyist Coder

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

Posted 21 June 2014 - 01:52 AM

The trick is not to put a filter on the os.pullEvent() call. In this way, it'll still block the script when it runs, but the very next event will start things moving again - you use os.startTimer() to ensure another event comes quickly.

Every time you get an event, you then check to see what sort of event it is. If it's a "key" event, go ahead and move your character. If it's a timer event, go ahead and move whatever else, then set another timer...

#14 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 21 June 2014 - 03:21 AM

and as can be seen on the os.startTimer wiki page it will return an ID this means if you need multiple timers you can do that too.

local timer1 = os.startTimer(1) --# trigger after 1 second
local timer2 = os.startTimer(1.4) --# trigger after 1.4 seconds

while true do
  local event = { os.pullEvent() }
  if event[1] == "timer" then
	if event[2] == timer1 then
	  print("timer1 finished")
	  timer1 = os.startTimer(1) --# restart
	elseif event[2] == timer2 then
	  print("timer2 finished")
	  timer2 = os.startTimer(1.4) --# restart
	else
	  print("unknown timer finished, we didn't start this one, maybe I'm running in a multishell tab?")
	end
  end
end

Edited by theoriginalbit, 21 June 2014 - 03:22 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users