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
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.
I love observing the pattern of how many videos you have been making *snuggles*
This is probably the easiest to follow video I’ve seen yet on uncoupling dependencies with a concrete example, kudos.
Listen, broadcast, execute! Love this, keep ‘em coming Bmo!
Keep going with those type of vids I love them!
Its been long since i got away from unity but i can't stop watching your video and get the vibe to go back
I would like you to picture me in my best DJ Khalid voice saying “anotha one”
This channel really make things easy to understand, thank you so much
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
2 жыл бұрын
Thank you! Ill give it a good try
Thanks! That looks really handy. I'll definitely need to practice that.
Your hitting great points BMO Thank you
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.
Very good explanation.
Another great video. Thanks.
FINALY I CAN UNDERSTAND EVENTS! THANKS A LOT and again you are kinda Big Fitness Marshmallow ❤️
The best explanation of observer pattern. Thanks!
Cristal clear :) Advanced version looking forward. Thanks
Thank you for this
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.
Nice tutorial. I love how almost everything boils down to yeah just learn it xD
Great
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.
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); };
Exactly!!!!!!!!!
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
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.
Probbly a rookie question but: 1. Why not subscribe during Awake or Start? 2. Why unsubscrube from an event?
👍😎🤠
Me += OnBMo :)
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
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.
You traded one type of dependency for another. Big deal.