Design a site like this with
Get started

Day 63: Delegates & Events

I’m nearing the end of the Tower Defense tutorial and at this point I feel like explanations have become sparse and I’m only dutifully typing out the code as it’s shown.

To be fair, it was never presented as a beginner’s tutorial so it’s up to me to look into the concepts I’m coding.

The last video was on delegates and events and I need some better understanding of why to use it and how it’s different from just calling a regular method. I’m planning on watching some tutorials specifically on events and delegates starting with:

Current Progress Status

  • Tower Defense Tutorial: 60/73
  • Clean Code by Robert C. Martin (pg 348/464)



Author: Mike@PAUSED

Aspiring indie game developer. Devlog:

3 thoughts on “Day 63: Delegates & Events”

  1. Ah delegates and events, always so much fun. These are important topics for maintaining a separation of concerns with your classes. This is a theory example, hopefully it helps you understand the why of it.

    First example (the bad one). Say you have a pressure plate that you want to activate a gate and a switch elsewhere in the dungeon. You write methods in the gate and switch classes that the pressure plate calls directly. However, now the pressure plate knows too much about the gate and switch class, and more importantly the code to handle the gate and switch class is now spread among multiple classes. Not good. This eventually leads to weird and hard to find bugs as well as spaghetti code (

    A common mistake under this example is when you need to pass a variable to both the gate and switch but the value needs to be modified differently for the gate than the switch. Well the code is in the pressure plate class so why not modify the value there. And bam you have code that affects the operation of the gate and switch but it’s not in the those classes. So 3 months later you need to tweak that value but you can’t find it because you forgot where you put it and spend hours and hours trying to figure it out.

    Second example (the good one). Instead, the pressure plate has a few events, a) OnPressingDown, b) OnHeldDown, c) OnReleasingUp which happen when the a) a weight triggers the pressure plate to move down and engage, b) the weight is holding the pressure plate in the down position, and c) the weight has been removed and the pressure plate is resetting to the unpressed position.

    Now in the gate and switch classes you subscribe to the OnHeldDown event of the pressure plate. The same code you would write in the bad example can be used here, but now the pressure plate code doesn’t care who is listening to its events.

    Now you still pass the variable to the event but it no longer makes sense to modify the variable before sending it. Visually speaking there is no other gate or switch code in the pressure plate class so it “makes sense” to pass the variable to the respective classes and let them deal with it. This keeps the concerns of each class separated.

    Liked by 1 person

    1. Have you considered writing more of these great real-life
      coding examples to explain some of the more difficult concepts in a tutorial series?

      Another excellent explanation, thank you!

      Liked by 1 person

      1. Thanks. I’ve been an instructor in some of my jobs and I’ve gotten similar feedback. I have lots of knowledge about game dev, however, I have little experience with hands on game dev. I’d love to provide a tutorial series with analogies and code examples. I just need more code examples.

        I know for myself that I can grasp the analogy easily enough but until I get the code written I don’t really get it. I imagine a lot of people are that way (not sure if that’s really true it’s just my opinion).

        Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: