Menu
Lumberyard
Developer Guide (Version 1.11)

Integrating Lua and C++

CryLua is deprecated and will be removed in a future version of Lumberyard.

The CryScript system abstracts a Lua virtual machine for use by the other systems and the game code. It includes the following functionality:

  • calling script functions

  • exposing C++-based variables and functions to scripts

  • creating script tables stored in virtual machine memory

The CryScript system is based on Lua 5. More information on the Lua language can be found at http://www.lua.org.

Accessing Script Tables

A global script table can be retrieved by calling IScriptSystem::GetGlobalValue(). The IScriptTable is used to represent all script tables/variables.

Exposing C++ Functions and Values

To expose C++ functions and variables to scripts, you'll need to implement a new class. The easiest way is to derive the CScriptableBase class, which provides most of the functionality.

Exposing Constants

To expose constant values to scripts, use the IScriptSystem::SetGlobalValue(). For example, to expose a constant named MTL_LAYER_FROZEN to our scripts, use the following code:

Copy
gEnv->pScriptSystem->SetGlobalValue("MTL_LAYER_FROZEN", MTL_LAYER_FROZEN);

Exposing Functions

To expose C++ functions to scripts, implement a new class derives from CScriptableBase, as shown in the following example.

Copy
classCScriptBind_Game : publicCScriptableBase { public: CScriptBind_Game( ISystem* pSystem ); virtual ~CScriptBind_Game() {} intGameLog(IFunctionHandler* pH, char* pText); };

Add the following code inside the class constructor:

Copy
Init(pSystem->GetIScriptSystem(), pSystem); SetGlobalName("Game"); #undef SCRIPT_REG_CLASSNAME #define SCRIPT_REG_CLASSNAME &CScriptBind_Game:: SCRIPT_REG_TEMPLFUNC(GameLog, "text");

In a Lua script, you can access your new ScriptBind function as follows:

Copy
Game.GameLog("a message")