See Your Project To A Cleaner Setup With The OBSERVER PATTERN

The Observer Pattern is probably one that confuses people the most, but is not only rather simple - it feels the most gratifying to put into a project and have your components run off of events.
In video games the observer pattern typically breaks down into 2 parts - a broadcast is sent, and a bunch of "observers" or "listeners" are waiting to be notified of that broadcast, and then do something.
This is primarily driven with events and delegates, and the big bonus here is your components do not need to know about each other, they only need to know about the events that the observers can subscribe to.
This is useful for anything that happens in your games, unity or otherwise. Pickup events, damage events, game over events, enemy killed events, trigger events, etc etc etc. Games are driven by events already, the observer pattern just structures your project to give you that full separation.
✨Want to support the channel?
Buy me a coffee ☕: ko-fi.com/bmoli
COMMENT down below how you use the Observer Pattern, or if you're having trouble understanding it.
➤SUBSCRIBE to learn more about other design patterns as I cover them next!
➤Join our discord, I command you: / discord
Thanks for watching!
#bmo #gamedev #programming

Пікірлер: 33

  • @BigScrumbo
    @BigScrumbo Жыл бұрын

    Was having trouble with the instructions from my professor, the book, and other tutorials. I come here as a last resort and I finished my project in 15 minutes. I owe you a firm handshake and a soft drink of choice.

  • @T3QN1Q
    @T3QN1Q2 жыл бұрын

    I love observing the pattern of how many videos you have been making *snuggles*

  • @kitsunderedesu
    @kitsunderedesu Жыл бұрын

    This is probably the easiest to follow video I’ve seen yet on uncoupling dependencies with a concrete example, kudos.

  • @mandamoon9149
    @mandamoon91492 жыл бұрын

    Listen, broadcast, execute! Love this, keep ‘em coming Bmo!

  • @RaonCreate
    @RaonCreate2 жыл бұрын

    Keep going with those type of vids I love them!

  • @aboo2423
    @aboo24232 жыл бұрын

    Its been long since i got away from unity but i can't stop watching your video and get the vibe to go back

  • @headed_weast
    @headed_weast2 жыл бұрын

    I would like you to picture me in my best DJ Khalid voice saying “anotha one”

  • @regys9521
    @regys95212 жыл бұрын

    This channel really make things easy to understand, thank you so much

  • @iwoMalki
    @iwoMalki2 жыл бұрын

    Amazing content great voice and really friendly persona of yours there. I hope you will continue making great videos like all the ones you have made so far for a long long time.

  • @BMoDev

    @BMoDev

    2 жыл бұрын

    Thank you! Ill give it a good try

  • @suicune2001
    @suicune20012 жыл бұрын

    Thanks! That looks really handy. I'll definitely need to practice that.

  • @eileeng2492
    @eileeng24922 жыл бұрын

    Your hitting great points BMO Thank you

  • @MaximumSpice
    @MaximumSpice Жыл бұрын

    Super simple and super clean. It's been something I've been wanting to learn but haven't got around to it yet. Think I might consider switching my current project over into this.

  • @harshavardhanpatilsp190
    @harshavardhanpatilsp1905 ай бұрын

    Very good explanation.

  • @techdave99
    @techdave992 жыл бұрын

    Another great video. Thanks.

  • @sajadtroll9186
    @sajadtroll91862 жыл бұрын

    FINALY I CAN UNDERSTAND EVENTS! THANKS A LOT and again you are kinda Big Fitness Marshmallow ❤️

  • @andyBorko
    @andyBorko Жыл бұрын

    The best explanation of observer pattern. Thanks!

  • @nicolasportu
    @nicolasportu Жыл бұрын

    Cristal clear :) Advanced version looking forward. Thanks

  • @MountReal786
    @MountReal7862 жыл бұрын

    Thank you for this

  • @Fyres11
    @Fyres118 ай бұрын

    Yup very simple that way to decouple. A way to make it even safer and keeping the call private on both side is using a scriptableObject event as the middle man similare to what you did with scriptableObject variables.

  • @woistdasniveau8290
    @woistdasniveau8290 Жыл бұрын

    Nice tutorial. I love how almost everything boils down to yeah just learn it xD

  • @muhammadzubairirshad6724
    @muhammadzubairirshad6724 Жыл бұрын

    Great

  • @322ss
    @322ss2 жыл бұрын

    I usually have used empty delegate in my event (OnCoinCollected = delegate { }), so I don't have to do old or new style null checks at all.

  • @stephenkentperez7705
    @stephenkentperez7705 Жыл бұрын

    if for some reason your public method needs parameters you can also pass in parameters by using a lambda operator when adding a listener: OnExecute_Event += () => { SomeMethod(paramater); };

  • @Stompin40
    @Stompin402 жыл бұрын

    Exactly!!!!!!!!!

  • @TheKr0ckeR
    @TheKr0ckeR Жыл бұрын

    One thing I dont understand is, in this case, Subscriber to the event still needs to know about the class that has the Action? For example, I have an inputmanager class that has action inside of it called OnTap. When subscribing, I need smth like: InputManager.OnTap += DoSomething; How would i prevent this in this case? In your example, CoinText script is still coupled with Coin script?

  • @luckyknot

    @luckyknot

    10 ай бұрын

    I understood the same as you. The good thing is that InputManager does not have to implement any call to any other classes, so it's doing only one thing which is good (separation of responsibilities). There must be a way to centralize all these OnSomeAction() that are scattered around in different classes and put them in a single manager class so the other subscriber classes can refer to that only class. That would be pretty neat.

  • @zacoriot
    @zacoriot4 ай бұрын

    Probbly a rookie question but: 1. Why not subscribe during Awake or Start? 2. Why unsubscrube from an event?

  • @godzillakingkongvenom
    @godzillakingkongvenom2 жыл бұрын

    👍😎🤠

  • @_Garm_
    @_Garm_2 жыл бұрын

    Me += OnBMo :)

  • @stefan429
    @stefan4292 жыл бұрын

    You should totally make a followup video about ran hipple's talk and implementing scriptable objects as events. theres a package already that does it called SO Architecture, maybe you can go over it, its very useful especially for newbs since they dont have to worry about subscribing/unsubscribing to events :D

  • @jtmcfarland3512
    @jtmcfarland351223 күн бұрын

    I understand this is "proper" and "how it's done", but I have to disagree with the philosophy. You've only managed to move the dependency from one script to another, not remove any dependencies. Also, by using events, now you've put more burden in the Update() part of your program. Events should only be used in the most demanding of circumstances and NOT every time you need one script to talk to another.

  • @RayHorn5128088056
    @RayHorn5128088056 Жыл бұрын

    You traded one type of dependency for another. Big deal.