Adding flat file as asset

I’m looking to include the contents of a simple raw datafile as an asset for use from C++. However, I can’t find any way to just add a file, and I can’t find any good examples of methods for defining a new data type that UE4 understands as an asset.

Any suggestions on where to look?

1 Like

if you use it in c++ then I would think you could just add it as an item in your sln and set it to content and copy if newer (or copy always) and it will add it to the build.

What kind of data you want to hold and what for? Maybe there different way to do it

I don’t think that will work - the UE4 build system doesn’t pay attention to the .sln in any way, it’s just used as the standard way to get Visual Studio to launch the actual build script. You can actually build entirely without the .sln just by running the build script manually.

Edit: Plus, for hopefully obvious reasons, that won’t work on Linux or Mac.

Several kinds of library-specific bulk data for a third-party library. It doesn’t map to anything in UE4 and it’s already got a reasonable binary format, I just need a way to stash a blob of data.

Solved!

There’s no built-in way to accomplish this; you’ll have to create your own datatype and your own loader. It’s not super-complicated but it takes a lot of hunting to find the basics, so I’ll just go down a list of keywords and simple instructions and let any future readers puzzle out the rest.

In order to store the data you’ll need to create an object that inherits from UObject and contains some appropriate datastorage item. We’ll call this UMyBlob, and have it contain a TArray< uint8 > that is a property. .uasset files are just serialized objects - this will all be serialized through the normal system.

In order for the editor to create these objects you’ll first need an appropriate class derived from FAssetTypeActions_Base. This must be put inside an editor-only plugin module and has some functions you’ll want to fill out. In the IModuleInterface::StartupModule for that module, you need to register it (IAssetTools::RegisterAssetTypeActions) and in the IModuleInterface::ShutdownModule, you need to unregister it (IAssetTools::UnregisterAssetTypeActions).

Next, you need a factory, inherited from UFactory. You don’t need to instantiate this anywhere; the engine will do it for you. You’ll want a ConfigureProperties() function that opens a file dialog (IDesktopPlatform::OpenFileDialog - it’s a pain) and reads a filename, and a FactoryCreateNew() function that loads the file (IPlatformFile::FileSize(), IPlatformFile::OpenRead()), creates a UMyBlob, and jams the data inside.

Finally, when you want to get the data out, you’ll LoadObject< UMyBlob >() in the normal fashion.

The whole shebang clocks out at around 100-200 lines of code depending on boilerplate, whitespace, and errorchecking. I haven’t yet tested this with cooked content - I’ll update this comment if I run into problems, though.

Happy binary data embedding!

2 Likes