2
Non-stop os.pullEvent()
Started by AlkamlBan, Jun 20 2014 06:04 PM
13 replies to this topic
#1
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
Posted 20 June 2014 - 06: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.
#3
Posted 20 June 2014 - 06:22 PM
Lignum, 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...
#4
Posted 20 June 2014 - 06:27 PM
AlkamlBan, 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...
#5
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
Posted 20 June 2014 - 06:50 PM
And the 'os.pullEventRaw()' function isn't the solution ?
#8
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...
Where is the worm game program ? I can see it more closely...
#10
Posted 20 June 2014 - 09:34 PM
Thank, I haven't look this :$
Edited by JackCarterSmith, 20 June 2014 - 09:34 PM.
#11
Posted 20 June 2014 - 09:42 PM
Hello again, 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
#12
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.
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
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...
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
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