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

rapidly calling os.pullEvent()

Started by unobtanium, 15 January 2014 - 11:21 AM
unobtanium #1
Posted 15 January 2014 - 12:21 PM
Hello,

this might be an odd question, but how many times can os.pullEvent() can be called?
20 times in a second, based on the minecraft game ticks?

Basically, i am having an os.startTimer(0.1) running all the time, catching and freshing it with os.pullEvent every time.


local delay = os.startTimer(0.1)
while true do
local e = { os.pullEvent() }
if e[1] == "timer" and e[2] == delay then
delay = os.startTimer(0.1)
end
sleep(0)
end

That works!
Furthermore, i am receiving rednet messages (up to 4 per second in the final program i think) and catch them with the same os.pullEvent()


local delay = os.startTimer(0.1)
while true do
local e = { os.pullEvent() }
if e[1] == "timer" and e[2] == delay then
delay = os.startTimer(0.1)
elseif e[1] == "rednet_message" then
-- checking if the message is correct, saving information in an array as well as saving it into a file if the chunk gets unloaded
end
sleep(0)
end

It allmost every time stops my timer now when i am receiving a rednet message.
If there can only be every 0.05 seconds an os.pullEvent() activated then i am only having 20 actions per seconds to work with.
If this is true a solution would be pulling the timer every 0.05 seconds and refreshing the timer when a rednet message gets received as well. This would take a while to implement so i ask first here.

Let me know if someone knows more about it.
Thanks
unobtanium
Edited on 15 January 2014 - 01:10 PM
Lyqyd #2
Posted 15 January 2014 - 12:25 PM
Don't sleep in event handling loops. You're probably throwing away your timer event there. I'm surprised the first code even works. Also, what could you possibly be doing that would require resuming every tenth of a second?
unobtanium #3
Posted 15 January 2014 - 12:40 PM
Okay thanks i am going to try that out.
I am basically switching the redstone output on various sides to change the direction of iron pipes. And redstone cant be changed every 0.05 seconds, based on the 10 ticks for redstone.

edit: sleep(0) really was the problem.
Edited on 15 January 2014 - 11:46 AM
CometWolf #4
Posted 15 January 2014 - 02:06 PM
Just a quick note e[2] == "rednet_message" won't work. It should be e[1]
unobtanium #5
Posted 15 January 2014 - 02:10 PM
Yeah that's right :D/>
Wrote the example above really quickly… derp derp :P/>
amtra5 #6
Posted 15 January 2014 - 08:11 PM
Redstone can be changed 20 times a second.

Check out Gundershot's YT channel for a tutorial.
oeed #7
Posted 15 January 2014 - 09:06 PM
The limiting factor is how often you can create an event. I did a quick test using the code below, I got 256 os.pullEvents in about a tenth of a second. Now, this code obviously has rather large issues, so don't use it in an actual program, but you get the idea.

print('...')
for i = 1, 257 do
os.startTimer(0.1)
end
os.pullEvent()
print('Starting')
local startTime = os.clock()
for i = 1, 256 do
os.pullEvent()
end
print('256 pullEvents in '..os.clock()-startTime..' seconds')
Printed '256 pullEvents in 0.1 seconds'

Anything higher doesn't seem to work. So, unless someone can prove me wrong, asume 256.
Edited on 15 January 2014 - 08:44 PM
theoriginalbit #8
Posted 15 January 2014 - 09:10 PM
That was in an emulator, so it might be a bit different to in game.
Its definitely different in-game. for these types of tests never do them in an emulator!
oeed #9
Posted 15 January 2014 - 09:44 PM
That was in an emulator, so it might be a bit different to in game.
Its definitely different in-game. for these types of tests never do them in an emulator!
Yes, you're quite correct. I tried it again in game and I've edit my post with the correct results.
unobtanium #10
Posted 16 January 2014 - 06:49 AM
Thanks a lot for that research.
So there is a slight chance that multiple events can happen at the same time and just the first one gets pulled? (just a theoretic question!!)

In the Minecraft Wiki 20 ticks per second are called Gametick and allmost everything works with it (mob spawning ect). Redstone ticks are just every second gametick so in the end only 10, which are 0.1 seconds :S
Edited on 16 January 2014 - 05:50 AM
theoriginalbit #11
Posted 16 January 2014 - 07:41 AM
So there is a slight chance that multiple events can happen at the same time and just the first one gets pulled? (just a theoretic question!!)
No. it is an event queue. each time you call one of three functions to dequeue an event (os.pullEvent, os.pullEventRaw, coroutine.yield) you'll get one event and the rest are maintained in the queue ready for the next time you pull an event…
unobtanium #12
Posted 16 January 2014 - 09:36 PM
Oh yeah that makes sense. Honor that guy who invented that xD