Complete Example of building and extending UELibrary
I’ve spent a lot of time figuring our how to set up UELibrary for my custom project. The official documentation is helpful but unfortunately incomplete. I’ve compiled all of my findings in one post + added an example on how you can extend the base API.
You can find the complete example here. I will outline the main points in this post:
- Get the source code for Unreal Engine. Compile it in the Development Editor configuration.
- Create a new Blank C++ Project. Let’s call it
UELibExtended
. - Modify the
UELibExtended.target.cs
file per official documentation. You must add UELibrary module as well. - Compile
UELibExtended
in Development configuration. It will produceUELibExtended.lib
andUELibExtended.dll
. - Create a new Win32 app and link it against
UELibExtended.lib
. You’ll need to reference theUnrealEngine\Engine\Source\Runtime\UELibrary\Public\UELibraryAPI.h
header in your app. - When your project compiles copy
UELibExtended.dll
to the output directory.
To extend the base API you will need to create a new .h and .cpp file. Expose your functions using the __declspec(dllexport)
syntax.
// .h file
#pragma once
#ifdef UELIBEXTENDED_LIBRARY_DLL_EXPORT
#define UELIBEXTENDEDLIBRARYAPI __declspec(dllexport)
#else
#define UELIBEXTENDEDLIBRARYAPI __declspec(dllimport)
#endif
extern "C"
{
UELIBEXTENDEDLIBRARYAPI int UELibExtended_CountActors();
}
// .cpp file
#define UELIBEXTENDED_LIBRARY_DLL_EXPORT
#include "UELibExtendedApi.h"
#undef UELIBEXTENDED_LIBRARY_DLL_EXPORT
#include "EngineMinimal.h"
UELIBEXTENDEDLIBRARYAPI int UELibExtended_CountActors()
{
if (!GEngine) {
return 0;
}
for (const FWorldContext& Context : GEngine->GetWorldContexts())
{
if (Context.WorldType == EWorldType::Game) {
return Context.World()->GetActorCount();
}
}
return 0;
}
Compile your game in Development configuration and copy your .h file to include folder of your app.