Long time ago I wrote an article about adding a multiplatform runtime module to the Unreal Engine 4. This day that article would be complately out of date. UE4 changes and the way the new stuff can be added to it changes too. First of all, I want to to be clear – this article will not cover 100% of the topic, because it’s simply too broad. This is rather a quick start into writing your own plugins, getting know the basics. This article will cover the following topics:

  1. Creating a plugin
  2. Building a plugin
  3. Making a plugin that works differently,  depending on the platform
  4. Using the plugin from C++
  5. Using the plugin from Blueprints
  6. Creating a plugin settings editable from the Editor
  7. Calling asynchronous functions from Blueprints

When writing this article I was using the Unreal Engine 4.18.1.

For the lazy ones – here’s the github source code version.

So, with all of this in mind – let’s start!

Creating a plugin

The easiest way to create a plugin for UE4 is to open any C++ UE4 project in the Editor, press Edit->Plugins and then press “New Plugin” button in the Plugins window (it will be at the right-bottom corner of the window).

pluginbut

Then, select “Blank Plugin”, name it somehow (for this article I’ve named our plugin “XPlugin”) and press “Create Plugin”.

blankplug

Right after the Editor finished its job you will see that the Plugin has already been added and enabled for this project (it will be somewhere on the bottom of the list).

xplug

You will also notice that the source of the plugin has been added to the Visual Studio solution.

solution

Building a plugin

If you build a solution the plugin will not build by itself. To trigger it’s building add to your project’s Build.cs the following line:

This will make the project dependent on the plugin, so when the project is built, then the plugin will be built too The project will also be able to call this plugin’s methods from code.

Making a plugin that works differently,  depending on the platform

There are probably many ways to achieve this, but I always create a base class which handles the default implementations of methods (in case if the plugin is used on the unsupported platform) and classes that inherits from it for every platform I want to support. Then when starting up the plugin I simply create an object of the proper class, dependently of the platform.

Here is a little trick the Unreal Engine 4 does – if you put files inside the directories named the same as a platform name – it will compile only files from the directory with the name of the platform we currently build for. The directory structure should looks like this:

struct

For the XPluginImplBase, which is the base implementation of what plugin does, we have a following code:

XPluginImplBase.h

XPluginImplBase.cpp

 

For every platform we want to support (in this situation: Windows, Android and IOS) we have a class that overrides the TestXPlugin function:

XPluginImpl.h

XPluginImpl.cpp

 

We’ve got an object that implements a functionality of this plugin. But we need a place where this object can be created. We are doing it in StartupModule() function in the generated by the Editor XPlugin.h file. Because we need only one instance of this object – if you like it or not – we’re going to use a singleton. There is a code of the plugin class below. I was trying to explain using comments what the specific lines of code do.

XPlugin.h

XPlugin.cpp

 

We are almost done. The only file we need to improve is the Build.cs script of the plugin:

XPlugin.Build.cs

Using the plugin from C++

The plugin is ready to use! Let’s try it.

To call a method from the plugin you simply need to get the singleton object of the plugin implementation class and run the function implemented there. I quickly added the BeginPlay() override in my project’s Game Mode and run the method from the plugin at start of the game:

As you can see – when the game has started the defined in Windows XPluginImpl class message was displayed. Well done!

winplat

Using the plugin from Blueprints

What if we want to run this function from Blueprints? We have to create a new class which implements the Blueprint Function Library class. Let’s call this class a XPluginBPLibrary. Add the header file to the Classes directory and the source file to the Private directory. Instead of the Classes directory you can use the Public one, but I like to keep all of the UObject related headers in this directory.

The directory tree should look like this:

bplib

The header file contains a list of the static BlueprintCallable or BlueprintPure UFunctions. The source file – their implementations:

XPluginBPLibrary.h

XPluginBPLibrary.cpp

Now you can simply execute this function from Blueprints, for example – from the Level Blueprint:

bptest

Delegates and Events

The another way the plugin can comunicate with the Blueprints or with the Game is by delegates. The best known to me approach is to create an ActorComponent that can receives events and which can be added to any Actor. The events are triggered by the Multicast Delegates from code.

Let’s create a XPluginComponent class. Once again – header file inside Classes directory, source file inside Private directory.

XPluginComponent.h

XPluginComponent.cpp

To bind any suitable function to this delegate in C++ run:

To broadcast this delegate run:

To bind an Event in Blueprints – first you need to add the XPluginComponent to any Actor, for example – to the Game Mode:

comp

With XPlugin Component added to the Actor – you can implement the On received XPlugin Test Message Event!

Ev1

Ev2

Creating a plugin settings editable from the Editor

To create a settings for the plugin that are visible in the Editor the XPluginSettings class must be created. You know  the drill. The header file to the Classes directory, the source file to the Private directory.

This class is simply an UObject that is serializable to the config. Let’s add some configurable parameters:

XPluginSettings.h

XPluginSettings.cpp

These values can be modified by changing the values in the DefaultEngine.ini file (this is the file where this class serializes itself):

To be able to modify these settings from the Editor we need to add one line to the Build.cs file first (for all platforms):

Then, modify the XPlugin.cpp. Add to the StartupModule and ShutdownModule the following code:

After you build the project and you open the Editor go to Edit->Project Settings… and at the very bottom of the Settings list you should see the XPlugin section:

Notice, that settings are greyed out, because we added EditCondition field to the UProperties meta.

sett

To get any value from these settings you can use either classic get config way:

Or use the SettingsModule

Calling asynchronous functions from Blueprints

First of all – we need some asynchronous functionality to call. Lets make a function that will run the HTTP request!

To create a Blueprint block that will run asynchronously the UBlueprintAsyncActionBase class must be created. I’ve added it to the XPluginBPLibrary file, but you can make a separate translation unit for it.

But first – let’s add the Http module to the Private Dependency Modules in the plugin Build.cs

XPluginBPLibrary.h

XPluginBPLibrary.cpp

Calling this function in the Blueprints looks like this:

Async

Conclusion

This is it! This is how you can write a multiplatform Unreal Engine 4 plugin with the C++ and the Blueprint support!
These are only basics, of course. With plugins you can implement third party libraries, add buttons or even whole windows and editors to the Editor. But this might be a material for a whole new article. I hope this one will help You somehow.
The plugin we’ve just created is available on github.

Now – go and make some!