In this lesson, you will learn how to look "inside" your classes while the game is running to make logic decisions based on the metadata you defined in your headers.
Why Query Metadata at Runtime?
When you define a UCLASS or UPROPERTY, you often add metadata specifiers like Meta = (ToolTip = "...", Category = "Combat"). Usually, these are just for the editor, but you can actually query these strings at runtime to drive gameplay. This allows you to write "data-driven" code where the behavior of an object changes based on how you tagged it in the code.
Accessing the Metadata
Every UObject has a UClass associated with it. This class object holds the reflection data for your type. To inspect a property's metadata, you use the UProperty (specifically FProperty in modern Unreal) associated with that class.
Here is how you check if a specific property has a custom metadata tag:
void AMyActor::CheckPropertyMetadata(FName PropertyName)
{
// 1. Get the class of the current actor
UClass* MyClass = GetClass();
// 2. Find the property by name
FProperty* Property = MyClass->FindPropertyByName(PropertyName);
if (Property)
{
// 3. Query the metadata string for a specific key
if (Property->HasMetaData("MyCustomTag"))
{
FString Value = Property->GetMetaData("MyCustomTag");
UE_LOG(LogTemp, Log, TEXT("Property %s has custom tag: %s"), *PropertyName.ToString(), *Value);
}
}
}
Practical Application
Imagine you have an inventory system where you want to automatically categorize items. Instead of hardcoding an Enum or a switch statement for every item type, you could tag your variables in C++ with CategoryTag = "Weapon" or CategoryTag = "Consumable".
At runtime, when the player picks up an item, your inventory manager can query the FProperty metadata of the item's variables. It looks for that CategoryTag, reads the value, and automatically places the item into the correct UI tab without you ever having to update the inventory logic when adding new items. This keeps your systems decoupled and highly flexible.
Key Takeaway: By using FindPropertyByName and GetMetaData, you can transform your C++ code into a dynamic system that reacts to its own structure at runtime.