Question about UClass

So I am kind of new to Unreal, but what I understand is that you need to define an object as UPROPERTY() to allow saving / restoring.

My class is a container class that has a lot of functions including and few values. I need to be able to save / restore it and have it as a variable of the player entity. I don’t need it to show up in the editor. I also have another class that is info about the people in the game world. I need to have a TArray of it inside the data container class and it also needs to be saved and restored.

So what I have understood is I should use UPROPERTY() and UCLASS() but for those I need to use the UClass as the base class. I am not sure if UObject is the right, because it’s a container class of info, not an actual object in the physical world.


UCLASS()
class I_AM_HUMAN_API UPersonInfo : public UClass
{
public:
	GENERATED_UCLASS_BODY()

	...
};

I have included “PersonInfo.generated.h” into the header file.

But when I try to compile:


C:\Program Files (x86)\Epic Games\4.10\Engine\Source\Runtime\Core\Public\Templates\MemoryOps.h(95): error C2248: 'UPersonInfo::UPersonInfo': cannot access private member declared in class 'UPersonInfo'

I have tried adding


UPersonInfo(const class FObjectInitializer& ObjectInitializer = FObjectInitializer::Get());

but it complains that the default parameter is already defined. I have also tried making every variable in the class public and adding and removing the default constructor but nothing seems to work.

1 Like

subclass not from UClass, but UObject
UClass is for presenting type metadata.

What he means by presenting type metadata is that you can describe to UE4 information about your class.

One of the easiest examples of this is Blueprintable/NotBlueprintable. If you have a UCLASS(Blueprintable), then that will make the class show up (or not) when you try to make a blueprint class and choose a parent for it.

Now, it seems that you are struggling to find a parent class for the class you are creating.

I would look at some of the classes in ue4 and follow their hierarchy. This will be a quick way of learning what certain ue4 classes inherit from.

For Example,

But doesn’t UObject refer to something physical that exists in the game world? I come from Source Engine programming and I would almost understand UObject as an Entity. Source had limited amount of Entities and I wouldn’t want to make your container class an Entity for no reason. The container class or the PersonInfo class should have no physical representation either, it should be just a “cheap” class containing some info and have some functions to help organize the info.

I can create a struct with USTRUCT() but I am having so much trouble figuring out how I can create a class that has UCLASS(). I tried looking up some examples of classes that use UClass as base class in the solution, but only found these two examples:



UCLASS()
class ENGINE_API UBlueprintGeneratedClass : public UClass
{
	GENERATED_UCLASS_BODY()

public:

	...
};


class ULinkerPlaceholderClass : public UClass, public TLinkerImportPlaceholder<UClass>
{
public:
	DECLARE_CASTED_CLASS_INTRINSIC_NO_CTOR(ULinkerPlaceholderClass, UClass, /*TStaticFlags =*/0, CoreUObject, /*TStaticCastFlags =*/0, NO_API)

	ULinkerPlaceholderClass(const FObjectInitializer& ObjectInitializer = FObjectInitializer::Get());

	...
};

I tried mimicking their class structure, but neither resulted in a successful compile. One time I got it to compile with another class the Unreal editor crashed and would keep crashing until I would remove the Uclass stuff from it.

Again, UCLASS() and USTRUCT() have nothing to do with base classes. This is apparent if you’re familiar with C++ syntax. Keep looking through tutorials and you’ll figure it out.

Before the class declaration starts you can put UCLASS() which I assume is needed if you want to use it as UPROPERTY() (and for saving and restoring). UClass is different from UCLASS(). It is an actual class not a macro.

I think you’re confusing UObjects and Actors. Actors exist in the game world, UObjects do not necessarily. All actors are UObjects, but not all UObjects are actors. (The inheritance chain for actors is UObject -> AActor -> MyActor.)
An example UObject is a sound wave. You can’t place a sound wave in the world (instead you need e.g. a sound cue that uses it), but you can view it in the asset browser. This is possible due to being a UObject based asset that is automatically serialized on load and save. So actually UObject does sound like what you need.

The first one is close to what you want, but not quite. You want to inherit from UObject instead of UClass (and leave out the ENGINE_API as well as that may be causing problems too, it’s not intended or needed for gameplay code).

No, UObject is a base class for all classes, even UClass. subclassing a UObject means you will have metadata and reflection, and other system stuff. It is like System.Object in C# or Java.
Game Entities are AActors and UComponents.

Okay, thanks!

I have it based on UObject and it seems to be working now. Thanks a bunch!