How do I make my class inherit UObject?

Hi, I`m trying to import a class but I have a built error with it.

Here is the class code:

UCLASS()

class UFloatintPawnMovement;

(It wouldnt work if I wont declare it as an UCLASS)

And here is the error:

Error Class ‘UFloatintPawnMovement’ must inherit UObject or a UObject-derived class

thx for the help.

Hello! If you want just a forward declaration then remove UCLASS() macro and that’s it. For more precise answer more code of your class is needed…

I use the follow code and works fine for me.

UObject derivated Header File

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "UObject/NoExportTypes.h"
#include "BaseCharacterData.generated.h"

UCLASS()
class BP_PLAYERTEST_API UBaseCharacterData : public UObject
{
	GENERATED_BODY()
public:

	UBaseCharacterData();	
	~UBaseCharacterData();

        // get internal count of all Objects (not really needed) 
	static int32 GetCount();

private:
	// internal object count
	static int32 iCount;

public:
	// last seen Actor (Targets)
	UPROPERTY(Transient)
		class AActor* LastOverlappedActor;

	float Speed;
};

CPP File:

// Fill out your copyright notice in the Description page of Project Settings.

#include "BaseCharacterData.h"

UBaseCharacterData::UBaseCharacterData()
{
	UBaseCharacterData::iCount++;
	UE_LOG(LogTemp, Display, TEXT("BaseCharacterData created, number: %d"), UBaseCharacterData::iCount);

	LastOverlappedActor = nullptr;

	// Default-Speed 
	Speed = 600.f;
}

UBaseCharacterData::~UBaseCharacterData()
{
	iCount --;
	UE_LOG(LogTemp, Warning, TEXT("UBaseCharacterData destroyed!: %d"), UBaseCharacterData::iCount);
}

int32 UBaseCharacterData::GetCount()
{
	return iCount;
}

// init global object count
int32 UBaseCharacterData::iCount = 0;

Example instanzing → in BaseCharacter.h

...
	UPROPERTY(Transient)
		class UBaseCharacterData* ObjectData;
...

CPP File → In BeginPlay…

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

	GEngine->SetMaxFPS(60.f);

	// Create Data Object
	ObjectData = NewObject<UBaseCharacterData>(this);
...

And dont forget #include in all instanzing classes…

1 Like

Maybe you could generate new class in the UE4 Editor. It will do the class declaration for you.

For manually declaring a new class inheriting UObject, some additional things are required:

// FPMovement.h
#pragma once

/* "CoreMinimal.h" has the basic functions of UE4. Almostly always needed. */
#include "CoreMinimal.h"  
/* "xxx.generated.h" is always needed as the LAST included file. */
#include "FPMovement.generated.h"

UCLASS()
/*
* For a UClass, you need to add "XXX_API" macro between "class" and your class name. XXX is usually your project name.
* If you don't know what the macro is, just generate a class in the UE4 Editor.
* Also, all UClass inherit UObject as the error says, so write like this:
*/
class XXX_API UFPMovement : public UObject {

	// UClass always requires a "GENERATED_BODY()".
	GENERATED_BODY()

public:

}; 

Before getting familiar with UClass declaration, it’s recommended to add new C++ classes and structs in the Editor.