I know that UCLASS() macro creates a seperate UClass for every UObject class ,but what is the need for this seperate class , how both of these classes relate to each other and what is the basic difference between both of them??


In Unreal Engine the base class used for every reference counted or garbage collected object is UObject. UObject is the base for classes like UActorComponent, AActor, APawn, AGameMode, etc. almost every single class in the entire engine.

UClass is also a UObject based class. UClass is a class that is used for meta/reflection.

Meta/reflection allows you to ask questions at runtime about the types in your project. What do I mean by types? I mean classes. A simple question might be. "What properties does this class have?" or "What methods does this class have?" A very helpful part of reflection is the ability to build very generic editors because all of the properties can be broken down into string entries, numeric fields, checkboxes, asset selectors, etc...

There are many classes used in Unreal's reflection system. UStruct, UField, UProperty, UEnum, UFunction, etc... Each one of these types maps to a different queryable attribute that a UObject based class can contain. As an aside you can also query meta/reflection on structs as well but only properties.

Meta/reflection is a common part of larger game engines but is not built into C++ natively. Some programming languages build that meta-data and reflection capabilities in, C# and Java both come to mind.

Because it isn't built into C++ natively game engines need to find a way of reflecting that data and making sure users know which data is reflected in an easy way. The unreal devs decided that marking your class with a syntax like UCLASS() and your methods with UFUNCTION() and properties with UPROPERTY() was a great way of explicitely defining which data/methods is going to be reflected.

Then during the compilation process they pre-process your header file and generate a new header file "yourfilename.generated.h" which includes all of the necessary reflection data that you marked up. They then run the rest of the compilation process and once your binaries/dlls are built they include reflection information, neat!

Here's a link to Unreal's article on reflection info. Unreal Property System (Reflection)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.