Course contentsShow
← Unreal Engine Game Development
Lesson 224 of 8803. C++ and UObject ArchitectureFree lesson

Building Custom Editor Tools with Reflection Metadata

Students will use the reflection system to iterate over object properties at runtime, enabling the creation of automated debugging and data inspection tools.

In this lesson, you will learn how to use Unreal’s reflection system to programmatically inspect and modify object data, allowing you to build your own custom debugging tools.

The Power of Reflection

Reflection is Unreal Engine's ability to "look at itself" while the game is running. Every class derived from UObject that you’ve marked with UCLASS() and UPROPERTY() is indexed by the engine. Instead of manually writing UI code for every single variable in your game, you can use the reflection system to discover those properties at runtime and iterate through them automatically.

How to Iterate Over Properties

To inspect an object's properties, you need two things: a pointer to the UObject and its UClass. You then use TFieldIterator to traverse the property list.

Here is a simple example of a function that logs every property name and its value for a given object:

void LogObjectProperties(UObject* TargetObject)
{
    if (!TargetObject) return;

    // Use TFieldIterator to loop through all UProperties in the class
    for (TFieldIterator<FProperty> It(TargetObject->GetClass()); It; ++It)
    {
        FProperty* Property = *It;
        FString Name = Property->GetName();
        
        // Get a pointer to the actual data stored in the object
        void* ValuePtr = Property->ContainerPtrToValuePtr<void>(TargetObject);
        
        // We can use ExportText to get a string representation of the data
        FString Value;
        Property->ExportTextItem(Value, ValuePtr, nullptr, nullptr, PPF_None);

        UE_LOG(LogTemp, Log, TEXT("Property: %s | Value: %s"), *Name, *Value);
    }
}

Why This Matters for Editor Tools

Because this code works for any UObject, you can create a "Universal Debug Inspector" tool. Instead of creating a new Slate window every time you add a variable to a character, you can write one utility that detects your properties, builds the UI dynamically based on the metadata, and allows you to live-edit values while playing in the Editor. This dramatically reduces the time spent building UI boilerplate and ensures your tools stay in sync with your code.

Key Takeaway: By using TFieldIterator and FProperty, you can transform static C++ classes into dynamic, self-inspecting objects that allow for automated, data-driven debugging tools.