Binding delegates from owner vs in component to prevent coupling

159 Views Asked by At

While researching the topic of Coupling, I still have a little misunderstanding about what counts as less / more Coupling. I will take as an example two options for setting delegates in Unreal Engine C++.

Example One:

We wait for an entrypoint and get the owner of our component. Hoping that this owner is not nullptr, we expose delegates and rejoice if everything is fine and cry if it is still nullptr:

void UCustomCharacterMovementBase::BeginPlay()
{
    Super::BeginPlay();
    
    ACustomCharacter* CustomCharacter = GetOwner<ACustomCharacter>();
    
    if (CustomCharacter == nullptr) return;
    
    CharacterReference = CustomCharacter;

    CustomCharacter->OnJumpKeyStateChanged.AddDynamic(this, &UCustomCharacterMovementBase::ToggleJump);
}

Oh no! Coupling! Now our class depends on certain of our character. Badly.

Example Two:

void ACustomCharacter::BeginPlay()
{
    Super::BeginPlay();

    OnJumpKeyStateChanged.AddDynamic(CustomCharacterMovement, &UCustomCharacterMovementBase::ToggleJump)
}

We are waiting for the entry point of the character and put all our delegates there, now our movement class is independent, but the character class is dependent. In addition, now the functions of the movement can not be private.

The second option seems a little more universal, since now all the preparation of a character class rests on the one who contains it. But now replacing the character's movement component will cause problems, which is also bad.

As I understand it, both of these options are incorrect, and the correct option will be interfaces through which everyone can already implement their own class. Is this true or all of the solutions I have named are not correct? Or is it useless in the context of a game engine to try to get away from Coupling so much?

Thank you so much!

I tried to connect interfaces or make a global delegate, these solutions did not save me from escaping class codependency.

0

There are 0 best solutions below