Please disable your adblock and script blockers to view this page

Event Queue (Game Programming Patterns)



Henry Hatsworth

No matching tags

Game Loop
the Los Angeles

Grand Central Station

Data Locality

No matching tags

Positivity     45.00%   
   Negativity   55.00%
The New York Times
Write a review: Hacker News

“somewhere” is a queue.When user input comes in, the OS adds it to a queue of unprocessed events. you call getNextEvent(), that pulls the oldest event off the queue and hands it to your application.Most games aren’t event-driven like this, but it is common for a game to have its own event queue as the backbone of its nervous decoupled.If you want to know why they aren’t event-driven, crack open the Game Loop chapter.Say your game has a tutorial system to display help Instead, you could have a central event queue. so the combat code can add an “enemy died” event every time you slay a foe.Likewise, any game system can receive events from the queue. Event queues don’t have to be for processing it.A queue stores a series of notifications or requests in first-in, The request processor then processes items from the queue at a later time. need a queue when you want to decouple something in time.I mention this in nearly every chapter, but it’s worth emphasizing. queue provides that simpler decoupling patterns don’t.Queues give control to the code that pulls from it — the receiver can delay But queues do this by This makes queues a poor fit when the sender needs a response.Unlike some more modest patterns in this book, event queues are complex and tend entails.Say some AI code posts an “entity died” event to a queue when a virtual minion That event hangs out in the queue for who knows let’s get precise on some terms:The head of the queue is where requests are read from. the queue. It means that the queue will be empty if the head and tail are the same index.Now we’ve got a queue — we can add to the end and remove from the front. As we run requests through the queue, the We need to ensure the queue doesn’t When the queue requests that we can aggregate is only as big as the queue. the queue gets full, we’ll find more things to collapse.This pattern insulates the requester from knowing when the request gets of the game onto its own thread — audio, rendering, AI, etc.Straight-line code only runs on a single core at a time. really helps.We’re in good shape to do that now that we have three critical pieces:The code for requesting a sound is decoupled from the code that plays it.We have a queue for marshalling between the two.The queue is encapsulated from the rest of the program.All that’s left is to make the methods that modify the queue — playSound() burn CPU cycles until there’s a request to process.Many games use event queues as a key part of their communication structure, and event, sort of like an asynchronous Observer pattern.You are likely to allow multiple listeners. Event queues are often You can think of this as an asynchronous API to a service.Another word for “request” is “command”, as in the Command pattern, and queues can be used there too.You are more likely to have a single sender knows is that it sent a message.The queue is more encapsulated. listener (something more like a work queue).In either case, the listeners may end up doing redundant work or With a single listener, that complexity disappears.A broadcast queue:This is how most “event” systems work. region of the UI.A work queue:Like a broadcast queue, here you have multiple listeners too. one-sender-one-receiver queue, but that starts to feel less like the communication system this pattern is about and more like a vanilla queue data method, any part of the codebase can add a request to the queue. careful, that may trigger a feedback loop.You’ll likely want some reference to the sender in the event itself. With a queue, the queue. the asynchronous cousin to the well-known Observer pattern.Like many patterns, event queues go by a number of aliases. Where our event queues are within an application, message want it to respond to those asynchronously, it makes sense to queue them.When you have a bunch of state machines sending messages to each other, each is essentially an event or message queue.

As said here by