Iterate over Properties of Struct in C++

Hi,

I try to iterate over the properties of a structure, but I just can’t figure out how.

I have this example struct:

USTRUCT()
struct FCppPostStruct
{
	GENERATED_BODY()

	int32 userId;
	int32 id;
	FString title;
	FString body;
};

And this is code from the BeginPlay of an Actor in the scene:

FCppPostStruct* test = new FCppPostStruct();
test->id = 0;
test->userId = 0;
test->title = "hello";
test->body = "world";

for (TFieldIterator<UProperty> It(test->StaticStruct()); It; ++It)
{
	UProperty* Property = *It;
	FString VariableName = Property->GetName();
}

The loop isn’t executed at all, but my understanding is that the loop should run four times.

Could someone point me in the right direction?

You forgot to add UPROPERTY() to struct variables. UE4 only see things that are marked with those macros, UHT gathers information about them and generate code that register those properties in to reflection system, which you later read out in the this code.

I thought that’s only used for BluePrints, but sure, if you don’t declare it as an UProperty the reflection system won’t see it as an UProperty, so obvious…

Correct example struct, in case anyone has a semilar problem:

USTRUCT()
struct FCppPostStruct
{
	GENERATED_BODY()

	UPROPERTY()
	int32 userId;

	UPROPERTY()
	int32 id;

	UPROPERTY()
	FString title;

	UPROPERTY()
	FString body;
};

All those macros are for reflection system, that why they don’t expose things to blueprints by default :stuck_out_tongue: there many other specifiers that are not blueprint related, like auto ini config saving or replication