Integrating Google FlatBuffers into UE4

I would like to use Google’s FlatBuffers to help write some tools for talking to some UE4 servers. I’ve sank GitHub - google/flatbuffers: FlatBuffers: Memory Efficient Serialization Library and built the project using CMake. I’ve copied the .lib generated into UE4’s Third Party folder and have attempted to create a module for it by creating a flatbuffers.build.cs pointing to the include and .lib. However during compile of the editor I’m getting the error

C:\p4\Engine\Source\ThirdParty\flatbuffers\include\grpcpp/impl/codegen/config_protobuf.h(25): fatal error C1083: Cannot open include file: 'google/protobuf/stubs/common.h': No such file or directory

Which makes sense because that file really doesn’t exist, but it doesn’t exist in the source either which is confusing. Has anyone been able to integrate FlatBuffers into UE4 and if so how did you manage to do that? Any help would be appreciated.

I didn’t get this to work quite yet, but have found a plugin and supporting pipeline projects that at least help integrate GRPC and Protobuf:

Then start up a server by following the example at grpc/route_guide_server.cc at v1.18.0 · grpc/grpc · GitHub

Create a folder under your Source directory, like “Includes” and put a folder in there called “flatbuffers”, containing the 14 .h files from flatbuffers/include/flatbuffers at d44931656ab5801db260236650701812f4a7eb18 · google/flatbuffers · GitHub

Then in your Source/[ProjectName]/[ProjectName].Build.cs file, add this to pick up those new .h files:

//The path for the header files
PublicIncludePaths.AddRange(new string[] { "Includes" });	//flatbuffers, etc

Then in some cpp file where you want to use the flatbuffers, add this at the top after your #includes

using namespace GameLogic::FlatBuffers; //use whatever namespace you gave to flatc

Finally, one of the trickier usage pieces is dealing with strings.
Once you have an FB object, to log a string value for example:

UE_LOG(LogMurray, Log, TEXT("Finally: %d. %s"), fb->ErrorCode(), ANSI_TO_TCHAR(fb->ErrorMessage()->c_str())); //pretend you build an FB that had an int prop ErrorCode and a string prop ErrorMessage

One side note is the pathing and naming. Flatc likes to append “.generated” to output file names, which is perfect in every way except for the fact that UE4 assumes that files with that ending are its own files, and you’ll be in hell. So you will need to rename your generated output files with a script or tool, and possible also change the include path in those files, or move flatbuffers into your project and not in a top level subfolder like Includes

#include "flatbuffers/flatbuffers.h" // <-- you may need to change the generated include to this

It is a PITA but worth it assuming you aren’t making these by hand. gl

ANSI_TO_TCHAR(fb->ErrorMessage()->c_str())

1 Like