Event Queue

Has anyone built a game engine (2d) around an event queue? It looks like an interesting method to design your game engine. I don’t professionally develop but more or less play with Monogame. One thing I constantly run into is it turns into a mess of code the more I progress.


https://gameprogrammingpatterns.com/event-queue.html

So out of curiosity, what patterns do you use to ensure your code doesn’t turn into a debugging nightmare?

It seems arbitrary, as it doesn’t seem to make things more efficient or simple. I guess it would be a fun experiment, but I’m sure it has its place. :confused:

My key to not letting my programs die a tangled mess is to obsessively organize things. Prioritizing “things making sense” over “things working just fine”. Where possible giving each class one straightforward purpose. I think the concept of an event driven game world is more appealing than anything in practice.

Yes. It works very well.

Let’s assume that you are already applying SOLID, TDD, CI, clean architecture, and you create cohesive software at a class and module level.

What do events get you?

They enable you to decouple the interactions between all the different systems in a game engine: sound, animation, character entities, etc.

They also enable you to send future messages. This is tremendously powerful.

Originally, object oriented programming was inspired by biological cells. OO entailed creating objects that sent messages to each other. Events are a loosely coupled way of enabling this. I apply it at the module level between bigger systems, not between everything. So I define clear boundaries, and boundaries are crossed with events. They let you abstract the sender and receiver from the message. So the idea of a future event can be reused across any message between any two objects. AI and players can “use” the system the same way. Display and business logic are completely separated. It’s easy to connect customized events, like a sound on a certain animation.

You will get control flow that is more complex. However, the structure of the tighter coupling required in lieu of events is just as complex. Events enable a simpler structure but make control flow more difficult. However, I ensure that event processing is serialized, predictable, and deterministic. I also ensure that event processing only happens at one point per loop.

It works very well. Happy to answer more specific questions.

Sorry for the double post. I wanted to add one clarification.

If you find your code is tangled, events will not help with this.

Go learn what single responsibility principle and the dependency inversion principle are. Start practicing test driven development. You cannot write tightly coupled code if you TDD. Use it as a design pressure.

As noted above, try to make each function and each class do only one logical thing. Events are much more advanced.

No need to be sorry; I appreciate your clarification!

I’m more or less brain storming for a project I do want to work on (2d). I’ll take a look at the principles you mentioned. If you think of anything else let me know :slight_smile:

Right now 99% of my coding is ASP.NET Core. I’m sick of web development and databases.