How can I get access to a structure defined in blueprints through C++?

Hey folks,

Using unreal and I have created a series of enums, structures and data tables within Blueprints so that my designers friend can access them, change the values etc. I then need to be able to access the data table through c++. This should be manageable if I can get access to the structure but I can find no way to get the structure from blueprint to c++. Any help would be greatly appreciated.

Thanks

1 Like

Structures made in blueprints, same as blueprints themselves exist virtually in UE4 reflection system and can be accessed only in runtime. That means you need to grab values of struct as well of structure of struct from reflection system, but you won’t be able to directly reference it like you normaly do in C++ as they are not declared in C++.

UE4 reflection system is system that stores information about code structure, blueprints use it too, in fact it is system that makes blueprint works in space of C++ code. each element of code structure, class, property, function, enum has it’s own UField object

each stricture type has it own class, you already probably see UClass* since it’s most common reflection system class that average C++ developer UE4 interact with :slight_smile: And you should be aware that each class has it own UClass so you should know what i’m saying, but not only Classes has it but all properties function etc. all that is registed in reflection system by using those UCLASS() and UFUNCTION() macros via UnrealHeaderTool.

So i bet you already should figure what to do here ;] You need to get UStruct of blueprint structure, lucky for you they are assets and you can catch UStruct same as you catch UClass of blueprint if you ever did that. The asset class of blueprint structure is UUserDefinedStruct which inference from UStruct, so when you get this asset you already have UStruct. here doc on how to get asset if you not familiar with this:

Now you need to get UProperty of varable, by using this function from UStruct oyu got

Each property type has UProperty class so you need to cast that property to proper type, note that UField is still UObject and it also has it’s own UClass so you can check type by checking the class of UProperty object you have:

https://docs.unrealengine.com/latest/INT/API/Runtime/CoreUObject/UObject/UProperty/index.html

From there you use GetPropertyValue

https://docs.unrealengine.com/latest/INT/API/Runtime/CoreUObject/UObject/UBoolProperty/GetPropertyValue/index.html

There also SetPropertyValue

https://docs.unrealengine.com/latest/INT/API/Runtime/CoreUObject/UObject/UBoolProperty/SetPropertyValue/index.html

Argument A is object from which you want to read varable. Some types might have different way of accessing property so check API reference.

And so you can read and write properties in blueprint. You probably will want to get struct from struct property from blueprint, you do it exactly the same way just from UClass of blueprint insted of UStruct

As you can see it would be easier to just simply declare those structs in C++, so you and your designers can access them evenly. You most likely end up redeclaring that struct in C++ anyway even if you use method above.

Btw as we in this topic aside of finding properties (as well as functions if you swap UProperty with UFunction down there) by name you can also search properties with TFieldIterator:

for(TFieldIterator<UProperty> PropertyIt(UYourClass::StaticClass); PropertyIt; ++PropertyIt) {

}

With this you can code self generating UI for some settings so you don’t need to change UI code when you add varables, same as Property Editor does in UE4 editor ;] You can also access meta specifiers that you write in properties (for example UPROPERTY(meta=(ExampleValue=1)) )

https://docs.unrealengine.com/latest/INT/API/Runtime/CoreUObject/UObject/UField/GetINTMetaData/1/index.html

I really recomand you to explore reflection system because if you master it you can save up a lot of coding time by making code that is aware of it’s own structure and adopt itself. As you may already be aware editor use this intensively and it a main reason why just adding BlueprintCallable is all it takes to make Blueprint node, your code can as same as smart

1 Like

Yeah I ended up seeing the wisdom of your words and just creating the struct in c++. Hell of a lot easier. Thank you for your help.

1 Like

Thank you for this detailed guide!!

Yes to this lol, just recreate the enum in code.