Jump to content




os.pullEvent(): another way how to not lose events while waiting for another event


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

#1 timikx

  • New Members
  • 2 posts

Posted 19 June 2013 - 02:55 AM

Hi I have question about events:
I've made my version of programs for BdoubleO's and Etho's call of dutty map. I have main computer which is checking various events and then working with them. I'm using wireless modem, player detector, chatbox, touch screen and inputs from bundled cable.

Before I played just with turtles so this was my first time working with monitors and events.
It was kind of like learning experiment for me, code could be cleaned surely but the program is working so I am happy.

So before I discovered that you can increase number of messages from chat box per second in miscperipherals config file, my program was terminated because sometimes it send more than 1 chat box message per second.

From my understanding of events:
they are stored in event queue and can be pulled with os.pullEvent() but when you use sleep(), read() or some other commands (I don't know them all) the queue will be cleared.

So in order to avoid termination of my program I need to somehow place delay after chatbox.say() command but also don't destroy events that happens during the delay.
So I made wait() function that starts another timer, and during the timer time checks events (os.pullEvent) and then filters only events I care about and store them in table. After time passes all events in table are placed to event queue (os.queueEvent).

I was just wandering is there some function or trick how to do this differently or easily.

I think I don't need to use my wait() function any more because I increased the number of messages from chat box to 25 per second but i kept it just for safety.

Thanks for answers.

wait() function:
Spoiler

demostration is in this video (it has bad frame rate - I know)


Programs and world download:
http://www.mediafire...bkhg71efleuh3ec


All programs:

Main:
Spoiler

Detecotor
Spoiler

Score
Spoiler

Players1
Spoiler

Players2
Spoiler

Edited by Lyqyd, 19 June 2013 - 12:28 PM.
added code tags


#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 19 June 2013 - 12:21 PM

Split into new topic.

Essentially, you should declare a couple of new variables at the top: canChat and chatLines. canChat will be a boolean that is true when we can send out a chat message immediately and false if we are waiting for a timer. Then, any time you would send out a chat message, you check canChat. If it's true, you send out the message, set canChat to false, and start a timer. If canChat is still false when the next message would be sent, you instead put the message into the chatLines table with table.insert. In your main loop, you add a handler for timer events (this will handle the timer that we start whenever we send out a chat message). Whenever a timer event comes in, it looks at the chatLines table. If there's anything in it (#chatLines > 0), it uses table.remove(1) to remove the first entry from the table (table.remove returns the value of the entry it removed) and sends it out as a chat message. It leaves canChat false and starts another timer. If the chatLines table was empty when the timer event was received, we simply set canChat to true again.

This is going to be a much more robust solution than re-queuing events. One should never re-queue events that have already come in, as other coroutines will not know that the event is a duplicate and will treat it as a new event.

#3 timikx

  • New Members
  • 2 posts

Posted 19 June 2013 - 12:47 PM

Thank you,
I didn't think about it like that. I will try to do it your way.
Thanks again





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users