UNREAL ENGINE | EVENT DISPATCHERS (BP) + DELEGATES (C++)

Sdílet
Vložit
  • čas přidán 15. 07. 2024
  • Let's learn about using Event Dispatchers and Delegates in Unreal Engine!
    -----
    Written version: kitatus.github.io/ue4/ue5/eve...
    Project Files:
    github.com/KITATUS/KFEventDis...
    ---
    0:00 Introduction
    0:14 Example #01: Door (Blueprint)
    1:33 Example #02: Coins (C++)
    4:23 Making a Blueprint Event Dispatcher
    4:50 Further Reading

Komentáře • 28

  • @apokrif7315
    @apokrif7315 Před rokem

    Bro, it’s so good!

  • @TheCropDuster2
    @TheCropDuster2 Před 3 měsíci

    Great explanation and video.

  • @leannviolet
    @leannviolet Před rokem +1

    THIS IS AMAZING! I NEED TO KNOW MORE!!!! (Like explain it again and pretend I'm an idiot, granted we don't have to pretend.)

  • @DomensionStudios
    @DomensionStudios Před rokem +1

    This video explains it all so much better than how my "teacher" did

  • @furrybproductions
    @furrybproductions Před 2 lety +1

    Glad to see you back

    • @KITATUS
      @KITATUS  Před 2 lety +2

      Thank-you, it's nice to be back!

    • @furrybproductions
      @furrybproductions Před 2 lety

      @@KITATUS I've got a question: what happened to the Metal Gear Solid videos?

    • @KITATUS
      @KITATUS  Před 2 lety +1

      @@furrybproductions Being remade properly :)

    • @furrybproductions
      @furrybproductions Před 2 lety

      @@KITATUS AWESOME!

  • @deivid-01
    @deivid-01 Před rokem

    thanks a lot!

  • @miraj636
    @miraj636 Před 9 měsíci +1

    My code does not compile. It gives the error cannot 'class' 'delegate' 'enum' or 'struct' with the 'Name of the delegate that I put into the macro before the class)

    • @verminology9999
      @verminology9999 Před 5 měsíci +1

      Did you figure this out? I'm having the same problem.

  • @LaPutiGamba
    @LaPutiGamba Před 7 měsíci

    hiii, sorry for bothering. I have a question. If I have an event that only happen once and only with one object (for an example, a thing that happen when a puzzle it's done but it's exclusive, won't happen again), it's worth it the delegates or it's better to just do a simple function. also, the delegates increases or decreases performance?

    • @KITATUS
      @KITATUS  Před 7 měsíci

      Performance-wise, there's no real difference, it's just one class broadcasting out a messing on a bound variable. For your use case there, if it is literally just this one use-case then a direct function might serve you better but if you plan to do something like "Puzzle complete" - then it may make sense to use the delegate system.

  • @BigRubberDucky
    @BigRubberDucky Před rokem +1

    It looks like you have been inactive for a while, but I hope you can help me understand something. Why use a delegate over a function call? I get that you are looping over the coins and when the delegate happens you call CoinCollected(). Would it not be the same thing to just get a reference to this object (UUW_CoinWidget_Base)and call the function from the coin class?

    • @KITATUS
      @KITATUS  Před rokem +4

      Hey! There are a few big benefits over Delegates instead of direct calls. For one, you don't need to store a reference to the original class - which means when you create / first access the class, you can bind to that callback and not have to directly deal with the class again. An alternative (and what I've been doing recently) is if you have many actors, such as buttons that fire off specific events in the parent class, the parent class and bind to a callback which takes an int or string. You would then use that int or string as an id to know what button was pressed so you can act accordingly meaning once that first bind takes place, you don't need any further knowledge of those classes, they'll tell YOU when they need something.
      Delegates as a whole are a great way to avoid having to cast to a class too, for the reasons listed above - after the first cast, bind you event and away you go!
      If you're only dealing with 5 or 6 versions of an actor, it can be hard to justify a delegate's usefulness, but once you start getting into the upper 10's or 20's, they are a great way of keeping efficiency and good looking code that is still human readable.
      Callback events in particular are agnostic to what is bound to it so technically you can use this method to communicate between multiple classes with one callback assigned to their delegates.
      Hopefully this helps, if you need a more in-depth write-up, please let me know.

    • @BigRubberDucky
      @BigRubberDucky Před rokem

      @@KITATUS That makes perfect sense. I guess it would be annoying to reference the other actors all the time. Kind of feels obvious after reading. Thank you for taking the time to write that!

    • @justinrivera7800
      @justinrivera7800 Před rokem +2

      Basically delegates/events prevent your code from being coupled as your project expands too. Imagine copying a script from one project to another if you did a function call you would HAVE to bring all the dependencies.

  • @GTexperience_Channel
    @GTexperience_Channel Před 10 dny

    How did u get your custom blueprint assignable class to not throw an error?

    • @KITATUS
      @KITATUS  Před 10 dny

      What is the error?

    • @GTexperience_Channel
      @GTexperience_Channel Před 10 dny

      @@KITATUS I fixed it, it wasn't in the header file, but somehow another error referenced the delegate variable as an error.

  • @nomad7317
    @nomad7317 Před 7 měsíci

    Can I bind the delegate in more than one places?
    In my case, the latest one is taking precedence, all communications are going there! Please help!

    • @KITATUS
      @KITATUS  Před 7 měsíci

      Yup! Anything you bind to a delegate should be called. All of them should be called, not just one of them. Are you sure the others are bound in time?

    • @nomad7317
      @nomad7317 Před 7 měsíci

      @@KITATUS hey! Thanks for yoir reply.
      Yes, they're all working now. Turns out, I had bound a delegate to a function in a begin play of a class. But it's begin play was not being called by it's child class! *smh 🤦‍♂️🤦‍♂️
      It was a silly error that kept me up for 2 hours past my bed-time lol.
      Calling parent:begin_play (super) in the child class fixed it!

  • @Quicksymphony
    @Quicksymphony Před rokem +1

    items like DECLARE_DYNAMIC_MULTICAST_DELEGATE_Param don't come up in ue5 as intended. IT would be good to go over each cast type instead of just 1... Please confirm the include file... also the DelegateSignatureImpl

    • @KITATUS
      @KITATUS  Před rokem +1

      Which version of UE5 are you referencing? All of the code from this video and the written version in the description are from UE5. I have not seen the need to include "Delegate/Delegate.h" or the DelegateSignatureImpl in any UE5 version (5.0, 5.1, 5.2 or UE5-Main). MULTICAST delegates are touched on here instead of just DECLARE_DYNAMIC_DELEGATE due to not wanting to confuse a new user to concept of delegates and the fact that MULTICAST is a standard delegate on roids.
      The TL;DR differences between MULTICAST and standard delegate are: DECLARE_DYNAMIC_DELEGATE is used for defining single-cast delegates in Unreal Engine, which means only one function can be bound to the delegate at a time. DECLARE_DYNAMIC_MULTICAST_DELEGATE is used for defining multi-cast delegates, allowing multiple functions to be bound simultaneously, and when the delegate is invoked, all bound functions are executed.
      For most (if not all use cases) Multicast delegates are the preferred method as they give you more power and opportunities than a very binary singular Delegate, especially when dealing with complex systems in games. More bang for the same buck.
      Hope this helps.

    • @Quicksymphony
      @Quicksymphony Před rokem

      ​@@KITATUS 5.0.3 -when calling to Declare_Multicast_Delegate it's fine and this example works. The Dynamic delegates have serialization options. Your explanation of it in terms of normal calls is fine tbh.
      On the documentation page those includes are still listed when using Delegates. The example you showed is fine for the understanding of usage in a generic case. I was interested in why in many cases the normal cast works fine, but param ones mention an error in visual studio.
      I'm calling a few OneParams and Two Params, but it mentioned a definition missing concern. The includes I mentioned show a deprecated section for certain calls with those includes.
      They are listed in the search criteria, but implementation mentions a new conflict.
      I'm cross referencing between this vid, Ben UI topic on Advanced Delegates in C++, the unreal documentation and the Wadstein examples.
      Though I'll try a change on code side to confirm DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam, DECLARE_MULTICAST_DELEGATE_OneParam
      I'll also check the TDelegate usage. Initially your Multicast One Param was commented out and the other call that does work was used. So let me double check some items.