x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

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?

Product Version: UE 4.21
Tags:
more ▼

asked Mar 26 '19 at 04:17 PM in C++ Programming

avatar image

RelaX92
5 1 1 3

avatar image RelaX92 Mar 27 '19 at 07:28 AM

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;
 };
(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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.

more ▼

answered Mar 26 '19 at 09:55 PM

avatar image

Shadowriver
36.6k 930 169 1111

avatar image RelaX92 Mar 27 '19 at 07:28 AM

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...

avatar image Shadowriver Mar 27 '19 at 11:05 PM

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

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question