The more I learn about game dev the more I learn that events are essentially the most important tool to use. Decoupling is so important for re-usability and maintenance.
@riteshkhokhani2 жыл бұрын
Yes, for re-usability, code maintance and code extensibility loosely coupled code is must. and for loosely coupled code, observer pattern is must have.
@CoReeYe5 жыл бұрын
I'm working in a game studio and these stuff he talked about are the things we keep discussing about every day. These videos are really useful if you want to make things in a professional way.
@nitras.design4 жыл бұрын
if that is a daily discussion topic i wouldn't stay long there.
@CoReeYe4 жыл бұрын
@@nitras.design Why do you hate discussing architecture?
@IDontReadReplies420692 жыл бұрын
@@CoReeYe he doesn't hate it, he's just saying if the entire team is sitting around discussing things a junior should already have engrained into him you must be working for a joke of a company
@shermenz5 жыл бұрын
Jason from Unity3d collage, I'll have you know my good sir! That I was wondering how on earth will I be able to learn design patterns with unity, and your wonderful Solid series popped into my mind, well then! I went to your channel thinking I'll suggest you'll do a video or two on the subject, and lo and behold, my man delivered once again. Thank you thank you! Keep em Coming, these videos have a very deep value for us noobs compared to the shallow depths most youtubers dare to dip.
@arthurgentz25 жыл бұрын
Hey Jason, It's great that you're covering Nystrom's book. I was actually introduced to it by another youtuber. In the book, the patterns were sort of well explained, but the examples were a bit empty. Because you don't exactly see where they slide into a project. But since you're covering them in C# and Unity, well, that's awesome. I can't wait to finish this series man. Seriously, thank you for this.
@hplovecraftmacncheese2 жыл бұрын
This is a nice "make it better" implementation. I'm still in the "just make it work" stage as a noob, but I'm sure I'll get to this at some point.
@lukefischer84245 жыл бұрын
got the book. i find it really helpful to read a chapter, see your video and then read the chapter again. thank you for this series!
@Unity3dCollege5 жыл бұрын
Thank you :) I definitely recommend everyone take a look at the book too. He does a good job explaining how the patterns work and some of their origins.
@kobby2g85 жыл бұрын
Thanks for this video. I had setup events in my game prior to watching this video but they were non-static. Making them static solved a ton of issues I was facing with null references. Edit: Also should add that (especially now that Unity supports C# 7), You can use invoke together with a null conditional '?' as a shorthand for null checking before calling like so: OnKilledEnemy?.invoke(args)
@philipwilliams98334 жыл бұрын
Man, I just clicked this video without looking who made it, and and soon as the hey whats up started I realised I had clicked another one of your amazingly informative videos. I know its an old video but thanks anyway dude!
@walterh21135 жыл бұрын
Great knowing you're doing a full series on the book!
@unchartouille12083 жыл бұрын
Thanks ! I'm a front-end Angular developper and we use A LOT the observable pattern. I'm new into unity, and I love your video. The thing that haunt us in web dev is when we have multiple sources of truth hidden everywhere in your code. This is a nightmare. Redux pattern is good.
@SunnyApples5 жыл бұрын
I've learned this from one of your previous videos. And I've been using it all the time.
@ThoughtExperiment5 жыл бұрын
Keep the content coming.. design patterns with large-scale projects is the stuff I have been struggling with.
@SirRelith5 жыл бұрын
Hey man, been watching your videos a long time. Love your content, you are a great teacher! I just have one request. Could you use a dark theme in visual studio. I like to watch your videos at night sometimes, and it just blinds my eyes, even with my monitor brightness turned all the way down. I skipped watching some of your videos because of the blinding light. Anyways, not a huge deal for everyone I bet, just my opinion. Thanks for this great lesson! I hope to put it to good use.
@STARasGAMES5 жыл бұрын
same here
@ryanzwe5 жыл бұрын
Yessssd
@beardordie53085 жыл бұрын
Agreed.
@Unity3dCollege5 жыл бұрын
hmm, i'll definitely consider it lol. not sure how it'll feel w/ how bright my office is, but i can give it a shot :)
@monohybridstudios5 жыл бұрын
This is the theme I use. Not too dark but just right for day and night use in my opinion. It says "VS 2015" but will work with 2017 no problem. There are lots more on the website as well to choose from. studiostyl.es/schemes/indie
@MarekNowakowski3 жыл бұрын
my programming skills didn't reach going out of Class yet, so it was hard to follow ;p I had a 30 year pause since Basic and Pascal were cool and your videos are helping me transition ;p cheers.
@samwalter172 жыл бұрын
Making events static is such a useful piece of information, thank you!
@walkwithsharif33413 жыл бұрын
Thank You Jason for explaining such clear presentation of observer pattern in unity
@riteshkhokhani2 жыл бұрын
Very well explained, I think to learn about c# observer pattern this is the best video. thank you so much.
@user-qh9xl9ry7d2 жыл бұрын
this is best video for observer pattern in unity
@Pedro5antos_5 жыл бұрын
Very interesting! I'm loving this programming pattern series, and how you detail every possibility, and ways of implementing the pattern! Keep up the awesome work Jason
@Unity3dCollege5 жыл бұрын
Thank you very much! :) I really like doing examples like this, just takes a while to put together a good one that's really usable in a project but fits into a short video, but it's also a lot of fun :)
@Pedro5antos_5 жыл бұрын
@@Unity3dCollege I was just watching the video again (1 month later), getting inspiration for coding and saw you answered my comment. Thanks for the response, I truly enjoy the content you put out for us!
@MohamedAdelQuickDeveloper5 жыл бұрын
Great man, I like your explanation a lot. Your videos helped a lot to get around things in unity. Keep up the good work. (thumbup)
@Unity3dCollege5 жыл бұрын
Thanks! :)
@SabaFilms4 жыл бұрын
Hey Jason, thanks for your videos! This is very good as I have been reading the game programming patterns book and have wanted to see c# specific examples and this really fits the bill!
@cptant76105 жыл бұрын
I was implementing my game's inventory UI and this was exactly the kind of pattern I was looking to implement.
@marcusotter5 жыл бұрын
Woah, new intro? Looks awesome. Thanks for these videos!
@Unity3dCollege5 жыл бұрын
Ah thx !: )
@bletzkarn76355 жыл бұрын
your videos are great, please keep it up, your channel will be big!
@jugibur21173 жыл бұрын
As always very helpful - thanks for your effort!
@Alluvian5674 жыл бұрын
Love these videos, going to have to spend some time unpacking that syntax, never used c# events before
@thomastum69925 жыл бұрын
Thank you for your wisdom. Please do make some tutorial on events!
@Unity3dCollege5 жыл бұрын
It's on my shortlist for things to cover soon :)
@thomastum69925 жыл бұрын
@@Unity3dCollege much appreciated!
@IvisibleCat5 жыл бұрын
Sebastian Lague did a realy good job on explaining events. search for it on his channel.
@CouchFerretmakesGames5 жыл бұрын
Very useful, Thanks!
@cam47225 жыл бұрын
Another great video, Jason. Thank you!
@DJZ3XY2 жыл бұрын
Too much event-based programming becomes a hard to debug mess though. Like if you have everything "loosely coupled" through events, like events for when a spell is cast, events for when it hits something, events for when it dissipates, basically everything being event-based, that starts to feel like a nightmare to debug because you're always looking for what event is triggering this thing that just happened, what's registered and not registered to my events, etc. Any thoughts or suggestions for this problem?
@maurofuentes80765 жыл бұрын
Genius! Love watching your videos.
@monochromenight89435 жыл бұрын
You're the man for this.
@tonymengelkoch74134 жыл бұрын
Incredibly helpful!! Thank you!
@amirtavakoli48092 жыл бұрын
What a nice tutorial. Thank you so much
@geri43675 жыл бұрын
Very useful. Great as always!
@Unity3dCollege5 жыл бұрын
Very welcome! :)
@vitalypopuzin94735 жыл бұрын
Thanx! Video about observable collections would be great!
@D_RockBottoms5 жыл бұрын
I love your videos. It is great that you explain and demonstrate importent subjects that arent usually shown in other tutorial channels. A good way to implement the observable pattern in unity is to use UnityEvents in order to be able to register to the events from the editor. I really recommend this lecture kzfaq.info/get/bejne/qMeBZsx5zavDe58.html to see a good example. In my team we use a system that has 3 components. The event raiser (object) the event listener (observer) and also a class that passes parameters with the event (we call it event params). When an event is raised it passes a parameters object with the event to all the listeners. In this way you can pass different parameters. For example, if you raise an event every time the score in the game is updated then you can pass with the event an event parameters object that holds the score value. If you for example have an event that needs to pass the player state you can send an event parameters object that holds the player object or an object that holds the player state. I hope this help answer some questions i saw in the comments.
@unlockener5 жыл бұрын
I do not understand the naming convention here (I see it everywhere not just your example). Why is the event called "OnPointOfInterestEntered" and not just "PointOfInterestEntered" ? I use to name events without the "On" prefix and then the subscribed methods are named "OnEventName". It's the same like e.g. "OnCollistionEnter" is the name of the method that is called when CollisionEnter happens. I understand that it can look nice in the code "OnEvent += DoThis", "OnEvent += DoThat", but I find the readability of code and logic better when I have only one method in the class that handles the behavior if the event is raised, so I have: "EventName += OnEventName()". If you know more, please enlighten me.
@owofinctive81865 жыл бұрын
This is useful and clear !!!
@fearshad82035 жыл бұрын
Really Cool Man . Please Make More Tutorial About Game Programming Patterns :P
@AlbertoFdzM5 жыл бұрын
Thanks for this video. I have doubts about when I should use UnityEvent System and when to use C# Event.
@MMMM-vc5oi5 жыл бұрын
It is easy. If you have monoBehaviours, you should use UnityEvent. It is powerful while if you have plain C# classes, employ C# events. You know why UnityEvent is powerful because it decouples your codes and you add listeners/observers inside the inspector not inside codes but one big drawback is that you can not rename the functions easily because you have to add them again (missing references)
@rafaelkuhn3 жыл бұрын
Hey Jason what do you think about using UnityEvents and UnityActions to implement this pattern?
@alexandreclement93992 жыл бұрын
They're slower so it also depends on the optimisation you're looking for
@vox-scriptum3 жыл бұрын
Thank you a lot for your work!
@Tech-Guy4 жыл бұрын
Many sites warn that one should not use static events because of memory leaks. Did you check if this will create dependency and keep GC from collecting a gameobject that has the class with this static event of something is subscribed to it?
@shaggun5 жыл бұрын
At 9:33 when you declared your static event in line 6 and then you check if it's null at line 15, In a previous video you declared the public static event like this: "public static event Action EventName = delegate {}" avoiding the null check later, is there any drawback of using that notation to avoid typing the extra 2 lines for the null check?
@Unity3dCollege5 жыл бұрын
There's no real drawback, I've just gotten in the habit of using ?.Invoke but didn't want to use that here since it's a bit outside the scope. But if you're able to use the latest .net, ?.Invoke() is the way to go IMO :)
@ZoidbergForPresident5 жыл бұрын
?.Invoke() ? What do you mean? How would it look here?
@@ZoidbergForPresident hi I know it is late to comment on your question but " .? " operator basically checks if left hand of the statement is null. Here is the reference to official .net explanation. docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-conditional-operators
@bogumiwyrwa2164 ай бұрын
Hello, Greate tutorial. It is any other solution to put OnPointOfInterestEntered in different place, not only in OnTriggerEnter?
@MrStoltverd4 жыл бұрын
I still don't get what is it you have to register... :/ Do you register the event you're looking for? Do you just make it an Observer and register it as such? if so... why? Where do you put the result of the notification? Where would one implement whatever function one may want when the observer is notified of something?
@onurtnrkl5 жыл бұрын
What if we have to observe multiple property in same subject, how can we implement abstract classes or interfaces. For example; We have a Player class with string name, uint level and double gold properties. How can we register OnNameChanged, OnLevelChanged and OnGoldChanged? Maybe using Observable class for each property?
@thinkofwhy5 жыл бұрын
public delegate void PropertyChangeHandler(); public event PropertyChangeHandler OnNameChangedEvent = delegate { }; public event PropertyChangeHandler OnLevelChanged = delegate { }; public event PropertyChangeHandler OnGoldChanged= delegate { }; public void RegisterOnNameChangedHandler(PropertyChangeHandler handler) { OnNameChangedEvent += handler;} ...
@onurtnrkl5 жыл бұрын
I asked that "How can we implement abstract classes or interfaces?". I mean, abstract Subject implementation. By the way I dont like event callbacks, Interfaces 2.5x faster than events.
@wokarol5 жыл бұрын
That's the point where using event would be a lot better. Maybe doing enum of event types would be good? Then AchievementSystem would do something like if(eventType == EventsType.HealthChange) { ... doing something with the value ... }
@STARasGAMES5 жыл бұрын
The only way is to split things in several MonoBehaviours that derives from Subject. So there would be PlayerName.cs, PlayerLevel.cs, PlayerGold.cs. And place them on player GameObject. Composition! XD I`m also dont like c# events, but not because of performance. You can't serialize c# events, also no way to access listeners and debug things. I think the best way to do events in unity is ScriptableObject's Events. The 'must have' video: kzfaq.info/get/bejne/qMeBZsx5zavDe58.html (27:33)
@onurtnrkl5 жыл бұрын
@@STARasGAMES I already thought about that but I am finding better way, thanks any way :)
@Umbravi4 жыл бұрын
This is a really nice video on events and handlers, and how to implement them in Unity. I'm new to unity, and game design, but I'm wondering if you are familiar with the flux/redux pattern of managing state, and how would one go about a similar action -> reducer -> state subscription pattern with unity. Thanks!
@LanPodder4 жыл бұрын
Isnt that like the react way of observer pattern?
@Umbravi4 жыл бұрын
@@LanPodder The flux pattern is a larger idea than observers, and would use observers on specific pieces of state to fulfill subscriptions. Flux details how data flows and interactions happen, not just what piece of data belongs to a or many interact-able entities.
@rickster52245 жыл бұрын
Thank you my good sir
@andriibilych4 жыл бұрын
Thank you, man
@devolli34012 жыл бұрын
Events in super short: When this event happens, do that event.
@ardrion51624 жыл бұрын
Wow, that pattern using events got me really confused. Mainly because it mixes static events and non-static private functions of an object. (I'm not a C# dev so this whole event thing is very new to me)
@The28studio5 жыл бұрын
I use this a lot too , but i use Unity builtin events
@wokarol5 жыл бұрын
What do you mean by Unity built-in events? UnityEvent?
@jaxowaraxa5 жыл бұрын
Could you make more about this also with scriptable objects?
@Unity3dCollege5 жыл бұрын
Definitely. I don't normally do my observables with them, but there have been a few projects where it was a good fit, I'll see if I can put together a good example sometime soon.
@everabyss5 жыл бұрын
Event Systems e.t.c after the first method you told goes over my head. Can I go with the first techniques and still its good practice?
@nartousan38664 жыл бұрын
hello i have this error couldn t fix it Assets\scripts ew\PointOfInterest.cs(10,20): warning CS0649: Field 'PointOfInterest.poiName' is never assigned to, and will always have its default value null
@Kalahee3 жыл бұрын
5:06 is what should be immediately taught by UI tutorials... I'm getting exhausted by all the absolute beginner tutorials that are actually teaching bad programming hygiene. Beginners end up making a huge mess, spaghetti code, and just drop it.
@janosdomboroczki84565 жыл бұрын
Why is it a bad practice to inherit from that abstract class this case and generally? You can have static events in the abstract classes or plain classes as well.
@Aristaeus895 жыл бұрын
It is limiting. If you use a base class to subscribe to subjects, then it becomes difficult to inherit from other things. Say if you had a base class for Spells, but you also have a Fireball that should also be an observer. If both Spell and Observer inherit from Monobehaviour, you simply can't inherit from both in C#. Multiple inheritance comes with several issues and you might not want to have your systems be made in a way that makes these things occur.
@endihidir20874 жыл бұрын
I will request something about Design Pattern tutorials. Can you use these patterns in a real game example more specifically? Also Can you give more example about usage area of Observer pattern in Games?
@OtakuDYT5 жыл бұрын
If those events weren't static I guess the next best thing would be to make the achievement system static and have them register themselves? If neither are static then find is used?
@ZoidbergForPresident5 жыл бұрын
In the same book this video is based on, the author (and its sources) advises to not use singletons too often...
@OtakuDYT5 жыл бұрын
So maybe we can finally get to the bottom of this, are singletons used in game programming these days or not? So many ppl have varied opinions on this it's difficult to get a straight answer.
@Unity3dCollege5 жыл бұрын
Generally it wouldn't use a find, there'd be a singleton or some other way to inject to object. Much of the time though, the things observing something are the same things that create them, so I often find myself registering events right after creating the new object. On singletons... there are drawbacks & it's very easy to fuck them up and make your project a mess... but they're used almost everywhere, and using them carefully can be completely fine IMO. (i've been back and forth on this a lot over the years lol)
@plunntic3 жыл бұрын
@@OtakuDYT There's nothing wrong with using singletons per se. It's a simple pattern and it works. But as your project grows they may fuck you up. Say, you have this achievement system as a singleton, everything's fine, you're happy, and now 2 years down the road you decide that you want to have a local multiplayer (split screen) in your game where both players are logged in and both can unlock theirs achievements (don't know if anybody actually would want that, but that's just an example). So, now you need to have two achievement systems, right? One instance for every player. But it's a singleton, so you can't. And so the journey begins. You make it a not-singleton, just plain old instance. But you still have AchievementSystem.GetInstance() all over the place. So you have to get rid of that. And now what? Well now you have to provide the instance of achievement system to every other object instance that depends on it. Not an easy task when you have a lot of code structured around the assumption that everything is a singleton, with many layers of abstractions. It gets even harder because now you have two instances of the system, and you need to somewhere / somehow decide which instance of the achievement system to pass to which game objects. It probably would be better to just leave the system as a singleton, and make it support any number of players, and pass the player that unlocked the achievement as an argument when calling the system. And in this specific case this may be a good enough solution. But in other situation may not. Also, singletons are not great when it comes to writing tests - which you should if you're making anything more sophisticated than a pong - because you can't see (from the outside of the class) that it's using some singletons, and even if you know it, it is still hard to mock them. Sure, you can always do some monkey patching magic or what not. But at this point it is just more hassle than it's worth, i'd say.
@OtakuDYT3 жыл бұрын
@@plunntic Thanks for that, yeah I'm currently busy trying to use things like scriptable object events to trigger things rather than direct system connections. Probably going to be issues with that down the line too but all I can do is improve as I go as necessary.
@HAWXLEADER5 жыл бұрын
How is this any different than just doing AchievementSystem.instance.UnlockAchievment(poiName) using a Singleton?
@gamerholic32144 жыл бұрын
HAWXLEADER with a singleton pattern, while you have global access to the singleton instance in any class, you have to directly call an object method. it’s like a 1 to 1 interaction. The object is not listening to a signal as you called the method directly. With the other observer pattern, you fire a notification (signal) and any listeners will respond to the signal. So you can have day 10+ objects listening to the same signal. When the signal fires all 10 objects will get notify and do their thing.
@janosdomboroczki84565 жыл бұрын
Hi all, could someone tell me, how is the Unity ecs and job system is going to affect these patterns?
@NilsMoller3 жыл бұрын
I do this with unity events, but I've recently been told C# events are better. Why is that?
@bobmcbob43992 жыл бұрын
C# events are great in non gaming apps. In gaming apps, you don't want GC allocations and events/Action/delegate will add this behind the scenes giving the GC a random chance to run during gameplay and introducing micro pausing in your main thread which is judder, affecting the smooth FPS you desire.
@makulVR3 жыл бұрын
why not using events?
@rockname5 жыл бұрын
What are the benefits of this pattern instead of delegate and events?
@Unity3dCollege5 жыл бұрын
Delegate & events are essentially a c# implementation of that observer pattern functionality, wrapping up a lot of the underlying code :)
@Caden_Burleson2 жыл бұрын
@4:33 "But, just to keep it simple, as my not perfect example", I think you should actually use a real world use case next time. Your videos always show up first when I'm looking for a tutorial, but I don't always watch them, because of this exact reason. They tend to be way too long and don't have very good examples. Just a tip for your next video. Thanks!
@mchajakanhna5 жыл бұрын
this video is like chinese to me, I tried to stay focused watching from the begenning to the end. maybe I'll understand when my beard turn grey
@LanPodder4 жыл бұрын
Have you worked with javascript? Because events are used a lot there aswell and i found that knowing about those helps with understanding this pattern
@gazoonman4 жыл бұрын
Jason please share the code, maybe on your repo?
@Gnerlich4 жыл бұрын
hey nice boi
@Sodomantis5 жыл бұрын
Eye nuke
@humadi20013 жыл бұрын
Bottom line 14:06 lol
@midnightoilgames97965 жыл бұрын
A lot of bad practices in here just to show off one good one. Maybe make the videos a bit longer so you can have good practice code all around
@ggjosh32965 жыл бұрын
Insert cliche "First" comment here. ;)
@ZoidbergForPresident5 жыл бұрын
"cliché"
@alexaba87145 жыл бұрын
Hey guys. I need your help. I have an app(it such as book). And I need to store text from this book on the server and download it later from the server. What is the best way to store this text. To save style, color font size and so on.