This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Non-stop os.pullEvent()
Started by AlkamlBan, 20 June 2014 - 05:04 PMPosted 20 June 2014 - 07: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.
Posted 20 June 2014 - 07:14 PM
You could use the parallel API.
E.g:
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.
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.
Posted 20 June 2014 - 07:22 PM
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:/>
Posted 20 June 2014 - 07:27 PM
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.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:/>
Posted 20 June 2014 - 07:36 PM
The parallel API is the wrong solution here. Check out the way the worm program is coded.
Posted 20 June 2014 - 07:50 PM
And the 'os.pullEventRaw()' function isn't the solution ?
Posted 20 June 2014 - 07:58 PM
Nope. os.pullEventRaw is the same as os.pullEvent but it doesn't handle termination.And the 'os.pullEventRaw()' function isn't the solution ?
Posted 20 June 2014 - 09:19 PM
Yes I see… The coroutines API remains possible solution.
Where is the worm game program ? I can see it more closely…
Where is the worm game program ? I can see it more closely…
Posted 20 June 2014 - 10:01 PM
It's in rom/programs/fun/worm.Yes I see… The coroutines API remains possible solution.
Where is the worm game program ? I can see it more closely…
Posted 20 June 2014 - 10:34 PM
Thank, I haven't look this :$
Edited on 20 June 2014 - 08:34 PM
Posted 20 June 2014 - 10: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:/> :)/>
Posted 21 June 2014 - 01: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.
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.
Posted 21 June 2014 - 02: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…
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…
Posted 21 June 2014 - 04: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 on 21 June 2014 - 02:22 AM