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), // Color and transparancy!
"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;
}