Cannot Forward Declare for a TSubClassOf or for a UPROPERTY() Member. How Can I Have Co-Dependency?

So in cases like this, I would normally forward declare the classes I’ll be referencing in a struct.h file, and then include struct.h with the classes that use it, then avoid Infinite Recursion with the .cpp file.

However, I’ve hit 2 major problems.

  1. TSubclassOf<template> cannot accept forward declarations.
  2. UPROPERTY() cannot accept forward declarations of structs (won’t recognize it as a USTRUCT())

The won’t as far as I know that is. If there is a way to make either of these accept the forward declaration, then problem solved.

[HR][/HR]
[SUP]struct.h[/SUP]


USTRUCT(BlueprintType)
struct FItemStruct
{
    GENERATED_BODY()

    UPROPERTY(BlueprintReadWrite, Category = "Inventory")
        TSubclassOf<AItem> myItem; //ERROR IF: I forward declare AItem

};

[SUP]item.h[/SUP]


struct FItemStruct;

UCLASS()
class MYPROJECT2_API AItem : public AActor
{
    GENERATED_BODY()

public:    
    AItem();


protected:
    //UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Item")
        FItemStruct *itemDetails; //Cannot make UPROPERTY() from a forward declared struct

};



[HR][/HR]
I absolutely must have the TSubclassOf<template> (unless there is an exact equivalent) in the struct, and it would be ideal to have itemDetails be a UPROPERTY() for the designers to edit the information cleanly.

Is there a way to make either of these accept the forward declaration? If not, is there a way to get the same desired behavior?

In case like that, because we have to deal with different compilers for multiple platforms, I create AItem_base to use it on both the Struct and as a base class for AItem.
Later I add templated auto casting routines for algorithms that must be members of the AItem class.

I’ve run into this quite often.

Although it seems counter-intuitive, I’ve found the forward declaration often works when including the SubclassOf.h header. Still not certain as to why.



#include "Templates/SubclassOf.h"

class USomeType;


You may know already, but if not - you can only forward-declare pointers to types.

  • Make sure you’ve included CoreMinimal.h.
  • Forward declarations in TSubclassOf works completely fine as long as you do not initialize it in the header.
  • You can’t make a UPROPERTY to a struct pointer, and you can’t forward declare a struct property (as it would not know the size).

I have CoreMinimal.h included. You cannot create a pointer for TSubclassOf, so how are you getting it to completely work?


class AItem;

USTRUCT(BlueprintType)
struct FItemStruct
{
    GENERATED_BODY()

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Inventory")
        TSubclassOf<AItem> myItem; //Error, does not compile.   Cannot make it pointer either, error for being a pointer
};

[HR][/HR]

Omgosh duh… I forgot I could do that, Its been way too long since I’ve had to. Thanks! (This is my first time on these forums, idk how to, if I even can, accept your response as the answer)
[HR][/HR]

Thank you, that’s very good to know! I’ll certainly keep that in mind for future reference.

Hello, can you explain me what exactly you are saying? I am stuck in similar problem now. I want 2 Pawns to assess each other, call functions on eachother, gives me ability to choose another pawn in details panel etc. Can you please help me?

Just ran into another iteration of this:
TSubclassOf works fine in arrays and variables but when being used in a TMap as the key, VS compiler throws an error.