Including the network middleware RakNet in Unreal Engine 4

raknet-logo.png
raknet-logo.png

Hello people,

according to the current situation of some of you guys I decided to create a small tutorial about how to include RakNet in UE4. I also saw that there is already a plugin for the middleware out there : RakNet for UE4 - Community & Industry Discussion - Unreal Engine Forums
In the past I

What is RakNet ?

RakNet is a cross-platform C++ and C# game networking engine. It is designed to be a high performance, easy to integrate, and complete solution for games and other applications.(Source: RakNet - Multiplayer game network engine)

Why doing this ?

I have been working on a small multiplayer roleplay game and had to find a solution for connecting players to a dedicated machine which handles all custom functions and methods. So I decided to use RakNet because its’ open source license and also just it is easy to understand for greenhorn developers in RakNet. Another important fact is that if I help just one guy of you, I’ll be happy!!!

Is this tutorial the right one for me ?

This tutorial is for all kinds of developers who want to code their own network solution for example a “login-system” and send information in a professional way over the internet.
You’ll need to code your own functions in C++.

“Stop writing and start with that tutorial!!!”

What is needed?

  • Visual Studio 2015
  • The RakNet source files
  • UE4 (in this case 4.12.*)

Known compatibility: 4.09 - 4.12

Start

  1. Clone or **download **the source files of RakNet (GitHub - facebookarchive/RakNet: RakNet is a cross platform, open source, C++ networking engine for game programmers.)
  2. **Extract **the files (no matter where you extract it)
  3. Create a copy of the folder and **move **it into your engine’s third party folder. For example: “C:\Program Files\Epic Games\4.12\Engine\Source\ThirdParty\RakNet” (While inside the RakNet folder are all the extracted files like the “RakNet_VS2008.sdf”)
  4. Open up the “Epic Games Launcher” and **create **a new Third-Person-C+±Project
  5. Wait until VS stopped compiling, save all and close VS and the Engine itself
  6. Start up your C++ project again (the “sln” file)
  7. **Navigate **to your “Source” folder inside VS and **open **up “YourGame.Build.cs”
  8. We will have to change the dependency module names to hold RakNet. This is how it looks like:

// Copyright 1998-2016 Epic Games, Inc. All Rights Reserved.

using UnrealBuildTool;

public class YourGame: ModuleRules
{
	public YourGame(TargetInfo Target)
	{
		PublicDependencyModuleNames.AddRange(new string] { "Core", "CoreUObject", "Engine", "InputCore", "RakNet" });
        PrivateDependencyModuleNames.Add("RakNet");
        PrivateDependencyModuleNames.AddRange(new string] { });
    }
}

  1. **Save and close **your VS and **navigate **to your UE project folder
  2. Right click the “YourGame.uproject” file and **chose **“Generate Visual Studio project files”
  3. **Open **the “sln” and **compile **your project
  4. Have fun!!!

Getting started

  1. After the editor finished loading right click in your content browser’s window and select “New C++ Class”. Its’ parent class will be “Actor”
  2. Call it “Network” (or a name which you prefers)
  3. Close the engine and switch over to VS
  4. Open up your network header file called “Network.h”
  5. RakNet will need some includes to run. So add the following to your header includes:

//******************************************************************
// RakNet Network includes START
//******************************************************************
#include "AllowWindowsPlatformTypes.h" // UE4
#include "RakPeerInterface.h"
#include "MessageIdentifiers.h"
#include "BitStream.h"
#include "RakNetTypes.h"
#include "Rand.h"
#include "HideWindowsPlatformTypes.h" // UE4
//******************************************************************
// RakNet Network includes END
//******************************************************************

  1. You’ll notice that “UCLASS()” is underlined and brings up an error. To stop this switch over to “YourGame.h” and change:

#include "EngineMinimal.h"

to


#include "Engine.h"

  1. Save, close and restart VS again. Don’t forget to regenerate the project files before starting the project again. If the “UCLASS()” is underlined anymore, it is just a VS bug. Just remove the “U” in “UCLASS” and write it back again. Wait until IntelliSense crawled it again. You’ll be able to compile now (you don’t need to)

These were the basics. Thank you for reading and I hope that I helped some of you. If you wan’t to have additional stuff, for example receiving and sending packets, feel free to post it here. Maybe I will add additional features of the tutorial.

Greetings

Place for future updates

How to do this for blueprint project?)