Unreal Blueprint callable functions in C++

Writing some C++ that can be called in Unreal is really easy and is something you’re going to want to do at some point if only for performance reasons.

Here’s the header from a test class:

1
2
3
4
5
6
7
8
9
10
UCLASS()
class UHelpers : public UBlueprintFunctionLibrary
{
GENERATED_BODY()

public:
UFUNCTION(BlueprintCallable, Category = "Save Game")
static void LoadGame(const APlayerState* playerState, USaveGame* saveGameIn, USaveGame*& saveGameOut, bool& status);

};

This function takes an APlayerState and a USaveGame object and returns a USaveGame and a boolean. These are all shown as pins in the blueprint editor as you would expect.

This shows how to solve two of the not quite so obvious problems that you are going to have. The first is how to return more than one value from a function. A value can be returned from the function as with any C++ function, and Unreal
will create an output pin for it but any parameters that are passed by reference are considered to be output parameters by Unreal.

The second thing it shows is how to return a class as an output parameter. This is done by having a pointer to a reference for that class which is probably not obvious unless you’ve had a bit of experience with C++. In this particuar case I then just assign saveGameIn to saveGameOut at the end of the function so that the value can then
be chained into the next node of the graph. I’m not sure if you can define a function parameter to be both an input and an output pin, but if you can it’s not obvious as to how.

You can also add UPROPERTY(ref) before each parameter definition if you wish for it to be considered a reference in the blueprint editor. I’ve not experimented with any speed differences this may have.

Logging to screen in C++ with Unreal

In order to log to the screen in Unreal from code, as you can with PrintString in blueprints add the following:

1
#include <Runtime/Engine/Classes/Engine/Engine.h>

And then you can call a method on the global engine object.

1
2
3
4
5
6
GEngine->AddOnScreenDebugMessage(
-1, // Adds a new message, like the BP
0.5f, // Duration of message
FColor::Cyan.WithAlpha(64), // Colour
"Some text"
);

Whatever you do, don’t just include the main Engine.h as it’ll tank your compile times by including everything.

Using Dropbox from code

When I’m writing a utility that keeps some data I like to store it in my dropbox folder so it’s synchronised between all of my computers. This C# code will attempt to find where this folder is stores, or return null if it doesn’t seem to be there because either Dropbox isn’t installed or the internal details of how the location is stored has been changed again.

It checks in two locations because Dropbox changed where this was stored not so long ago and this code should work with both old and new installs.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public static string GetDropboxFolder() {
var appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var dbPath = Path.Combine(appDataPath, "Dropbox\host.db");

if(!File.Exists(dbPath)) {
appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
dbPath = Path.Combine(appDataPath, "Dropbox\host.db");
if(!File.Exists(dbPath)) {
return null;
}
}
var lines = File.ReadAllLines(dbPath);
var dbBase64Text = Convert.FromBase64String(lines[1]);
var folderPath = Encoding.ASCII.GetString(dbBase64Text);
return folderPath;
}