Unreal Property System? some questions

Hi. I would like to get some deeper understanding for Unreal Property System.
During reading article

i suddenly encountered this moment which i tottaly dont understand

“You can get the UClass or UScriptStruct for a reflected C++ type by writing UTypeName::StaticClass() or FTypeName::StaticStruct(), and you can get the type for a UObject instance using Instance->GetClass() (it’s not possible to get the type of a struct instance since there is no common base class or required storage for structs).”

Can someone give me some example ?
Beacuse from what i understood before any created class i should put UCLASS() macro but what about above mentioned situation?
Thanks

Randomly stumbled upon this question while browsing the wiki for more information on the property system.

The quote simply means that it’s possible to call UObject::GetClass() on any given UObject instance, but not on any F instance because there simply is no class from which all structs derive, unlike the UObject class for all objects.

Example:

// Assigned somewhere in your code
AActor MyActor;
// Get a pointer to the AMyActorClass UClass instance at runtime.
UClass *MyActorClass = AMyActorClass::StaticClass();

// For the sake of this example, the following code SHOULD work (not sure right now,
// haven't tested), even though there is a method UObject::IsA() which does the same.

if( UClass::FindCommonBase(MyActor->GetClass(), MyActorClass) == MyActorClass ) {
    // Print a debug message for 10 seconds on screen. For more info, google the function.
    GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::Yellow,
        // This is just the text we want to display.
        FString(TEXT("My actor is an instance of AMyActorClass"))
    );
}

// This will not work because the `GetClass()` method is defined nowhere within the Unreal Engine:
FColor::Yellow->GetClass();

The above code should attempt to find the first common base between the two given classes, namely the class of the given AActor instance and a custom class AMyActorClass. If the provided actor indeed is an instance of AMyActorClass the check should return true and print an onscreen message. If it is not, nothing should happen.

However, the last line will result in an error because the symbol GetClass is undefined. It is neither defined in FColor nor in any of its parent structs - in fact, FColor does not derive any struct at all. I found a nice little complementary article on garbage collection here. It also explains the difference between structs and objects in UE4.

I’m by far not proficient with the Unreal Property System and frankly I’m a bit confused about the FTypeName::StaticClass part - usually structs or unreflected classes are prefixed with F meaning they are more or less out of scope of the property system. Thus FTypeName::StaticClass, as for my understanding, usually is an undefined symbol as well.

Once you have access to a UClass, you can use TFieldIterator It(MyClass) to iterate over the reflected fields of your class. This would be useful for custom serialization where you want all reflected properties to be somehow saved to a file, for example.

Hi Newest…
Thanks for effor answering this question. But Could comment every line in your code comment. I mean for example
UClass *MyActorClass = AMyActorClass::StaticClass();
What does it mean? Why you create pointer?.. What is StaticClass() function?

There are several reasons, but the most evident one is simply because that’s the method’s signature. UObject::StaticClass() returns a pointer to a UClass object. We are not creating the pointer, we are receiving a pointer.

That in turn, however, has to do with the inner workings of the Unreal Engine. In order for UE4 to properly manage memory for us we avoid directly interacting with memory. Thus if we were to create a new instance of the UClass class and copy the values that UE4 has stored internally over to that new class we’d have a potential memory leak since UE4 has no way of knowing what you’re doing with that instance afterwards. For example you could store it elsewhere and forget to free it. Do that ten thousand times and you have significant memory loss.

The UObject::StaticClass() method is defined in the UObject class and inherited from every class with the prefix U or A. The class simply returns a pointer to the UClass instance which represents the class on which the statc method was invoked on at runtime. In this example, AMyActorClass::StaticClass() returns a pointer to a UClass which represents and stores information on the AMyActorClass at runtime.

Hope this helps?