Resolving Circular Dependency

Can anyone help me understand how this works? I am trying to implement the LastTakeHitInfo feature in my game. I was looking at the ShooterGame example to see how they did it. I am running into a circular dependency issue and I am not sure why the Shoot Game works and mine doesn’t.

ArenaCharacter.cpp

#include "ArenaTypes.h"
#include "ArenaDamageType.h"
#include "GameFramework/Character.h"
#include "ArenaCharacter.generated.h"

UCLASS(Abstract)
class AArenaCharacter : public ACharacter
{
	GENERATED_UCLASS_BODY()

    (...)

    /** Replicate where this pawn was last hit and damaged */
	UPROPERTY(Transient, ReplicatedUsing = OnRep_LastTakeHitInfo)
	struct FTakeHitInfo LastTakeHitInfo;

    (...)

}

Because ArenaCharacter implements “struct FTakeHitInfo LastTakeHitInfo” it needs to include #include “ArenaTypes.h”. There is a problem though

ArenaTypes.h

#include "Object.h"
#include "ArenaCharacter.h"
#include "GameFramework/DamageType.h"
#include "ArenaTypes.generated.h"

#pragma once

USTRUCT()
struct FTakeHitInfo
{
	GENERATED_USTRUCT_BODY()

        (...)

        /** Who hit us */
	    UPROPERTY()
	    TWeakObjectPtr<class AArenaCharacter> PawnInstigator;

        (...)

}

The pawn investigator needs to know about AArenaCharacter. If I don’t include #include “ArenaCharacter.h” I get the error: TWeakObjectPtr can only be constructed with Object types. But obviously when I add #include “ArenaCharacter.h” header it creates a circular dependency. This is how the Shoot game works. What am I missing?

Hi Ky,

Do you really have AArenaCharacter defined in ArenaCharacter.cpp file, as you have suggested? Any marked-up (i.e. UCLASS, USTRUCT etc.) types must be defined in a .h file of the same name as the type, but without the prefix - i.e. ArenaCharacter.h in your case - and in a Public, Private or Classes folder located underneath your module folder.

Steve

I figured it out. In the ShooterGame example in the ShooterGame.h they #include a header file called ShooterGameClasses.h this file contained a #include to every class in the project. Its is actually auto generated by the Unreal Engine. I looked in my project and found I too have a MyProjectClasses.h however mine was empty. You can’t add anything to that file since it is auto generated and will delete anything you add to it. I instead added all the includes into MyProject.h then I simple #include MyProject.h for every classes. This will insure no circular dependencies are created even when two classes refence each other.

2 Likes

Yes it is defined, I actually just figured out what the problem was, thanks though!

For me it also worked to set the default value in the constructor in the .cpp instead of directly in the .h file.

thank you so much.
Worked for me :slight_smile:

thank you so much.
Worked for me :slight_smile:

Hello, can you please help with that? I tried to use all includes in my project.h file and then I used include project.h in both header files. Can you please explain me with the example. Please help me out with this, I am stuck here as I cannot use forward declaration because I have to store reference of each other for further use.