Deep Dives, Dots and Diagrams

Deep Dives, Dots and Diagrams

We’re going to try something a bit different this week; we’re going to do a bit of a deep dive on one of the systems I’ve been working on. It should be understandable for folks with no coding knowledge, so don’t be scared to read on! Normal blogging should resume next fortnight. I will give you a brief rundown on the last two weeks before we get into that though!

I’ve spent a lot of the last fortnight brushing up on some programming fundamentals. I was attempting to refresh myself on my entire games programming course in record time. It has been a wild ride. From the Dot Product to Path Prediction to Q-Learning, I’ve been stuffing as much information into my brain as I can.

You know what? It has been fantastic! I had forgotten the simple joy of learning things for the sake of learning. That endorphin rush when you go “OH!” and your world gets a little bit wider is addictive. I’ve loved every second.

So what of Strays? Well. I’ve been working on some less visual internal code I’m afraid. Namely the Event System. I’ve been building a flexible way of sending information to a bunch of places. Let’s take a closer look!

Deep Dive on the Event System:
There is a concept in programming called “clean code”. This amounts to writing code that is efficient, and easy to understand. It makes code easier to come back to, makes it easier for others to understand, and avoids unnecessary code bloating. It’s a huge topic that I cannot unpack in a single post! However, one of the concepts important to writing clean code is “Blackboxing”. This means making each section of your code a closed system, an isolated pocket that is independent of the rest of the codebase. For example, an Input System does not need to know about the Audio System.
There are a lot of practical reasons as to why this makes a lot of sense. The biggest one is that it avoids dependencies, and makes your code more flexible. It’s a lot easier to swap out an Input System if nothing else is relying on it being there! The problem then becomes, “Well what if I need to send another system a message?” That’s when things get complicated.

Lets take an example: When we are on the start screen of a game and press the A button to start a New Game, we need a few things to happen. We need the button to respond, to flash or give some visible cue that it has been pressed. We also would like it to make a sound, an audio cue. Most importantly, we want the game to start loading up the first level.

Ok, so let’s assume we want to do all of that from some “OnA” function. This might be a function on the button, or in the UI, or in some “Game Manager”. We’ll assume the latter for now. “OnA” will need to check the Input System every frame to see if you have pressed A. It will also need to know about the New Game button, so it can do the visual effect. Finally, it will need to know about the audio system so it can request it play a sound, and we will assume the loading happens in Game Manager. Let’s look at that:

This isn’t the worst example by any means, but you can already see, Game Manager needs to know about a lot of things, and we have arrows going in both directions. It could also be that the button controls the sound, so Buttons need to know about the Audio System and connect to the Game Manager, which might use the Audio System for background audio, and the input system detects volume changes so connects to the Audio System also and… you get the idea.

Instead, what if we had just one way of communicating between each of these systems? This is where an Event System comes in handy. The simplest method of using an event system is to have a list of Events, which could just be the text (or string, in programming) “A_Button_Pressed”, that other entities can ask to be told about. The Start Game Button, Game Manager, and Audio System will ask the Event System to tell them when the “A_Button_Pressed” event happens. Input System tells the Event System when that happens through a “Dispatch” function, then the Event System sends that message out. The Start Game Button receives that message, and flashes. Meanwhile the Game Manager and Audio System have also picked up that message, and start loading and play the correct sound respectively, all without knowledge of each other’s existence.

What’s more, thanks to a nifty coding concept called an “Interface” the Input System doesn’t even need to know that Game Manager, New Game Button and Audio System are those things; to the Input System they are just an “Event Receiver”. I won’t go into how exactly, but it is because the Event System doesn’t need to know that they are those types, it only needs to know that they can receive messages! Pretty nifty right?

Now the Input System doesn’t know about any system other than the Event System, the Event System only knows about Event Receivers, and nothing else is aware of anyone else’s existence! Our code is blackboxed, and we can still inform each system about tasks it needs to do!

So that’s basically what I’ve been working on. As you might imagine, my version is a little more complex than the above. For example, I want to package data with my event, so I need to account for data being sent around. But the principle is largely the same!

Anyway, that about does it for this post. I’m hoping to return to the more visual blogs I’ve been striving for of late soon. Until then,

Matt out.

29 thoughts on “Deep Dives, Dots and Diagrams

  1. I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article.

  2. Good – I should certainly pronounce, impressed with your web site. I had no trouble navigating through all the tabs and related information ended up being truly easy to do to access. I recently found what I hoped for before you know it at all. Reasonably unusual. Is likely to appreciate it for those who add forums or something, website theme . a tones way for your client to communicate. Excellent task..

  3. Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.

  4. Whats up very cool site!! Guy .. Beautiful .. Wonderful .. I’ll bookmark your blog and take the feeds alsoKI am glad to seek out so many helpful information right here within the submit, we need develop extra strategies in this regard, thanks for sharing. . . . . .

  5. Hello There. I found your weblog using msn. This is a really well written article. I will be sure to bookmark it and come back to learn more of your helpful information. Thank you for the post. I will definitely return.

  6. The other day, while I was at work, my cousin stole my iPad and tested to see if it can survive a forty foot drop, just so she can be a youtube sensation. My apple ipad is now broken and she has 83 views. I know this is totally off topic but I had to share it with someone!

  7. I like the helpful info you provide in your articles. I’ll bookmark your weblog and check again here regularly. I’m quite certain I will learn lots of new stuff right here! Best of luck for the next!

  8. Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me?

  9. I was very pleased to find this internet-site.I wished to thanks for your time for this glorious learn!! I undoubtedly having fun with each little little bit of it and I’ve you bookmarked to take a look at new stuff you blog post.

  10. Greetings from Florida! I’m bored to death at work so I decided to check out your blog on my iphone during lunch break. I really like the info you provide here and can’t wait to take a look when I get home. I’m amazed at how quick your blog loaded on my mobile .. I’m not even using WIFI, just 3G .. Anyhow, great blog!

  11. I was very pleased to find this web-site.I wanted to thanks for your time for this wonderful read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you blog post.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top