How To Make A Game In Lua And C++

CryENGINE Game Programming with C, C#, and Lua. By Filip Lundgren, Ruan Pearce-Authers. $5 for 5 months Subscribe Access now. $26.99 eBook Buy. 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. If you want something more complex, you're probably going to need to use the actual Lua API. Luckily, the Lua API is reasonably simple. We're using OOLua for Pioneer. It gives a pile of macros that you use to bind Lua 'classes' to C classes. Its got a fair bit of power but is pretty straightforward.

  1. How To Make A Game In Lua And C++ 2

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

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.

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:CrytekCryENGINE-Programming-Sample.

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.

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.

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.

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:

Now that we have the CryMono source code, we'll need to compile it. /jojo27s-fashion-show-2-mac-free-download/.

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:

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/.

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 ..CryMonoMonoDllHeaders 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.

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

How To Make A Game In Lua And C++

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

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

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

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

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:

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

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.

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.

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.

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.

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.

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.

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.

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.

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 aDynamic 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.

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 solution includes the following three projects, one of which compiles to a .dll file.

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

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.

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.

How To Make A Game In Lua And C++ 2

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.

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.

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.

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.

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.

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.