CryENGINE Game Programming with C++, C#, and Lua

By Filip Lundgren , Ruan Pearce-Authers
    Advance your knowledge in tech with a Packt subscription

  • Instant online access to over 7,500+ books and videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies
  1. Introduction and Setup

About this book

CryENGINE is a complete 3D game development solution that can run on multiple platforms. It is orientated around giving intuitive tools to the developer. A variety of interactive video games can be created using CryENGINE. CryENGINE is one of the most beginner-friendly engines out there to learn. If you are interested in diving into the various systems and understanding their workings in a way that is easily understood, then this book is for you.

This book provides you with the knowledge to tame the powerful but hard-to-master CryENGINE. CryENGINE Game Programming with C++, C#, and Lua dives into the various systems and explains their workings in a way that can be easily understood by developers of all levels. It provides knowledge on the engine along with step-by-step exercises and detailed information on the backend implementation of the subsystems, giving you an excellent foundation to build upon when developing your own CryENGINE games.

Written by developers with years of CryENGINE experience, this book breaks down the common confusion that encompasses the CryENGINE engine code, guiding you through a series of chapters aimed towards giving you the ability to create your own games in a rapid yet productive fashion. You will learn everything you need to know in order to create your own CryENGINE-powered games as well as detailed information on how to use the engine to your advantage. By teaching systems such as audio, particle effects, rendering, AI, networking, and more, we’ll be exposing the most inner parts of CryENGINE that commonly confuse programmers. If you want to quickly gain the knowledge required to create your own CryENGINE game title, then this book is for you.

Publication date:
November 2013
Publisher
Packt
Pages
276
ISBN
9781849695909

 

Chapter 1. Introduction and Setup

CryENGINE is known as one of the most extensible engines available due to its ability to portray a vast variety of impressive visuals and gameplay. This makes it an invaluable tool at the hand of a programmer, where the only limit is one's creativity.

In this chapter, we will cover the following topics:

  • Installing Visual Studio Express 2012 for Windows Desktop

  • Downloading the CryENGINE sample installation or using a custom engine install

  • Registering an account at http://www.crydev.net, the official CryENGINE development portal

  • Compiling a stripped-down CryGame library

  • Attaching and utilizing the debugger

 

Installing Visual Studio Express 2012


In order to compile the game code, you will need a copy of Visual Studio. For this demonstration, we'll be using Visual Studio Express 2012 for Windows Desktop.

Note

If you already have Visual Studio 2012 installed, you may skip this step.

To install Visual Studio, follow the given steps:

  1. Visit http://www.microsoft.com/visualstudio/ and download Visual Studio Express 2012 for Windows Desktop.

  2. After downloading the executable, install the application and proceed to the next step after restarting your computer.

 

Choosing your CryENGINE installation type


Now that we have Visual Studio installed, we'll need to download a version of CryENGINE to develop on.

We have created a stripped-down sample installation for the book, which is recommended for users who are just starting out with the engine. To download it, see the following Downloading the book's CryENGINE sample installation section.

If you would rather use another build of CryENGINE, such as the latest Free SDK release, please see the Using a custom or newer CryENGINE installation section later in this chapter. This section will cover integrating CryMono on your own.

 

Downloading the book's CryENGINE sample installation


For this book, we will be using a custom CryENGINE sample as a base for learning the workings of the engine. Most exercises in the book depend on this sample; however, the working knowledge you get from this can be applied to the default CryENGINE Free SDK (available at http://www.crydev.net).

To download the sample installation, follow these steps:

  1. Visit https://github.com/inkdev/CryENGINE-Game-Programming-Sample and click on the Download ZIP button in order to download a compressed archive containing the sample.

  2. Once downloaded, extract the contents of the archive to a folder of your choice. For the sake of the example, we will be extracting it to C:\Crytek\CryENGINE-Programming-Sample.

What just happened?

You should now have a copy of our sample CryENGINE installation. You can now run and view the sample content which we will be using for the most part of this book.

 

Using a custom or newer CryENGINE installation


This section helps out the readers who choose to use custom or newer builds of the engine. If you are unsure of this process, we recommend reading the Downloading the book's CryENGINE sample installation section in this chapter.

Verifying that the build is functional

Before starting, you should verify that your version of CryENGINE is functional so that you can use it for running and creating code based on this book's chapters.

Note

Note that if you are using an older or newer version of the engine, certain chapters may provide examples and information on changed systems. Keep this in mind, and refer to the sample mentioned previously for the optimal learning experience.

A good way to check this is by starting the Editor and Launcher applications and checking whether the engine behaves as expected.

Integrating CryMono (C# support)

If you're interested in using the sample code and chapter contents written with C# in mind, you'll need to integrate the third-party CryMono plugin into your CryENGINE installation.

Note

Note that CryMono is integrated by default in the sample we created specifically for this book.

To begin integrating CryMono, open the Code folder present in the engine root folder. We'll be placing the source files here, inside a subfolder called CryMono/.

To download the source code, visit https://github.com/inkdev/CryMono and click on Download Zip (or Clone in Desktop if you prefer using your Git revision control client).

Once downloaded, copy the contents into the Code/CryMono folder we mentioned earlier. If the folder does not exist, create it first.

When the files have been successfully moved, your folder structure should look similar to this:

Compiling the CryMono project

Now that we have the CryMono source code, we'll need to compile it.

To start, open Code/CryMono/Solutions/CryMono.sln using Visual Studio.

Note

Make sure to use CryMono.sln and not CryMono Full.sln. The latter is only used when you need to rebuild the entire Mono runtime, which ships precompiled with the CryMono repository.

Before we compile, we'll need to modify the engine's SSystemGlobalEnvironment struct (this is exposed using the global gEnv pointer).

To do so, open ISystem.h in the Code/CryEngine/CryCommon/ folder. Find the struct's definition by searching for the struct SSystemGlobalEnvironment.

Then add the following code to the very end of the struct's members and functions:

struct IMonoScriptSystem*
  pMonoScriptSystem;

Note

Modifying interfaces is not recommended if you do not have full engine source, as other engine modules have been compiled with the default interfaces in mind. However, appending to the end of this struct is mostly harmless.

Once done, open up the instance of Visual Studio where you opened CryMono.sln and start compiling.

Note

The automated post-build step in the project should automatically move the compiled files to your build's Bin32 folder following a successful compilation pass.

To verify that CryMono was compiled successfully, search for CryMono.dll in your Bin32 folder.

Loading and initializing CryMono via the CryGame.dll library

Now that we have the CryMono binaries present in our Bin32 folder, we'll just have to load it during game startup. This is done via the CryGame project, via the CGameStartup class.

To start, open your CryEngine or CryGame solution file (.sln) present in Code/Solutions/.

Including the CryMono interface folder

Before we modify the game startup code, we'll need to tell the compiler where to find the CryMono interfaces.

Start by right-clicking on the CryGame project in Visual Studio's Solution Explorer and select Properties. This should bring up the following CryGame Property Pages window:

Now, click on C/C++ and select General. This will bring up a screen of general compiler settings, which we'll use to add an additional include folder as shown in the following screenshot:

Now all we have to do is add ..\..\CryMono\MonoDll\Headers to the Additional Include Directories menu. This will tell the compiler to search CryMono's Headers folder when the #include macro is used, allowing us to find the CryMono C++ interfaces.

Initializing CryMono at start up

Open GameStartup.h in the CryGame project and add the following to the bottom of the class declaration:

static HMODULE
m_cryMonoDll;

Then open GameStartup.cpp and add the following before the CGameStartup constructor:

HMODULE CGameStartup::m_cryMonoDll = 0;

Now navigate to the CGameStartup destructor and add the following code:

if(m_cryMonoDll)
{
  CryFreeLibrary(m_cryMonoDll);
  m_cryMonoDll = 0;
}

Now navigate to the CGameStartup::Init function declaration, and add the following prior to the REGISTER_COMMAND("g_loadMod", RequestLoadMod,VF_NULL,""); snippet:

m_cryMonoDll = CryLoadLibrary("CryMono.dll");
if(!m_cryMonoDll)
{
  CryFatalError("Could not locate CryMono DLL! %i", GetLastError());
  return false;
}

auto InitMonoFunc = (IMonoScriptSystem::TEntryFunction)CryGetProcAddress(m_cryMonoDll, "InitCryMono");
if(!InitMonoFunc)
{
  CryFatalError("Specified CryMono DLL is not valid!");
  return false;
}

InitMonoFunc(gEnv->pSystem, m_pFramework);

Now all we have to do is compile CryGame in order to have CryMono loaded and initialized at startup.

Registering flow nodes

Due to a recent change in the flow system, flow nodes have to be registered at a certain point during game startup. To make sure that our C# nodes are registered, we'll need to call IMonoScriptSysetm::RegisterFlownodes from IGame::RegisterGameFlowNodes.

To do this, open Game.cpp and add the following inside the CGame::RegisterGameFlowNodes function:

GetMonoScriptSystem()->RegisterFlownodes();

Now, after compiling, all managed flow nodes should appear in the Flowgraph Editor.

 

Registering your CryDev account


The CryENGINE Free SDK requires a CryDev account in order for the application's to launch. This can be easily retrieved via http://www.crydev.net, by following these steps:

  1. Visit http://www.crydev.net in the browser of your choice.

  2. Click on Register in the upper-right corner.

  3. Read and accept the terms of usage.

  4. Select your username data.

What just happened?

You now have your own CryDev user account. While running the CryENGINE Free SDK applications (see Running the sample application), you will be prompted to log in with the details you have just registered.

 

Running the sample application


Before we get started with building the game project, we'll go through the basics of the default CryENGINE applications.

Note

All executables are contained inside either the Bin32 or Bin64 folders, depending on the build architecture. However, our sample only includes a Bin32 folder to keep things simple and the build repository small in size.

Editor

This is the main application that developers will use. The Editor serves as the direct interface to the engine, used for all kinds of developer-specific tasks such as level design and character setup.

The Editor supports WYSIWYP (What You See Is What You Play) functionality, which allows developers to preview the game by hitting the shortcut Ctrl + G, or by navigating to the Game menu, and choosing Switch to Game.

Starting the Editor

Open the main sample folder, and navigate to the Bin32 folder. Once there, start Editor.exe.

When the Editor has finished loading, you will be greeted by the Sandbox interface, which can be used to create most visual aspects of the game (excluding models and textures).

To create a new level, open the File menu, and select the New option. This should present you with the New Level message box. Simply specify your level name and click on OK, and the Editor will create and load your empty level.

To load an existing level, open the File menu, and select the Open option. This presents you with the Open Level message box. Select your level and click on Open to have your level loaded.

Launcher

This is the application seen by the end user. Upon startup, the Launcher displays the game's main menu, along with different options allowing users to load levels and configure the game.

The Launcher's game context is commonly referred to as Pure game mode.

Starting the Launcher

Open the main sample folder, and navigate into the Bin32 folder. Once there, start Launcher.exe.

When you've started the application, you'll be greeted with the default main menu. This interface allows the user to load levels and alter game settings such as visuals and controls.

The Launcher is preferable over the Editor when you want to play the game as the end user would. Another benefit is the quick startup time.

Dedicated server

The dedicated server is used to start up a multiplayer server for other clients to connect to. The dedicated server does not initialize a renderer, and instead functions as a console application.

 

Compiling the CryGame project (C++)


The CryENGINE Free SDK ships with complete source access to the game logic library, CryGame.dll. This dynamic library is responsible for the main part of game features, as well as the initial game startup process.

Note

A library is a collection of existing classes and functions that can be integrated into other projects. In Windows, the most common form of library is a Dynamic Link Library , or DLL , which uses the .dll file extension.

To start off, open the main sample folder, and navigate to Code/Solutions/, in which a Visual Studio solution file named CE Game Programming Sample.sln should be present. Double-click on the file and Visual Studio should start up, displaying the included projects (see the following breakdown).

Note

A solution is a structure for organizing projects in Visual Studio. The solution contains information on projects in a text-based .sln file, as well as a .suo file (user-specific options).

To build the project, simply press F7 or right-click on the CryGame project in the Solution Explorer and select Build.

What just happened?

You just compiled CryGame.dll, which should now be present in the binary folder. (Bin32 for 32-bit compilation, Bin64 for 64-bit). Launching the sample application will now load the .dll file containing the source code you compiled.

The CE Game Programming Sample solution breakdown

The solution includes the following three projects, one of which compiles to a .dll file.

CryGame

The CryGame project includes the underlying game logic used by the engine. This compiles to CryGame.dll.

CryAction

The CryAction project includes partial source to CryAction.dll, which is responsible for a large number of systems, such as actors, UI Graphs, and game objects. This project does not compile to a .dll file, but is instead only used for interface access.

CryCommon

The CryCommon project is a helper containing all shared CryENGINE interfaces. If there is a subsystem you want access to, look for its exposed interface in here.

 

The CryENGINE folder structure


See the following table for an explanation of the CryENGINE folder structure:

Folder name

Description

Bin32

Contains all 32-bit executables and libraries used by the engine.

Bin64

Contains all 64-bit executables and libraries used by the engine.

Editor

Editor configuration folder, contains common editor helpers, styles, and more.

Engine

Used as a central folder for assets used by the engine itself, not any particular game.

Shaders and configuration files are stored here.

Game

Each game contains a game folder, which includes all its assets, scripts, levels, and so on.

Does not have to be named "Game", but is dependent on the value of the sys_game_folder console variable.

Localization

Contains localization assets such as localized sounds and text for each language.

PAK files

The engine ships with the CryPak module, allowing for the storage of game content files in compressed or uncompressed archives. The archives use the .pak file extension.

When game content is requested, the CryPak system will query through all found .pak files in order to find the file.

File query priority

The PAK system prioritizes the files found in the loose folder structure over those in PAK, except when the engine was compiled in RELEASE mode. When that is the case, the file stored in a PAK system is preferred over the loose one.

If the file exists in multiple .pak archives, the one with the most recent filesystem creation date is used.

Attaching the debugger

Visual Studio allows you to attach the Debugger to your application. This allows you to use functionalities such as breakpoints; letting you stop at a specific line in your C++ source code, and step through the program execution.

To start debugging, open CE Game Programming Sample.sln and press F5, or click on the green play icon on the Visual Studio toolbar. If a No debug symbols could be found for Editor.exe message box appears, simply click on OK.

What just happened?

The CryENGINE Sandbox editor should now have started, with the Visual Studio Debugger attached. We can now place breakpoints in code, and watch program execution pause when that specific line of code is executed.

 

Summary


In this chapter, we have downloaded and learned how to use a CryENGINE installation. You should now be aware of the process of compiling and debugging the CryGame project.

We now have the basic knowledge needed to move on to learning the ins and outs of the CryENGINE programming API.

If you feel like learning more about CryENGINE itself, besides programming knowledge, feel free to start the Sandbox editor and play around with the level design tools. This will help you prepare for future chapters, where you'll be required to utilize the Editor Viewport and more.

About the Authors

  • Filip Lundgren

    Filip Lundgren is a Technical Director at Poppermost Production AB where he works on SNOW, the first free-to-play open-world skiing title. Filip has been working with CryENGINE since 2007 with the release of Crysis 1 SDK, and has developed community tools such as CryMono and the CryENGINE toolbox.

    Browse publications by this author
  • Ruan Pearce-Authers

    Ruan Pearce-Authers is a game programmer currently working for Crytek GmbH. In 2009, he began managing Crytek's development community, and providing technical support for CryENGINE users. Prior to this, he was active in the Crysis modding community himself, and followed this up by producing sample projects and additional tools for the Free SDK. He co-developed the CryMono engine extension to bring support for .NET to the CryENGINE, and the Tanks game sample written exclusively in C#. At present, Ruan works as part of the development team for Crytek's award-winning entrance, into the free-to-play market, Warface, in Frankfurt.

    Browse publications by this author
Book Title
Unlock this book and the full library for FREE
Start free trial