Are delegates a good practice?

Hello, I am doing a multiplayer project and I wanted to ask two questions maybe I can get it more clear.

First question is that I created an actor component for Character Stats, is a component that have all stats variables replicated, some with rep notify like health.

Im trying to keep in track in the UI the variables, because I have a menu where you can see all the stats and upgrade them. But right now I am getting the owner (PlayerController), then getting the Pawn and from the pawn the StatsComponent to set the variables in the interface when rep notify occurs.

Then I discover that Unreal Documentation says that you should use delegates but Im scared of delegates because I think they will kill the performance in the project.

Could you give me some advices about this?

The second question is almost the same.
I have a combat system like Baldurs Gate, where you are in Exploration mode and when an enemy see you, you and that enemy enters in combat.

But that logic I have it in the GameMode.
Should I create and Actor with the combat logic so when the combat requeriment meets, the combat start?
Should I also use delegates on this? Like all the characters in the combat subscribe to a delegate, so when a turn ends everyone get the call?

I really appreciate any advice.
Thank you everyone.

Absolutely use delegates
There are thousands of delegates across most engine’s classes, and the ability system uses them for everything as well, including attribute changes

For the second question, remember that the game mode doesn’t exist on clients, so no replication or RPCs can happen, so you need another actor to handle that

I would probably have combat actors, yes, and would use delegates fired by replicated properties notifies to update UI

Thank you for the answer, Im gonna put it as solution but, I would like to ask you one more thing before.

Let me put an example of my project.

As I have one widget and I switch between characters, possessing them.
I have a Health variable that replicates so when the Health change I have a delegate that sends Health and MaxHealth.

So in my widget I have a Health Bar and a method that subscribe to that delegate in the constructor.

The problem: The widged had subscribe to that delegate of that character, so when I switch pawn Im still receiving the Health changes of the first pawn.

  • Would be a good practise subscribing and unsubscribing to the delegate in the widget?
  • Is better if I just destroy and recreate the widget every time I switch pawn?
  • Should I stop using in that case delegates and just be casting to the pawn class and get the Health instead?

Thank you so much for your answer.

It is a good idea to unbind any events that you’re not interested in, yes

I’m guessing you have bound to your controllers on pawn changed delegate to initialize your widget, so that should be really easy to unbind the events from the old pawn (which comes with the delegate)

Regarding destroying and recreating the widget, if you can get it to work without issues when you switch pawns, then I don’t see why you would want to recreate it

If you were to check the health instead of using the delegate, you would have to do that on tick, which seems a bit unnecessary for a large amount of time
I would 100% use a delegate for updates like this, and keep tick for visual updates or interpolations

Hey @zeaf I am really thank you for your answers.

I got a new solution for the possessing thing.
I was making a client RPC OnPossess and OnUnpossess but I found that Pawn has a overridable method that is (virtual void NotifyRestarted() override;)

This method executes when pawn is possess in client after the client has possess it…

I have been looking for that method for a long time…

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.