XRay Unreal Engine 4.5 source code

Published November 13, 2014 by CoderGears Team, posted by CoderGears
Do you see issues with this article? Let us know.
Advertisement

The Unreal Engine is a game engine developed by Epic Games, first showcased in the 1998 first-person shooter game Unreal. Although primarily developed for first-person shooters, it has been successfully used in a variety of other genres, including stealth, MMORPGs, and other RPGs. Its code is written in C++ and it's used by many game developers today. Its source code is available for free from GitHub. Many amazing games were developed using this engine, it permits developers to produce very realistic renderings like this one.

488-unreal-engine-4.jpg

What's the source code executed behind the scene to produce this realistic rendering? It's very interesting to go inside this powerful game engine and discover how it's designed and implemented. C++ developers could learn many good practices from its code base. Let's XRay its source code using CppDepend and CQLinq to explore some design and implementation choices of its developement team.

1- Namespaces

Unreal Engine uses namespaces widely for three main reasons:

  • Many namespaces contain only enums as shown by this following CQLinq query, which gives us the ones containing only enums.
unreal2.png

In a large project, you would not be guaranteed that two distinct enums don't both get called with the same name. This issue was resolved in C++11, using enum class which implicitly scope the enum values within the enum's name.

  • Anonymous namespace: Namespace with no name avoids making global static variable. The "anonymous" namespace you have created will only be accessible within the file you created it in. Here it is the list of all anonymous namespaces used:
unreal3.png
  • Modularizing the code base: Let's search for all the other namespaces, i.e. neither the anonymous ones nor the ones containing only enums:
unreal6.png

The namespaces represent a good solution to modularize the application; Unreal Engine defines more than 250 namespaces to enforces its modularity, which makes the code more readable and maintainable.

2- Paradigm used

C++ is not just an object-oriented language. As Bjarne Stroustrup points out, "C++ is a multi-paradigmed language." It supports many different styles of programs, or paradigms, and object-oriented programming is only one of these. Some of the others are procedural programming and generic programming.

2-1 Procedural Paradigm

2-1-1 Global functions

Let's search for all global functions defined in the Unreal Engine source code:

unreal7.png

We can classify these functions in three categories:

1 - Utility functions: For example 6344 of them concern Z_Construct_UXXX functions, which are used to create instances needed by the engine.

unreal8.png

2 - Operators: Many operators are defined as it is shown, by the result of this CQLinq query:

unreal9.png

Almost all kinds of operators are implemented in the Unreal Engine source code.

3 - Functions related to the engine logic: Many global functions containing some engine treatments are implemented. Maybe these kinds of functions could be grouped by category, as static methods into classes, or grouped in namespaces.

2-1-2 Static global functions:

It's a best practice to declare a global function as static unless you have a specific need to call it from another source file.

unreal10.png

Many global functions are declared as static, and as specified before, other global functions are defined inside anonymous namespaces

2-1-3 Global functions candidate to be static.

Global not exported functions, not defined in an anonymous namespace and not used by any method outside the file where they were defined. These are good candidates to be refactored to be static.

unreal65.png

As we can observe some global functions are candidates to be refactored to be static.

2-2 Object Oriented paradigm

2-2-1 Inheritance

In object-oriented programming (OOP), inheritance is a way to establish Is-a relationship between objects. It is often confused as a way to reuse the existing code which is not a good practice because inheritance for implementation reuse leads to Tight Coupling. Re-usability of code is achieved through composition (Composition over inheritance). Let's search for all classes having at least one base class:

unreal13.png

And to have a better idea of the classes concerned by this query, we can use the Metric View.

In the Metric View, the code base is represented through a Treemap. Treemapping is a method for displaying tree-structured data by using nested rectangles. The tree structure used in a CppDepend treemap is the usual code hierarchy:

  • Projects contain namespaces.
  • Namespaces contain types.
  • Types contain methods and fields.

The treemap view provides a useful way to represent the result of a CQLinq request; the blue rectangles represent this result, so we can visually see the types concerned by the request.

unreal12.png

As we can observe, the inheritance is widely used in the Unreal Engine source code.

Multiple Inheritance: Let's search for classes inheriting from more than one concrete class.

unreal15.png

The multiple inheritance is not widely used, only a few classes inherit from more than one class.

2-2-2 Virtual methods

Let's search for all virtual methods defined in the Unreal Engine source code:

unreal19.png

Many methods are virtual, and some of them are pure virtual:

unreal21.png

As with the procedural paradigm, the OOP paradigm is also widely used in the Unreal Engine source code. What about the generic programming paradigm?

2-3 Generic Programming

C++ provides unique abilities to express the ideas of Generic Programming through templates. Templates provide a form of parametric polymorphism that allows the expression of generic algorithms and data structures. The instantiation mechanism of C++ templates insures that when a generic algorithm or data structure is used, a fully-optimized and specialized version will be created and tailored for that particular use, allowing generic algorithms to be as efficient as their non-generic counterparts.

2-3-1 Generic types:

Let's search for all genric types defined in the engine source code:

unreal23.png

Only a few types are defined as generic. Let's search for generic methods:

unreal26.png

More than 40000 methods are generic; they represent more than 25% of the methods implemented. To resume the Unreal Engine source code, mix between the three paradigms.

3- PODs to define the data model

In object-oriented programming, plain old data (POD) is a data structure that is represented only as passive collections of field values (instance variables), without using object-oriented features. In computer science, this is known as passive data structure Let's search for the POD types in the Unreal Engine source code

unreal28.png

More than 2000 types are defined as POD types, many of them are used to define the engine data model.

4- Gang Of Four design patterns

Design Patterns are a software engineering concept describing recurring solutions to common problems in software design. Gang of four patterns are the most popular ones. Let's discover some of them used in the Unreal Engine source code. 4-1 Singleton The singleton is the most popular and the most used one. Here are some singleton classes defined in the source code:

unreal29.png

TThreadSingleton is a special version of singleton. It means that there is created only one instance for each thread. Calling its method Get() is thread-safe. 4-2 Factory Using factory is interesting to isolate the logic instantiation and enforces the cohesion; here is the list of factories defined in the source code:

unreal30.png

And here's the list of the abstract ones:

unreal31.png

4-3 Observer The observer pattern is a software design pattern in which an object maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods. They are some observers implemented in its source code, FAIMessageObserver is one of them. Here's a dependency graph to show the call of the OnMessage method of this observer:

unreal70.png

4-4 Command The command pattern is a behavioral design pattern in which an object is used to represent and encapsulate all the information needed to call a method at a later time. Four terms always associated with the command pattern are command, receiver, invoker and client. A command object has a receiver object and invokes a method of the receiver in a way that is specific to that receiver's class. Here's for example all commands inheriting from the IAutomationLatentCommand:

unreal33.png

5- Coupling and Cohesion

5-1 Coupling

Low coupling is desirable because a change in one area of an application will require fewer changes throughout the entire application. In the long run, this could alleviate a lot of time, effort, and cost associated with modifying and adding new features to an application.

Low coupling could be acheived by using abstract classes or using generic types and methods.

Let's search for all abstract classes defined in the Unreal Engine source code :

unreal34.png

Only a few types are declared as abstract. The low coupling is more enforced by using generic types and generic methods. Here's for example the methods using at least one generic method:

unreal27.png

As we can observe many methods use the generic ones, the low coupling is enforced by the function template params. Indeed the real type of these parameters could change without changing the source code of the method called. 5-2 Cohesion

The single responsibility principle states that a class should not have more than one reason to change. Such a class is said to be cohesive. A high LCOM value generally pinpoints a poorly cohesive class. There are several LCOM metrics. The LCOM takes its values in the range [0-1]. The LCOM HS (HS stands for Henderson-Sellers) takes its values in the range [0-2]. A LCOM HS value higher than 1 should be considered alarming. Here are how to compute LCOM metrics:

LCOM = 1 - (sum(MF)/M*F) LCOM HS = (M - sum(MF)/F)(M-1)

Where:

  • M is the number of methods in class (both static and instance methods are counted, it includes also constructors, properties getters/setters, events add/remove methods).
  • F is the number of instance fields in the class.
  • MF is the number of methods of the class accessing a particular instance field.
  • Sum(MF) is the sum of MF over all instance fields of the class.

The underlying idea behind these formulas can be stated as follows: a class is utterly cohesive if all its methods use all its methods use all its instance fields, which means that sum(MF)=M*F and then LCOM = 0 and LCOMHS = 0.

LCOMHS values higher than 1 should be considered alarming.

unreal36.png

Only some types are considered as not cohesive.

6- Immutability, Purity and side effect

6-1 Immutable types

Basically, an object is immutable if its state doesn't change once the object has been created. Consequently, a class is immutable if its instances are immutable.

There is one important argument in favor of using immutable objects: It dramatically simplifies concurrent programming. Think about it, why is writing proper multithreaded programming a hard task? Because it is hard to synchronize threads access to resources (objects or others OS resources). Why is it hard to synchronize these accesses? Because it is hard to guarantee that there won't be race conditions between the multiple write accesses and read accesses done by multiple threads on multiple objects. What if there are no more write accesses? In other words, what if the state of the objects accessed by threads, doesn't change? There is no more need for synchronization!

Another benefit of immutable classes is that they can never violate LSP (Liskov Subtitution Principle) , here's a definition of LSP quoted from its wiki page:

Liskov's notion of a behavioral subtype defines a notion of substitutability for mutable objects; that is, if S is a subtype of T, then objects of type T in a program may be replaced with objects of type S without altering any of the desirable properties of that program (e.g., correctness).

Here's the list of immutable types defined in the source code:

unreal38.png

6-2 purity and side effect The primary benefit of immutable types come from the fact that they eliminate side-effects. I couldn't say it better than Wes Dyer so I quote him: We all know that generally it is not a good idea to use global variables. This is basically the extreme of exposing side-effects (the global scope). Many of the programmers who don't use global variables don't realize that the same principles apply to fields, properties, parameters, and variables on a more limited scale: don't mutate them unless you have a good reason.(...) One way to increase the reliability of a unit is to eliminate the side-effects. This makes composing and integrating units together much easier and more robust. Since they are side-effect free, they always work the same no matter the environment. This is called referential transparency. Writing your functions/methods without side effects - so they're pure functions, i.e. not mutate the object - makes it easier to reason about the correctness of your program. Here's the list of all methods without side-effects

unreal41.png

More than 125 000 methods are pure.

7- Implementation quality

7-1 Too big methods

Methods with many number of lines of code are not easy to maintain and understand. Let's search for methods with more than 60 lines.

unreal44.png

Unreal Engine source code contains more than 150 000 methods, so less than 1% could be considered as too big.

7-2 Methods with many parameters

unreal45.png

Few methods have more than 8 parameters, most of them are generic, to avoid defining variadic functions, like the case of TCStringt::Snprintf methods.

7-3 Methods with many local variables

unreal46.png

Less than 1% have many local variables.

7-4 Methods too complex

Many metrics exist to detect complex functions, NBLinesOfCode, Number of parameters and number of local variables are the basic ones.

There are other interesting metrics to detect complex functions:

  • Cyclomatic complexity is a popular procedural software metric equal to the number of decisions that can be taken in a procedure.
  • Nesting Depth is a metric defined on methods that is relative to the maximum depth of the more nested scope in a method body.
  • Max Nested loop is equals the maximum level of loop nesting in a function.

The max value tolerated for these metrics depends more on the team choices, there are no standard values.

Let's search for methods that could be considered as complex in the Unreal Engine code base.

unreal49.png

Only 1.5% are candidate to be refactored to minimize their complexity.

7-4 Halstead complexity Halstead complexity measures are software metrics introduced by Maurice Howard Halstead in 1977. Halstead made the observation that metrics of the software should reflect the implementation or expression of algorithms in different languages, but be independent of their execution on a specific platform. These metrics are therefore computed statically from the code. Many metrics were introduced by Halstead, let's take as example the TimeToImplement one, which represents the time required to program a method in seconds.

unreal50.png

1748 methods require more than one hour to be implemented.

8- RTTI

RTTI refers to the ability of the system to report on the dynamic type of an object and to provide information about that type at runtime (as opposed to at compile time). However, RTTI has become controversial within the C++ community. Many C++ developers choose to not use this mechanism. What about Unreal Engine developers team?

unreal60.png

No method uses the dynamic_cast keyword, The Unreal Engine team chose to not use the RTTI mechanism.

9- Exceptions

Exception handling is also another controversial C++ feature. Many known open source C++ projects do not use it. Let's search whether in the Unreal Engine source code an exception was thrown.

unreal62.png

Exceptions are thrown in some methods; let's take as example the RaiseException one:

unreal61.png

As specified in their comments, the exception could be generated for the header tool, but in normal runtime code they don't support exception handling.

10- Some final statistics

10-1 most popular types It's interesting to know the most used types in a project; indeed these types must be well designed, implemented and tested. And any change occuring to them could impact the whole project. We can find them using the TypesUsingMe metric:

unreal71.png

However there's another interesting metric to search for popular types: TypeRank. TypeRank values are computed by applying the Google PageRank algorithm on the graph of types' dependencies. A homothety of center 0.15 is applied to make it so that the average of TypeRank is 1. Types with high TypeRank should be more carefully tested because bugs in such types will likely be more catastrophic. Here's the result of all popular types according to the TypeRank metric:

unreal52.png

10-2 Most popular methods

unreal54.png

10-3 Methods calling many other methods It's interesting to know the methods using many other ones, It could reveal a design problem in these methods. And in some cases a refactoring is needed to make them more readable and maintainable.

unreal57.png
Cancel Save
0 Likes 8 Comments

Comments

AlanSmithee
Very interesting read. Its fun to see how an actual engine is structured.
May 15, 2015 07:22 AM
Laury

I'm sorry, but I don't see the point of such an "article"...

May 15, 2015 08:31 AM
ferrous

I'm sorry, but I don't see the point of such an "article"...

While it certainly is an advertisement of sorts, it is interesting to get a peek at code analysis tools and what they can do. It's also interesting to see the development decisions behind Unreal.

May 15, 2015 06:07 PM
ongamex92

I'm sorry, but I don't see the point of such an "article"...

While it certainly is an advertisement of sorts, it is interesting to get a peek at code analysis tools and what they can do. It's also interesting to see the development decisions behind Unreal.

Yes it is, but it's not enough for an article.

May 16, 2015 01:04 PM
Laury

I'm sorry, but I don't see the point of such an "article"...

While it certainly is an advertisement of sorts, it is interesting to get a peek at code analysis tools and what they can do. It's also interesting to see the development decisions behind Unreal.

Yes it is, but it's not enough for an article.

Indeed. I don't see how an enumeration of this kind can be considered an analysis of the development desisions behind Unreal.

Moreover, it completly lacks the historical part of the engine and how it evolved from the previous versions. The "article" states Unreal Engine has been used to create lots of successful games but forgets hardly one game has been commercialy released with this iteration (UE4).

May 18, 2015 12:14 PM
slayemin

As a game programmer who is using UE4 and has built my own engine in the past, I don't see what value I could hope to get out of this article. The UE4 documentation does a much better job of describing the engine architecture and how to use it, and the API documentation shows exactly how each function call works.

Overall, I think I disagree with the author on how "interesting" the content within article is.

May 18, 2015 08:12 PM
aaaaaaaaaaaaaa12345

Doing a quick first pass the first thing I notice is that there are a lot of screenshots, however extremely little text. Not to sound out of place but this article seems as if it was rushed, it needs more text than screenshots, this shouldnt feel like a comic strip but a mini story with a few screenshots to backup the claims if text is not sufficient enough.

Also I think if you are going to talk about the engine you should focus more on a particular portion of the engine, you dont need to do an overview of the engine thats what their documentation is for and will do the job much better in most cases.

Edit*

I should also mention that, comparing how many for loops and generic functions exists in a codebase is like trying to convince someone how "red" the color "red" actually is.

May 20, 2015 08:22 AM
fafase

A quick look at the author name and its web page will let you know that this is just marketing. The reviewed products are products they sell.

The old days of providing articles for the sake of helping are gone. Most recent ones are just (more or less) hidden advertisement.

I would not be surprised if this was a copy paste of their documentation like it was for an engine series a couple of months ago simply pasting the starting tutorial from their website.

Under the tag "Look we want to help and share" is another tag saying "Know our name and our product".

May 21, 2015 11:47 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!

With the source code released, take a look at some of the implementations behind the awesome graphical power of the Unreal engine

Advertisement

Other Tutorials by CoderGears

CoderGears has not posted any other tutorials. Encourage them to write more!
Advertisement