Why does UPROPERTY cause a compile error?

I have a custom class that inherits from nothing.

In my game mode I have an array of this class and it compiles fine:

TArray< CharacterClass* > Party;

I want to add a UPROPERTY like so:

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "GameCharacters")
TArray< CharacterClass* > Party;

When I try to compile this I get the error:

In RPGSimulatorGameMode: Unrecognized
type ‘CharacterClass’

Edit: Further Details
This class was created by using the Add Class feature with no base class.

I’m using the UPROPERTY in my Game Mode, it has other UPROPERTY’s defined that work:

class ARPGSimulatorGameMode : public AGameMode

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = GameState)
	TEnumAsByte<EGameState::Type> GameStateEnum;

	//UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "GameCharacters") // the broken UPROPERTY
	TArray< CharacterClass* > Party;

	UFUNCTION(BlueprintCallable, Category = "GameCharacters")
	void CreateChar(FString name, uint32 maxhealth);

	virtual void StartPlay() override;

I added further details above.

Yes, it compiles when I comment out the UPROPERTY, breaks when I uncomment it.

The compiler does not recognize UPROPERTY. Since UPROPERTY is not a native C++ class. Something needs to tell the compiler how to handle that class. I am not sure how you went about creating this custom class but I am going to assume you simply added a new .CPP and . h file to your project.

You don’t want to do it that way, exciude those filef from your project then delete them. Lauch the UE4 editor. Once inside the UE4 editor click “file”. From the drop down select “add code to project”. It will then ask you to select a parent class, I would recommend selecting “UObject” based on what you are trying to do. Then it will ask you to name the class, so call it what ever you want.

Doing it this way, the editor will add all necessary files behind the scene so that the compiler knows how to read and understand teh custom UE4 classes.

Does you game mode have the #include CharacterClass.h at the top of the file?

Do you have #include “yourfilename.generated.h” in your header file? Its needed to tell UHT about your file and include macros … and it should be last include statement

Do you have UCLASS() before your class declaration? Its also needed

Heres an example


OK, here’s my recommendation for anyone that’s just trying to create a generic C++ class to be used as a data structure.

UE4 has a reflection system that lets you decorate your various variables and functions with UPROPERTY and UFUNCTION. Plain-Jane classes can’t use this reflection system as the base requirement is that you inherit from UObject.

So the easiest way forward is instead of creating a flat class with no inheritance, inherit from UObject. The 2 caveats to note here are:

  1. You won’t be able to set custom constructor parameters (use an init function instead and pass your parameters post object creation).
  2. Instead of using new/delete c++ operators, you’ll need to instantiate UObject’s properly with NewObject.

Thats what I originally told you

Your assumptions were wrong and you weren’t specific enough.