A set of Unreal Engine 4 plugins that will allow users to write game-play code in C# (and eventually other CLI languages such as F#), in game projects targeting the Windows platform. If you're looking for a cross-platform alternative Xamarin has developed a plugin that provides Mono integration, however a commercial Mono license is not cheap, and it doesn't work in newer UE versions. The primary aim of this project is to make it possible to do anything that can currently be done in Blueprints in C# scripts, in order to eliminate the need for complex Blueprint spaghetti.
The current development focus is on script components (see UKlawrScriptComponent
), these are
actor components whose functionality is implemented in a C# class. The C# class attached to a
script component can access any Blueprint accessible property or function exposed by the actor it
is attached to or any of its sibling components (this is still limited at present since only a
subset of all the possible property types is supported). Eventually the properties and methods
defined in a C# script component will also be exposed to Blueprints.
Some time was also spent developing script objects, which allow for "subclassing" in C# of almost
any UObject
derived class, actors included, but that's currently on the back-burner.
NOTE: This project is still highly experimental, and not very useful (though it's getting there).
Check the Wiki for additional details.
- Windows 7 or later
- Unreal Engine 4.10 or later (you'll need the source from GitHub)
- Visual Studio 2015
- .NET Framework 4.5.2 or later
Klawr consists of one native C++ library, a couple of C# assemblies, and three UE4 plugins.
The core of the CLR hosting code is located in the Klawr.ClrHost libraries. The Klawr.ClrHost.Native C++ library bootstraps the CLR, and the C# assemblies handle hosting related tasks and provide some core classes for use by the auto-generated UE4 C# API (as well as user scripts). Klawr.ClrHost.Native is linked into KlawrRuntimePlugin, which allows Unreal Engine to load and execute assemblies containing managed code.
KlawrCodeGeneratorPlugin generates C# wrappers for UObject
subclasses from the reflection
information gathered by the Unreal Header Tool (UHT) from UFUNCTION
and UPROPERTY
decorators in the engine/game source. This is the same reflection information that underpins much
of the functionality provided by Blueprints.
KlawrRuntimePlugin executes user scripts written in C#, scripts have access to the wrapped UE4 API generated by KlawrCodeGeneratorPlugin.
KlawrEditorPlugin provides a new Blueprint type that can be used to create actor components that are implemented in managed assemblies.
This is a two phase process, in the first phase the CLR hosting libraries must be built, in the second phase the plugins themselves.
The directory hierarchy of this repository matches that of the UE4 source repository, once you
check out this project you can just copy the Engine directory into your UE4 source checkout and
everything should end up in the right place. However, if you plan on making any changes to the
source you'll probably want create a couple of directory junctions instead of copy/pasting
files back and forth. To that end I've provided CreateLinks.bat
, which you can right-click
on and Run as administrator
directly from the checkout root. The batch file will prompt you
to enter the path to the UE4 source checkout that you'd like to use to build Klawr, and will then
create the necessary directory junctions.
Now everything should be in the right place, from here on any paths are relative to the UE4 source checkout.
To ensure that the Klawr code generator plugin is built before UHT is executed you'll need to
modify Engine/Source/Programs/UnrealHeaderTool/UnrealHeaderTool.Target.cs
in the engine source.
A patch file (UnrealHeaderToolPatch.diff
) with the relevant one-line change is provided in this
repository. However, since it is a one-liner you may find it quicker to make the change by hand,
all you have to do is replace the following line in UnrealHeaderTool.Target.cs
AdditionalPlugins.Add("ScriptGeneratorPlugin");
with
AdditionalPlugins.Add("KlawrCodeGeneratorPlugin");
- Open
Engine\Source\ThirdParty\Klawr\Klawr.ClrHost.sln
in VS2015. - Set the
Solution Configuration
to eitherRelease
orDebug
. - Set the
Solution Platform
tox64
(this is very important, Win32 builds are not supported yet). - Select
Build Solution
to build all the libraries.
Assuming that the build finished with no errors you can move on to phase two.
-
Configure UHT to use the Klawr code generator plugin, to do so add/edit the
Plugins
section inEngine\Programs\UnrealHeaderTool\Saved\Config\Windows\Engine.ini
(if the file doesn't exist, create it):[Plugins] ProgramEnabledPlugins=KlawrCodeGeneratorPlugin ScriptExcludedModules=ScriptPlugin ScriptExcludedModules=ScriptEditorPlugin ScriptExcludedModules=ScriptGeneratorPlugin ScriptExcludedModules=KlawrRuntimePlugin ScriptSupportedModules=CoreUObject ScriptSupportedModules=Engine
-
Run
GenerateProjectFiles.bat
in your UE4 source checkout. -
Open
UE4.sln
in VS2015. -
Set the
Solution Configuration
toDebug Editor
orDevelopment Editor
. -
Set the
Solution Platform
toWin64
(this is very important, Win32 builds are not supported yet). -
Select
Build Solution
to build everything.
During the build you may see a bunch of console windows popup briefly, don't panic, this is just
the Klawr code generator plugin building the UE4 C# wrappers assembly. The wrappers assembly can be
rebuilt manually by running Engine\Intermediate\ProjectFiles\Klawr\Build.bat
from the console.
First of all make sure you've got a project open that you don't mind obliterating in case something goes wrong, next enable KlawrRuntimePlugin and KlawrEditorPlugin in UnrealEd, restart as requested.
Now you can create a script component Blueprint.
Klawr is licensed under the MIT license.
Klawr uses the pugixml library to parse XML, it was written by Arseny Kapoulkine and is licensed under the MIT license.