Give non code example for use of delegate

I am trying to understand why I would use delegates.

Please do NOT point to documentation about HOW to use them or that other post with the same question and the useless answer.

I do NOT want code examples and I do NOT want any kind of documentation.

What I’d like to get:

  • The most basic situations you can imagine where a Delegate would be useful.

The reason I am asking this is that I just tried to learn how they work, I think I learned it, then I made an example or two and every time I use “ExecuteIfBound” or “Broadcast” I think:
“Why would I do it this way? Why not call the method directly?”

I am pretty sure there are very good reasons for the use of delegates, I just don’t get it currently.

So, in which cases and why would you accept the overhead from delegates?
Where is this “decoupling” useful? Or even better, are there cases where I HAVE to be able to do it?

1 Like

Great question, one reason you may want to use delegates rather that Directly calling a method would be the system which implements the Delegate doesn’t need to know about any other system involved.

Such as… a health system, in which the Health value feeds into other systems such as a UI (update
health bar), Audio (play ‘takedamage’ sound), animation (initiate ‘takedamage’ animation). The health system would have to have a “Direct Reference” to each system it wanted to pass that information to. If you then say… changed one of the subsequent systems such as… UI you’d need to go and adjust your code/BP to accommodate that change.
With a delegate, you’d need to know nothing about t he other systems, instead, the other systems would bind to the health system at runtime. Additionally, multiple systems can ‘Listen’ to the event simultaneously.

In blueprint, you could create and Event Dispatcher that can make use of “Delay” and “AddTimeline” nodes, however, you couldn’t do this with a Function you create in blueprint since these nodes are “Latent”.

this would keep you code flexible so you wouldn’t have to modify your code as much as the project grows in complexity, rather than strickly referencing everything involved in a single process.

1 Like

Arrrggg!
Yes!

Thank you.
This is the kind of answer I needed.

I was stuck in my head with the examples I read and did and it just did not make sense.

But just the idea of a “health system” and using it in different situations and perhaps not even knowing during development where the info will be used later and how many classes need the information that health has changed and … [imagine a wall of text here] :slight_smile:

Yes. NOW I can think about it in a useful way.

I still hope I get more answers!

Oehm. This is … kind of weird.
Next chapter in the book was “Custom Events”.

Now I am thinking… yeah… I do get “Custom Events” and they sound super helpful and all and I would probably use custom events in a health system and so…
You see where I am going…?

Back to the original question? Kind of?
I mean, I can’t - currently - imagine a health system where I’d need delegates when custom events do the same thing in a much easier way.

I DO know I am missing something, because no sane developer would use an unnecessary difficult solution, so, … this is why it is weird and I am not sure my repeated question now sounds a bit stupid…
Why use delegates?

The only advantage of a delegate over a custom event is the decoupling of signal location and the object which signals. Right? If not, please tell.
But even that can be easily done even with events.

I am missing something. Again. :frowning:

1 Like

Nah, your mode of thinking is correct; it could be helpful to know what each one is more specifically:

Delegates are a way to define callback functions that can be bound and called dynamically. They are essentially function pointers that can be assigned and triggered to call bound functions in C++.

Event Dispatchers are a higher-level concept built on top of delegates, specifically designed to work with Blueprints. They provide an easy way to declare and bind to events within Blueprint graph.

if you’re not concerned with diving into the C++ then you may be having a difficult time with the two parts because you aren’t implementing it that way (code), so it would seem a bit convoluted. Instead, you’d make use of Event Dispatchers within the editor.

1 Like

Thank you.
I’ll bookmark this answer and come back to it in some days.
Currently I have read it 3 times and I am thinking I should either take a pause or come back when I am smarter :slight_smile:

So, in practicality, the health thing can just call “UpdateHealth” and each thing that can respond to that event, will just-do-so?

Metaphorically, one calls out a command to all the actors/performers in the room, “Hamlet is dead!!”, and only the ones with ‘lines/drama’ to play on that queue/event, do so?

That is an amazing analogy, and is correct.

1 Like

It takes an idiot… :smiley: Thanks for the clarification & validation. I did learn; thx.