Seeking a purpose of Delegates!

Hello everyone, I’ve been trying to get my head around delegates for a while, including looking at lots of tutorials etc. More often and not they end up being multicast or dynamic related. I’ll layout the ground work for you with a diagram so you can see what I want to do:

239043-flow.png

There is no reason logically that the gun or the event system should know anything about the announcer object. When I initially heard of delegates I thought they were a clever way of getting across function calls without objects being directly exposed.

Let’s take an example, when the gun jams the announcer will say “Jammed”. I want a delegate to broadcast or call a function inside the announcer that loads the correct soundbase file and plays it.

Here is the delegate in my gun:

239044-delegate.png

Now it’s nothing special, but I’d imagine there is someway to get access to the announcer without directly including it’s header file to the gun.

My main point is, what is the point of using delegates if I need to expose the header of the announcer anyway. I could just capture a reference to it and then call public methods directly. I’m aware this is bad practice but they seem practically equivalent at this point.

How would I go about binding a function in the gun for an announcer function and then receiving that message within my announcer without directly exposing everything?

I’d love someone to really open my eyes to this as I’m sure there is a lot of power here but I’m just not understand how to access it.

You still can do it with delegate without exposing it’s headers.
E.G. you can pass a pointer to the cue file as parameter.

C++ don’t have event system out of the box, at most you got normal function calls which you know that will be called on specific occasion. The weakness of this set up is fact that you can’t detect events in code outside of the object it self, or else called of event sends it also somewhere else, external object can’t get call then other object is destroyed for example. Thats why delegates was made which work like variable and other object (or even object less static function) can register it to the event to receive the call when it occurs.

DECLERE_DELEGATE is declaring delegate type, and by making varable out of it you make point to which everything can subscribe to the event. The multicast allows for multiple functions and object to subscribe to delegate, the normal one does not allow that if you set new function the old one is replaced (do it kind of work like a pointer to function of a object).

Delegates are not messaging system which by your description you seem to expect to be… it’s not system to begin with, it just variable type that can gather function and object pointers and call them out on broadcast call, they require exposure as otherwise nothing can register to delegate.

UE4 have messaging bus system, but i really don’t see it being use widely (atleats in games and there no blueprint exposure at all) and i didn’t use it yet myself, the API reference related to has probably biggest intro documentation i know in API refrence, so read it up if you interested

Thanks for your description. I understand a bit better now. I was just interpreting it’s usage incorrectly. Cheers Shadow for the answer :slight_smile: