Download

Resolving circulatory dependencies

I have this particular issue where I want to reference my anim instance class in my character class and my character class in my anim instance class. This results in a circulatory dependency where both classes rely on the other and spit out the “missing type specifier” errors. The files:

MyAnimInstance.h



#include "MyCharacter.h" // <- This is one half of the problem

class CPPCHARACTERMOVEMENT_API UMyAnimInstance: public UAnimInstance
{
    GENERATED_BODY()

protected:
    UFUNCTION(BlueprintCallable, Category = "InitialiseAnimation")
    void InitAnimation();

private:
    UPROPERTY()
    const AMyCharacter* MyCharacterReference;
}



MyAnimInstance.cpp



#include "MyAnimInstance.h"

void UMyAnimInstance::InitAnimation()
{
    // During init animation, it is asserted that the controlled pawn indeed is AALSCharacter
    MyCharacterReference = Cast<AMyCharacter>(TryGetPawnOwner());
    check(MyCharacterReference)
}


MyCharacter.h



#include "MyAnimInstance.h" // <- This is the other half of the problem

class CPPCHARACTERMOVEMENT_API AMyCharacter: public ACharacter
{
    GENERATED_BODY()

protected:
    virtual void PostInitProperties() override;

    UPROPERTY()
    const UMyAnimInstance* MyAnimInstanceReference;
}



MyCharacter.cpp



#include "MyCharacter.h"

void AMyCharacter::PostInitProperties()
{
    MyAnimInstanceReference = Cast<UMyAnimInstance>(GetMesh()->GetAnimInstance());
    check(MyAnimInstanceReference)
}



Is there are graceful and/or inbuilt way of handling circular dependencies in Unreal? What is the best or correct way of resolving the issue where two classes rely on each other?

Forward declare character class into anim Instance class, or use a character base class.

// AA.h
#include “AA.generated.h”
class BB; <==

//AA.cpp
#include “BB.h” <==
#include “AA.h”

//BB.h
#include “AA.h” <==
#include “BB.generated.h”