Delegates, Events, Actions and Funcs - The Observer Pattern (Unity & C#)

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 มิ.ย. 2024
  • The observer pattern is essentially baked into C# and Unity. It comes in the form of delegates, events, actions, and to some extent funcs. The observer pattern de-couples the source of information from the object receiving the information. This makes unity projects more stable, easier to add mechanics, and far less likely to break.
    Blog Post Covering the Observer Pattern: onewheelstudio.com/blog/2020/...
    Sebastian Lague's Delegate Series: • [C#] Delegates and Eve...
    Become A Channel Member: / @onewheelstudio
    Buy me a coffee: www.buymeacoffee.com/onewheel...
    My video and streaming kit: kit.co/onewheelstudio/tutoria...
    Game Design Reading Suggestions: kit.co/onewheelstudio/game-de...
    Discord: discord.onewheelstudio.com
    Patreon: / onewheelstudio
    Twitter: / onewheelstudio
    Devlog: onewheelstudio.com/
    Twitch: / onewheelstudio
    Video Gear List (Amazon Affiliate)
    ************
    Sony A6100 Camera: amzn.to/32logXz
    Camera Lens: amzn.to/3KA4Wr4
    GoXLR (audio interface): amzn.to/35dmr03
    Shure SM7B Mic: amzn.to/3fNbV1E
    Blue Mic Arm: amzn.to/3tHgwdQ
    Neewer Light: amzn.to/3rD8eRv
    Light Softbox: amzn.to/3GUaHgQ
    Neewer RGB Light: amzn.to/3rCTQJ9
    TimeCodes
    0:00 Why you need the Observer Pattern?
    1:20 Problems Solved
    1:38 What does the Observer Pattern do?
    2:41 What Are Delegates?
    4:45 Defining Delegates
    6:21 Delegate Parameters & Return Values
    7:13 Multicast Delegate
    8:01 Static Delegates
    8:17 Events
    9:08 Actions and Funcs
    9:40 Observer Pattern Implementation
    #ProgrammingPatterns #ObserverPattern #UnityTutorial #GameDevelopment

ความคิดเห็น • 190

  • @pablo199014
    @pablo199014 3 ปีที่แล้ว +86

    This is the most underrated channel I've seen due its clean explanation and video quality.👌 👏

    • @OneWheelStudio
      @OneWheelStudio  3 ปีที่แล้ว +16

      Your kind words are much appreciated. Go tell 100k of your closest friends and lets get the channel rolling ;)
      I do feel like I've found a decent format with this series of videos. No nonsense. No fluff. No fumbling (or not much). No extraneanous information. Just an idea and examples boiled down to the minimum needed to understand.

  • @Quein922
    @Quein922 3 ปีที่แล้ว +30

    Simply amazing. I've struggled to understand delegates for hours (watching lots of tutorials), but this just made everything a whole lot easier. Thank you so much.

  • @m.guiliano6128
    @m.guiliano6128 2 ปีที่แล้ว +6

    I like the way you break design patters down into crisp and clear examples. I think your lucid explanations will be a watershed moment for many. Bravo!

  • @BanelingAspect
    @BanelingAspect 3 ปีที่แล้ว +11

    I've been struggling to understand the Observer pattern and its implementation for weeks now, but this video finally made it click for me! Thank you so much!

  • @Dahxelb
    @Dahxelb 4 หลายเดือนก่อน +3

    3:11 This is the best explanation of Delegates that I've heard so far and was what helped to make it finally click with me. And as you immediately follow up with, it's not just any kind of variable, but an Array[] or List type of variable, allowing multiple functions to be assigned to that one delegate.

  • @MojodoLPs
    @MojodoLPs 3 ปีที่แล้ว +16

    When thinking about tightly coupled code i always get flashbacks of one of my first big unity projects.
    I had so many errors just because of code running to late/to soon. Objects not being ready yet, and so on.
    I think learning to decouple systems is a really important step in becoming a better programmer.
    And delegates/events are pretty confusing when starting out, at least they were for me.
    So great job with that video :)

    • @OneWheelStudio
      @OneWheelStudio  3 ปีที่แล้ว +5

      Thanks! When I "discovered" this pattern it changed my code forever, but I definitely didn't understand all of what I was typing. And I 100% agree decoupling code is huge!

    • @sandsalamand3763
      @sandsalamand3763 2 ปีที่แล้ว

      If you're referring to the Awake and Start order of scripts, you can customize that in the Project Settings.

  • @Philosufur
    @Philosufur 5 หลายเดือนก่อน

    Watched a ton on videos on the topic(for the past few days!), this is the video that ultimately gave me clarity!

  • @ImBoyCryWolf
    @ImBoyCryWolf 11 หลายเดือนก่อน +1

    Dude you totally saved me with this one. I was really struggling to find clear explanations.

  • @seanc6193
    @seanc6193 2 หลายเดือนก่อน

    Ive been learning to Code with AI and this video cuts straight to the heart of what I've been tip toeing around. Amazing video, this has changed everything. Subbed

  • @hardworkingslacker7233
    @hardworkingslacker7233 7 หลายเดือนก่อน +1

    This is extremely sleek and beautiful to look at.
    I love simple solutions to complicated problems!

  • @lhorbrum1818
    @lhorbrum1818 2 ปีที่แล้ว +2

    Great video and well explained. But as i'm an idiot, i will need to watch it at least ten times! So, for the rest of the day, i shall be doing just that, as well as attempting mock ups. Wish me luck!
    Update: I finally get it! After all these years! How i lived without them is beyond me. Can't thank you enough! Thank you for this.

  • @hamzablueblood8bp847
    @hamzablueblood8bp847 2 ปีที่แล้ว

    Seen almost 10+ videos. But not even understand a what the heck is Delegate, events or Action. This one video made my day.

  • @Kudoshi
    @Kudoshi 3 ปีที่แล้ว +10

    Wow that is really useful. I was so confused with the many different types of events there are. Glad to see this video where it explains all of it.

  • @allanl5511
    @allanl5511 2 ปีที่แล้ว +1

    Unity beginner here, I was confused and looking for explanation everywhere until I found this video. Instant subscribe and thanks for your good work.

    • @OneWheelStudio
      @OneWheelStudio  2 ปีที่แล้ว +1

      So glad it was helpful. Events are definitely not an easy concept to wrap your head around!

    • @allanl5511
      @allanl5511 2 ปีที่แล้ว +1

      @@OneWheelStudio you made it plain and simple :)

  • @Tercurnich
    @Tercurnich 2 ปีที่แล้ว +2

    FINALLY! A video that explains it in an understandable way and a reasonable amount of time without pouring unnecessary code into the examples. You've got a new deserved subscriber!

    • @OneWheelStudio
      @OneWheelStudio  2 ปีที่แล้ว +2

      That's the style we go for around here. ;)
      Glad it was useful.

  • @iiropeltonen
    @iiropeltonen 2 ปีที่แล้ว +1

    I've seen several of your videos but this one got me to subscribe. I'm a beginner so I need this simple explanation style and throwing a shout-out to Sebastian Laque was most respectful. Good Job .

    • @OneWheelStudio
      @OneWheelStudio  2 ปีที่แล้ว

      Thanks for the sun! I think Sebastian is easily one of the best Unity tutorial creators out there!

  • @chrischains9947
    @chrischains9947 3 ปีที่แล้ว +1

    made my first delegate :D thanks one wheel studio!

  • @kruth6663
    @kruth6663 2 ปีที่แล้ว +1

    "Delegates can be thought as a variable that can be assigned a function as a value." That's the conclusion I finally figured out after watching a bunch of other tutorials on youtube. I wish I watched this video earlier and didn't have to figure this out myself!
    But I think that statement could be put a little bit more precisely:
    A delegate is *the class* of a variable that can be assigned a function as a value, while an event is the actual variable of that class (though it's not just a regular variable, but a collection of that class).

    • @OneWheelStudio
      @OneWheelStudio  2 ปีที่แล้ว

      I'd be a bit careful with that definition... as long as you can use them that's way more important than the definition... but "event" is a keyword that puts restrictions on the delegate.

    • @kruth6663
      @kruth6663 2 ปีที่แล้ว +1

      @@OneWheelStudio Oh, you're right, what I meant to say is that the individual event you define is the variable (collection), not the keyword event :)

  • @ftjnbunym4057
    @ftjnbunym4057 ปีที่แล้ว +1

    You are the best. Thank you for the good lessons.

  • @guille_sanchez
    @guille_sanchez ปีที่แล้ว +1

    @One Wheel Studio Excellent video, keep it up!!

  • @dibaterman
    @dibaterman ปีที่แล้ว

    I'll be honest as a person who already understands this, I know the very recent me who didn't understand would not have understood based on this video - I can't say what made me understand delegates, actions, func and so on but things just got very clear.
    I'll try to reiterate what was said here in a way that I feel I would have understood:
    Delegates, func and so on use subscribers. Think of it like subscribing on YT and hitting the alert icon. When you do this you are an observer, when the subscribed channel posts something you are alerted and take actions or not based on that alert.
    With that said you C# class that will be observing a particular thing needs to subscribe first. Generally the easiest way to do this with maximum decoupling of the code is to make a public STATIC class. In that class you write out all of your invoke situations so: onCritterDied or onPlayerJumped and so on. They should look like this:
    public static event Action onCritterDied;
    Inside the you can put variables and classes which can help identify or carry over data. So: or , the overloads goes on for a while so is all possible.
    Using a Func allows for the same stuff but you can also add a return type which is always the last overload variable.
    So say you had:
    var item = StaticClass.SpawnBullet(bulletPrefab);
    The static class would have:
    public static event Func onSpawnBulletRequest;
    public static void SpawnBullet(GameObject bullet)
    {
    onBulletSpawnRequest.Invoke(bullet);
    }
    This is a good way to communicate with a pooler that you don't want to reference from any other script.
    On the pooler you just OnEnable or Start or Awake put:
    StaticClass.onSpawnBulletRequest += DoSpawnStuff;
    Be sure to unsubscribe also, hope that translated things for someone.

  • @Kudoshi
    @Kudoshi 2 ปีที่แล้ว

    5 months later I am here again. This time i am even more clearer than I was last time. Amazing video indeed.

  • @jugibur2117
    @jugibur2117 2 ปีที่แล้ว +1

    Wow, finally I understand the intricacies to "delegate" because you started slowly at the base.
    You have a real talent for explaining complex things (as I have seen in your other videos), thanks for your effort!

    • @OneWheelStudio
      @OneWheelStudio  2 ปีที่แล้ว

      Thanks for the kind words! 15 years or so in a classroom builds at least a few skills ;)

  • @hello.4693
    @hello.4693 11 หลายเดือนก่อน

    Thank you so much, this is actually the first video that is clear and helpful for me.

  • @thelazyguy3735
    @thelazyguy3735 2 ปีที่แล้ว

    I came here from Sebastian Lague video to expand on the knowledge and it was nice to see you recommend it

  • @briana3186
    @briana3186 ปีที่แล้ว

    MAN! It's finally sinking in. Thank you so much! Best channel ever.

  • @griseus8879
    @griseus8879 2 ปีที่แล้ว

    You are an angel sent from the heavens to cast upon us mortals your infinite wisdom. Thank you!

  • @ProtoSimTechnologies
    @ProtoSimTechnologies 2 ปีที่แล้ว +1

    Ya know, I had to watch this a few times to really understand it, but this absolutely blew my mind on just how simple and effective it is. Outstanding explanation ;-)

  • @_Secession
    @_Secession 2 ปีที่แล้ว

    Was able to get something to work correctly due to this video... Awesome!

  • @MrDnBsound
    @MrDnBsound 2 ปีที่แล้ว +1

    Such a good, clear and easy to understand explanation with amazing examples! I can't wait to check out the rest of the channel.

  • @OneWheelStudio
    @OneWheelStudio  3 ปีที่แล้ว +2

    For those who like written tutorials - Observer Pattern Blog Post: onewheelstudio.com/blog/2020/7/24/observer-pattern-c-events

  • @AndyLaneOlson
    @AndyLaneOlson 3 ปีที่แล้ว +1

    Thank you so much! I have watched around ten other videos trying to wrap my head around this. You just made everything click. Subscribed and liked!

  • @Landon_Hughes
    @Landon_Hughes 2 ปีที่แล้ว +4

    Very good explanations! Honestly, it's probably the best one I've seen 😁

  • @Gregasaurus
    @Gregasaurus ปีที่แล้ว

    OMG. The best ever explanation on delegates. Such a clear, progression and explanation of the subject of Delegates. Subscribed. So good.

  • @d1a9n8a8
    @d1a9n8a8 3 ปีที่แล้ว +4

    so glad I found this video it makes things so much easier... thank you !!!!

  • @victor1102
    @victor1102 3 ปีที่แล้ว +1

    Just 4 words for you:
    You are the best!
    Cheers!

  • @hasanklc5721
    @hasanklc5721 ปีที่แล้ว

    that is the most effective and simple explanation of the delegates and events! Thank you so much

  • @Giorgos_Efthymiou
    @Giorgos_Efthymiou 2 ปีที่แล้ว

    Fundamental explanation about delegates, events, actions and funcs! Great video!

  • @jsn4591
    @jsn4591 3 ปีที่แล้ว +1

    Awesome explaination. Super clear and helped me understand the differences between delegates, events, actions and funcs. Thanks for the great video :)

  • @brianwilson7497
    @brianwilson7497 4 หลายเดือนก่อน

    This was so good, thank you so much. It finally clicked with me here - thank you for the simple examples, visuals and explanations.

  • @jopastudio
    @jopastudio 3 ปีที่แล้ว +1

    Clear explanation, complete, great video, thank you!

  • @omyb
    @omyb ปีที่แล้ว

    Even if I was using it, I was doing it without a proper understanding. I really like how you explain things, it's really crystal clear with great pedagogy!
    My new best game dev channel!

  • @Math429
    @Math429 3 ปีที่แล้ว +1

    You explain very well and clearly. Thank you for the video!

  • @homemacai
    @homemacai 2 ปีที่แล้ว +1

    Thanks! this one got some time to enter in my head, but after this video, Sebastian's one and a few others also, finally think I got the delegates stuff.

  • @JansenJaspion
    @JansenJaspion 3 ปีที่แล้ว +1

    Dude! I've been trying to make an Action work for like, 30 minutes, and 5 minutes of your video solved my problem!!! Thanks a lot! Keep up the good work!
    Liked and subscribed!

  • @sharat.achary
    @sharat.achary 2 ปีที่แล้ว +1

    Simple, clear and clean explanation. 👍

  • @sqdtnz
    @sqdtnz ปีที่แล้ว +1

    This is better than many paid udemy courses. I would like a video about assembly definitions, especially how to use these in a complex project. I keep getting stuck with cross referencing..

    • @OneWheelStudio
      @OneWheelStudio  ปีที่แล้ว +1

      That's a great idea! I'll add to my (long) list ;)

  • @giampaolo004
    @giampaolo004 2 ปีที่แล้ว +1

    excellent explanation, thanks! I had to run it multiple time at half speed to have a chance to grasp all of it (...I'm a little slow :-) but I managed to get a better idea about the the concept in the end.

    • @OneWheelStudio
      @OneWheelStudio  2 ปีที่แล้ว

      This is no easy topic! Something about it makes it hard to wrap your head around. I learned a ton by making the video. ;)

  • @AudioBoi1
    @AudioBoi1 2 ปีที่แล้ว +1

    This was, finally, the video that did explained this to me well.
    All other vids and tutors were too hard to understand.
    Thank u

  • @ShakerFS
    @ShakerFS 2 ปีที่แล้ว

    If not for this video I still wouldn't be able to understand delegates haha xD Simply amazing, thank you so much!

  • @PixelDough
    @PixelDough 3 ปีที่แล้ว

    I’ve tried using delegated before but struggled to figure out the proper way to use them. This gave me a whole new perspective on how to use them! Thanks! This will help a ton!

    • @OneWheelStudio
      @OneWheelStudio  3 ปีที่แล้ว +1

      I'm making these partially to learn more myself! I'm glad it helped you too.

  • @ronaldrozema8346
    @ronaldrozema8346 2 ปีที่แล้ว +1

    Thanks. Really clear explanation of what happens under the hood! Keep it up this is really useful!!

  • @anyone8225
    @anyone8225 2 ปีที่แล้ว

    Absolute gold this vid! I for some reason always have some unity technologys which I fear to look into, because I think they are surely hard... And when learning them, they are just super useful. Like this one! Finally using Actions and it is such a bless, thank u😘

  • @CosmicComputer
    @CosmicComputer 3 ปีที่แล้ว +2

    excellent overview, thank you!

  • @IndieDX
    @IndieDX 2 ปีที่แล้ว

    Wow! Such a clear explanation!

  • @unityvictor787
    @unityvictor787 3 ปีที่แล้ว +1

    Thanks for Tutorial Video, very easy to understand !!

  • @DocBloom
    @DocBloom 2 ปีที่แล้ว +1

    Great video and explanation!

  • @thomashaug5086
    @thomashaug5086 3 ปีที่แล้ว +6

    This is such a phenomenal channel. Keep it up. Seriously helped me with my game dev project... though I need to do some refactoring haha

    • @OneWheelStudio
      @OneWheelStudio  3 ปีที่แล้ว +1

      Thanks for the kind words. I'm glad it's helpful!

  • @VADIM-SOLOV
    @VADIM-SOLOV 2 ปีที่แล้ว +2

    Вау! Это действительно очень простое и понятное видео. Спасибо Вам за это :)

  • @LucasZaranza
    @LucasZaranza 2 ปีที่แล้ว +1

    Wow, thanks a lot for this video! The best explanation about event system with C# I've heard. Keep going, dude. ;)

  • @OldHeadTrivia
    @OldHeadTrivia 6 หลายเดือนก่อน

    Thank you so much for this video. So clear and concise!

  • @DiegoTube85
    @DiegoTube85 2 ปีที่แล้ว +1

    congrats, very useful, very well explained!

  • @3isr3g3n
    @3isr3g3n 2 ปีที่แล้ว +1

    THANKS! Great explanation and presentation, subscribed! This video really helped me with my studies.

  • @Dev-gp5hn
    @Dev-gp5hn 2 ปีที่แล้ว

    is very helpful. i watch so many video but your video is good explanation at all.

  • @PauloSamurai
    @PauloSamurai 3 ปีที่แล้ว +1

    Awesome explanation! Thanks

  • @yousefahmed1336
    @yousefahmed1336 2 ปีที่แล้ว +1

    Thank you so much
    Other videos are mad confusing

  • @CosbyKong
    @CosbyKong 2 ปีที่แล้ว +1

    Really clear explanation for this, extremely, going to try this out asap and stop only using singletons in larger projects!

    • @OneWheelStudio
      @OneWheelStudio  2 ปีที่แล้ว

      Events are amazing and game changing once you start to use them. One thing to watch out for is events invoking events. Meaning if one script is listening to an event and then calls a function that itself invokes an event you can end up with crazy loops - to the point of locking up the game. Also if multiple systems are listening to a given event you can potentially end up with "race conditions" where one system is responding before another - I've seen inconsistencies between the editor and a standalone build in this regard. Those aren't reasons to not use events - they aren't perfect - but in general definitely better than singletons. ;)

  • @luccabollani377
    @luccabollani377 3 ปีที่แล้ว +1

    Awesome explanation

  • @etistyle96
    @etistyle96 หลายเดือนก่อน

    incredible video thx a lot ! So much things make so more sens now.

  • @user-qk3jv4sf1x
    @user-qk3jv4sf1x 2 ปีที่แล้ว +1

    Very good video. Thank you.

  • @walkwithsharif3341
    @walkwithsharif3341 3 ปีที่แล้ว +1

    so amazing to understand observer pattern
    thanks

  • @kulpajj
    @kulpajj 2 ปีที่แล้ว +1

    wow this was great. You are a great teacher and use great examples! It's very cool you explain WHY this pattern is used and what it protects against. Sometimes it's the "why" that is missing in tutorials. The "why" makes the pattern stick! Also, thanks for the words of encouragement throughout the tutorial. =-))

    • @kulpajj
      @kulpajj 2 ปีที่แล้ว

      Hmmm, I have one further question about the Events system (sorry if it's been answered below). Say I have a prefab, e.g. a Wave gameobject, and at runtime I instantiate 10 Wave's, each with its own ID. In my system, other gameobjects are only supposed to observe one of the 10 Waves at a time. Is this only possible with GameObject.Find("Wave" + ID).GetComponent(), etc, etc? Or would such a thing be possible with delegates and the events system? Thank you in advance!

  • @rkpixeldesign
    @rkpixeldesign 3 ปีที่แล้ว +1

    Thanks man i really understand it now

  • @gamedevbaiyi936
    @gamedevbaiyi936 2 ปีที่แล้ว

    Thanks for your nice video. It help me a lot .

  • @AraragiAriel
    @AraragiAriel 2 ปีที่แล้ว +1

    Great video!

  • @UltramarineAfterglow
    @UltramarineAfterglow 3 ปีที่แล้ว +1

    this is a great tutorial, thanks

  • @1AnimalKingdom10
    @1AnimalKingdom10 2 หลายเดือนก่อน

    Such a good video. You are amazing!!

  • @evandavis3683
    @evandavis3683 3 ปีที่แล้ว +1

    Great Video!

  • @drorriov8833
    @drorriov8833 ปีที่แล้ว +1

    Good video, thanks.

  • @pacifista2099
    @pacifista2099 ปีที่แล้ว +1

    Thank you so much.

  • @wowgingermobile2123
    @wowgingermobile2123 2 ปีที่แล้ว

    Woohoo this is super precise and informative! Through your video I'd attained 90% clarity, with the remaining 10% that is still puzzling me is the "Eventhandler" and the "Eventargs", much appreciated if you can explain their usage within the Event System context.

    • @OneWheelStudio
      @OneWheelStudio  2 ปีที่แล้ว +1

      Hmm. I honestly didn't dive that deep. But could be a good follow up video!

    • @wowgingermobile2123
      @wowgingermobile2123 2 ปีที่แล้ว +1

      @@OneWheelStudio Thank you and appreciate your reply!

  • @JohnnyThousand605
    @JohnnyThousand605 2 ปีที่แล้ว

    It was indeed interesting and useful, thank you =)

  • @MahanRezaei
    @MahanRezaei ปีที่แล้ว

    The Best Video about Event . TNX

  • @lln6123
    @lln6123 ปีที่แล้ว

    Very good Video

  • @jackwithers5041
    @jackwithers5041 11 หลายเดือนก่อน

    Amazing video!!!!!!

  • @riteshkhokhani
    @riteshkhokhani 2 ปีที่แล้ว +1

    well explained

  • @zamidare9038
    @zamidare9038 2 ปีที่แล้ว

    This is what I've been looking for! I've been wondering how to create a weapon system and don't know which path to take with the ammo count and circle ammo ui bar communication since anything is possible but with a messy approach. I want to grasp delegate and events. Just listening to the first parts make me think the video is so friendly to beginners.

    • @OneWheelStudio
      @OneWheelStudio  2 ปีที่แล้ว +1

      Delegates are definitely the way to go! I'm glad the video was useful!

    • @zamidare9038
      @zamidare9038 2 ปีที่แล้ว

      @@OneWheelStudio Thank you very much for a great video! I would like to see a summary of topics in unity scripting video like a lesson plan but that is impossible because there is a lot to talk about! Again, thanks for the tutorial!

    • @OneWheelStudio
      @OneWheelStudio  2 ปีที่แล้ว

      Do you mean something like an outline at the start of the video?

    • @zamidare9038
      @zamidare9038 2 ปีที่แล้ว +1

      @@OneWheelStudio It is like a list of intermediate concepts in unity scripting but abstract and is just a general road map on what to learn next. Because it might be easy for someone to have an idea to learn singletons first before comand pattern then next is x, y, z... topics. Just a what to learn next approach.

  • @Renegen1
    @Renegen1 2 ปีที่แล้ว +1

    well said

  • @ApexArtistX
    @ApexArtistX 3 ปีที่แล้ว +1

    Good tutorial sub

  • @redgamingspecial
    @redgamingspecial 3 ปีที่แล้ว +1

    Congrats! u have won a subscribe :P

  • @eslammagdy2358
    @eslammagdy2358 ปีที่แล้ว

    Really we should watch you then Sebastian thanks :D

  • @beng758
    @beng758 ปีที่แล้ว +1

    Using UnityEngine and Using System can lead to ambiguous references. I.e. if you are using Random.Range you will get the VS error "'Random is is ambiguous reference between 'Unity.Random' and 'System.Random'". To fix this, declare which method to use, i.e. "using Random = UnityEngine.Random;"

  • @doublew8815
    @doublew8815 3 ปีที่แล้ว +2

    i think i have only one remaining cell but the thing im sure of that this cell have finally understood delegates and events

  • @JohnK68
    @JohnK68 ปีที่แล้ว

    Sebastian Lague's has it's entertaining strengths, but explaining code is not one of them. At this point i have seen my fair share of TH-cam tutorials about events and what comes arround it but this one from One Wheel Studio is one of the best in explaining it clearly, only do i wan't to give the advice to wave your hands less while explaining. I hope you find a monetizing way to develop yourself further in this area!!! The other youtuber which does a decent job about events is Infallible Code.

  • @jorhoto7064
    @jorhoto7064 3 ปีที่แล้ว

    Hello, what about if you want to trigger your events "deferred", like in the "on lateupdate" unity frame event, instead of triggering everything instantly?
    Thanks for your great video and effort!

    • @OneWheelStudio
      @OneWheelStudio  3 ปีที่แล้ว

      If I understand what you're asking, you can invoke a delegate/event anywhere - if there are limitations such as in constructors or such I'm not aware. So invoking can certainly be down in Late Update - I haven't tried it, but I would assume it would work. Please let me know if it doesn't! :)

  • @dissonance650
    @dissonance650 3 ปีที่แล้ว +2

    I somewhat get the concept but still can't understand the implementation so in the CritterDisable class you use Ondisable function to invoke whenever the object is disabled but are you disabling the Scoredisplay to unsub as well? I mean I get it almost every single example has OnDisabled and On Enabled function but what are we really disabling in order to unsubscribe is it just convention? that part confuses me. Second part that I don't understand Is all the functions subscribed to event called at the same time, or is there one main function that gets triggered when subbed functions called by their own? if so...(brain melts) anyway I give up :) I think this is one of those limits that I have but thanks your video was the clearest explanation among the tuts I've seen.

    • @OneWheelStudio
      @OneWheelStudio  3 ปีที่แล้ว +1

      This is definitely not an easy pattern to grasp and the notation and implementation is about as clear as mud until it clicks - I found it really helpful to make a video about it ;)
      In all seriousness, the unsubscribing in an disable function is to prevent errors from being thrown. If you don't do that and the object is destroyed an error will be thrown when the delegate/event/action is invoked because the object that is subscribed is null (i.e. destroyed). Using the OnEnable and OnDisable to subscribe and unsubscribe is a way to ensure that when the object is on it's subscribed and vice versa.
      If you have multiobjects (functions actually) subscribed to an event they get called in the order that they subscribed. It is very hard or nearly impossible to control the order that they are subscribed and thus called. I think of a delegate as a list of functions. When an function is subscribed it gets added to the list. When the delegate is invoked it simply goes down the list and calls each function in the list - I envision a for or foreach loop iterating through the list of functions.
      Lastly, on the critter script the reason the "critter kill" action is invoked in the OnDisable is because when the critter is killed it's turned off. So invoking the action there is the "safest" way to ensure that it gets called when the critter dies. It could also be called by the object doing the killing, but for me that's not a super clean implementation. This way no matter how the critter "dies" or is turned off the action is invoked.
      Hope that helps a bit :)

    • @dissonance650
      @dissonance650 3 ปีที่แล้ว

      @@OneWheelStudio Thanks a lot definitely helps a lot it cleared out lots of questions I had, still confused about subscribing and who is doing the listening but that's on me :). also one last question for the last part of the video you declare the critter kill action in critterdisable class, wouldn't it be better approach to declare it from the showscore class (since its managing the UI related stuff) and subscribe to that event from the critter because lets say we have other type of enemies to kill wouldn't it create more separate actions to subscribe for Showscore class?

  • @AraragiAriel
    @AraragiAriel ปีที่แล้ว +1

    Hey, wanted to say that this video has been very helpful for me so far, I've been getting back to this video frequently
    Now, recently I noticed something about actions which I think might have been a big misunderstanding on my part. Whenever I heard that they helped in decoupling the code I thought that meant that if I called an action on a class, eventual errors on other classes that were listening to the action would not pass on to the class calling the action, but I see now this is not the case. In my game I have a static action called by the enemies when they die, and the UI is listening to this. What happened is that when there was an error on the UI itself when trying to execute on the listened action, this error would propagate back to the enemy, which in turn would not die because it got an error before the destroy gameobject line.
    So I wanted to ask, is there an implementation for the observer pattern which stops the error from propagating back?

    • @OneWheelStudio
      @OneWheelStudio  ปีที่แล้ว

      I'd want to see the code! An error in the listener should not cause the event sender to stop functioning. Unless the event sender is also dependent on the listener. Which is definitely an issue and sounds like a problem.
      Basically, events should be "fire and forget" meaning whoever is invoking the event doesn't care if anyone is listening. If that's not true then I'd argue it may not actually be the "observer pattern."
      Happy to take a look at the code. Jump over to the OWS discord (link in the description) and ping me.

    • @ShackMan
      @ShackMan ปีที่แล้ว

      I think your code execution simply stops because of the error, so it doesn't continue with the enemy. Sounds simple but you need to make sure there are no errors in your UI (or anywhere else). You can use things like null checks (with debug warnings) to account for unexpected behavior so at least it doesn't break the code execution.

  • @richardrothkugel8131
    @richardrothkugel8131 ปีที่แล้ว

    I've been working on a game for a year and the coupling has gone out of hand. I've been trying to understand the observer pattern for a few months and I think the critical issue for me was public static variables. I have a CS degree and they drilled into us never to use public or static variables. My project references every other class directly, which has turned into a nightmare for super classes like the game controller or UI.
    Your video really helped me understand actions and events. I'm going to rewrite my project with this pattern.

  • @gustorvo
    @gustorvo 3 ปีที่แล้ว +2

    Great explanation, I've been using observer pattern quite often now...but I still suffer from understanding one simple thing: what is the actual difference between writing "public static event Action OnThisThingHappen;" and "public static Action OnThisThingHappen;" The last one is without "event" keyword. I use both and cannot se the difference... What's the practical benefit of using "event" in this specific case? (given that there is already an "Action" which does the same thing as for me)

    • @OneWheelStudio
      @OneWheelStudio  3 ปีที่แล้ว +2

      The keyword "event" puts restrictions on what other classes can do with the action. Without the event keyword other classes could invoke the action - which I generally don't like. Also other classes can assign a function, not just add/subscribe a function, to the delegate this would effectively unsubscribe all other functions. Again, not something I want to have happen.

    • @gustorvo
      @gustorvo 3 ปีที่แล้ว +1

      @@OneWheelStudio Great explanation which certainly makes sense!

  • @DorinicM
    @DorinicM 3 ปีที่แล้ว +1

    Hey great video! I'm curious, is this patter possible with Bolt?

    • @OneWheelStudio
      @OneWheelStudio  3 ปีที่แล้ว +2

      Yes it is! It’s less baked-in than with C# and would require lots more framework to be built. Bolt 1 events work differently than C# events... I do have a couple videos on Bolt events and even return values which use events. 😀 With Bolt 2 this pattern will be much easier and I’m intending to cover it once Bolt 2 is public.

    • @DorinicM
      @DorinicM 3 ปีที่แล้ว

      @@OneWheelStudio I think I watched those awhile back, but I'll check them out again. Yeah, bolt 1 events need a object to call events on, so I haven't worked out how to build a framework for this pattern. I would really like to use it though!

    • @StefanLopuszanski
      @StefanLopuszanski 3 ปีที่แล้ว

      @@OneWheelStudio : Ah, I thought Bolt 2 was public already, but it is only the Alpha that is out. Definitely curious to check this all out once that launches. Have been avoiding learning Bolt because of how different it will be with Bolt 2 from everything I've seen.

    • @OneWheelStudio
      @OneWheelStudio  3 ปีที่แล้ว

      It was and then when Unity bought it they closed the alpha. It’ll be a while until it has a production ready release, but I’m hoping a open alpha/beta will happen soon.

    • @StefanLopuszanski
      @StefanLopuszanski 3 ปีที่แล้ว

      @@OneWheelStudio : Gotcha. The closed alpha is going on and they are still letting people into it. I've considered checking out the alpha but don't have as much time as I'd like to invest into it.