If you want to help us maintaining this wiki, check out our discord server: https://discord.gg/3u69jMa 

Difference between revisions of "Writing Native Code"

From SWRC Wiki
Jump to navigation Jump to search
Line 1: Line 1:
Note: Some C++ knowledge is required. This is not a programming tutorial.
==Introduction==
==Introduction==
Native coding in the Unreal Engine refers to code that is written in C++ instead of UnrealScript. This C++ code can be called from a script to execute tasks that are either too slow in UnrealScript (about 20 times slower than C++) or are not possible at all due to the limited features of the language.
Native coding in the Unreal Engine refers to code that is written in C++ instead of UnrealScript. This C++ code can be called from a script to execute tasks that are either too slow in UnrealScript (about 20 times slower than C++) or are not possible at all due to the limited features of the language. Native coding is only officially supported by Unreal Tournament from 1999 (I think Deus Ex as well...) since it is the only game that has its native header files publicly available for download. Epic never released the headers for newer versions of the engine due to licensing issues with third party code. However, after countless hours of work it was possible to modify the UT99 headers in such a way that they're usable with Republic Commando. This process is not finished yet but the most important classes like Object, Actor, Pawn and some others already work.


==Introduction==
The way native coding works in Unreal is that you compile your scripts into a .u package and then when you write your C++ code you compile it as a dll with the same name as your package. When the game is loading a UnrealScript class and detects that it was declared as native, it also looks for a dll which contains the native code for this UnrealScript class.


==Getting Started==
==Getting Started==
 
Before you can write any C++ code you first need to create a UnrealScript class that you declare as native. This tutorial assumes that you already know how to compile UnrealScript code with ucc. If you don't, then the download for the [[Republic Commando UCC]] contains a brief explanation in its readme.
==Code==
This code assumes that the folders with the headers for Core and Engine are in the same directory as the source file
 
<source lang="cpp" line">
//Linking the import libraries for Core.dll and Engine.dll so that functions from these libraries can be called
#pragma comment(lib, "Core/lib/Core.lib")
#pragma comment(lib, "Engine/lib/Engine.lib")
 
//Including the headers for core and engine
#include "Engine/Inc/Engine.h"
 
//This function is going to replace appInit from UnFile.h and thus needs to have the same signature
__declspec(dllexport) void testFunc(const char* InPackage, const char* InCmdLine, FOutputDevice* InLog, FOutputDeviceError* InError,
                                    FFeedbackContext* InWarn, FConfigCache*(*ConfigFactory)(), int RequireConfig){
    const char* newCmdLine = "-windowed -log abcdefg 12345";
 
    InLog->Logf("Replacing command line with %s", newCmdLine);
 
    appInit(InPackage, newCmdLine, InLog, InError, InWarn, ConfigFactory, RequireConfig);
}
</source>
 
==Making The Game Call Custom Code==
 
[[File:IIDKing.PNG]]
 
[[File:Olly1.PNG]]
 
[[File:Olly2.PNG]]
 
[[File:Result.PNG]]

Revision as of 17:11, 27 January 2018

Note: Some C++ knowledge is required. This is not a programming tutorial.

Introduction

Native coding in the Unreal Engine refers to code that is written in C++ instead of UnrealScript. This C++ code can be called from a script to execute tasks that are either too slow in UnrealScript (about 20 times slower than C++) or are not possible at all due to the limited features of the language. Native coding is only officially supported by Unreal Tournament from 1999 (I think Deus Ex as well...) since it is the only game that has its native header files publicly available for download. Epic never released the headers for newer versions of the engine due to licensing issues with third party code. However, after countless hours of work it was possible to modify the UT99 headers in such a way that they're usable with Republic Commando. This process is not finished yet but the most important classes like Object, Actor, Pawn and some others already work.

The way native coding works in Unreal is that you compile your scripts into a .u package and then when you write your C++ code you compile it as a dll with the same name as your package. When the game is loading a UnrealScript class and detects that it was declared as native, it also looks for a dll which contains the native code for this UnrealScript class.

Getting Started

Before you can write any C++ code you first need to create a UnrealScript class that you declare as native. This tutorial assumes that you already know how to compile UnrealScript code with ucc. If you don't, then the download for the Republic Commando UCC contains a brief explanation in its readme.