Lua in Games
Mr. Nguyễn Ngọc Vân
Ass Software Manager – VNG Corporation
Agenda
1. Lua & Benefits
2. Core engine & Lua model
3. Lua integrations model
4. Secure Lua code
5. Disadvantages
1. Lua & Benefits
• Scripting language
• Works through LVM (Lua Virtual Machine)
• Stack model design
• “Ultimate game scripting language” (GDC 2010)
• Popular uses in games
• Runs on most machines and devices
• Written in ANSI C and distributed by small library
What’s Lua?
• Free and open source 
• Support OOP
• Runs on almost platforms
• Easy to interface with C/C++
• Very clean C API
• Auto memory management (GC)
• Powerful, fast and lightweight
• Flexible data structure (TABLE)
Lua Benefits
• Contents can be changed in real-time
• Auto generate contents
• Users can customize/change contents
• Fast and easy distributed contents
• Secure code
• Easy to use, embed and learn
• Good references
Lua Benefits
2. Core Engine & Lua Model
Configs
Settings
(XML, INI, T
XT, …)
Game Logic Process
(Behaviors, AI, …)
Control Flow
Core Engine
Configs
Settings
(…, LUA)
Lua Engine
Control Flow
Core Engine
LUA Logic
Process
(Behaviors,
AI, …)
3. Lua Integrations Model
Core Logic
(Logic Flow)
Lua Engine
(LVM)
Lua
codes
Lua APIs
Lua interface
Core Engine
• Lua APIs
– Call to LVM through events/triggers base
– C APIs to work with LVM
– Auxiliary library provides basic functions to check,
register, debug, … LVM
– Use C APIs to build core functions
• Lua Interface
– Call to core logic through exports APIs
– Provides APIs (game APIs) to work with core logic
– Use C APIs to build, register, and exports APIs
function OnPlayerUseItem(nItem)
core.InstanceObj(OBJ_KIND_ITEM, nItem)
if item.GetKind() == ITM_KIND_HP then
local nItemHP = item.GetAttribute(ITM_ATTR_HP)
local nPlayerHP = player.GetCurrentHP()
local nAddHP = 0.2 * nPlayerHP
if nAddHP > 0 and nAddHP < player.GetMaxHP() then
player.SetCurrentHP(nAddHP)
player.Message('Player:
'..player.GetName()..' use item '..item.GetName())
end
end
end
Quick Sample
4. Secure Lua Code
• Compile to byte-code
– Popular use
– Use luaC module included in library
– Modify luaC with keys for more secure
• Encrypted
– Little use
– Use keys-pair to encrypted
– Use signed file, CRC
• Simple code
function Add(a, b)
return a + b
end
print(Add(100, 200))
• Byte-code
5. Disadvantages
• C APIs are too low-level
• Error messages are very confusing 
• No try-catch, no exceptions
• Hard to debug
• Hard to interfaces with unlike C/C++
• Once LVM is crashed, it’s not likely to recover and the
service is crashed too 
• Hard to implement reload feature at run-time
• For extreme speed, LuaJIT is a good choice 
Lua in games

Lua in games

  • 1.
    Lua in Games Mr.Nguyễn Ngọc Vân Ass Software Manager – VNG Corporation
  • 2.
    Agenda 1. Lua &Benefits 2. Core engine & Lua model 3. Lua integrations model 4. Secure Lua code 5. Disadvantages
  • 3.
    1. Lua &Benefits
  • 4.
    • Scripting language •Works through LVM (Lua Virtual Machine) • Stack model design • “Ultimate game scripting language” (GDC 2010) • Popular uses in games • Runs on most machines and devices • Written in ANSI C and distributed by small library What’s Lua?
  • 5.
    • Free andopen source  • Support OOP • Runs on almost platforms • Easy to interface with C/C++ • Very clean C API • Auto memory management (GC) • Powerful, fast and lightweight • Flexible data structure (TABLE) Lua Benefits
  • 6.
    • Contents canbe changed in real-time • Auto generate contents • Users can customize/change contents • Fast and easy distributed contents • Secure code • Easy to use, embed and learn • Good references Lua Benefits
  • 7.
    2. Core Engine& Lua Model
  • 8.
    Configs Settings (XML, INI, T XT,…) Game Logic Process (Behaviors, AI, …) Control Flow Core Engine
  • 9.
    Configs Settings (…, LUA) Lua Engine ControlFlow Core Engine LUA Logic Process (Behaviors, AI, …)
  • 10.
  • 11.
    Core Logic (Logic Flow) LuaEngine (LVM) Lua codes Lua APIs Lua interface Core Engine
  • 12.
    • Lua APIs –Call to LVM through events/triggers base – C APIs to work with LVM – Auxiliary library provides basic functions to check, register, debug, … LVM – Use C APIs to build core functions • Lua Interface – Call to core logic through exports APIs – Provides APIs (game APIs) to work with core logic – Use C APIs to build, register, and exports APIs
  • 13.
    function OnPlayerUseItem(nItem) core.InstanceObj(OBJ_KIND_ITEM, nItem) ifitem.GetKind() == ITM_KIND_HP then local nItemHP = item.GetAttribute(ITM_ATTR_HP) local nPlayerHP = player.GetCurrentHP() local nAddHP = 0.2 * nPlayerHP if nAddHP > 0 and nAddHP < player.GetMaxHP() then player.SetCurrentHP(nAddHP) player.Message('Player: '..player.GetName()..' use item '..item.GetName()) end end end Quick Sample
  • 14.
  • 15.
    • Compile tobyte-code – Popular use – Use luaC module included in library – Modify luaC with keys for more secure • Encrypted – Little use – Use keys-pair to encrypted – Use signed file, CRC
  • 16.
    • Simple code functionAdd(a, b) return a + b end print(Add(100, 200)) • Byte-code
  • 17.
  • 18.
    • C APIsare too low-level • Error messages are very confusing  • No try-catch, no exceptions • Hard to debug • Hard to interfaces with unlike C/C++ • Once LVM is crashed, it’s not likely to recover and the service is crashed too  • Hard to implement reload feature at run-time • For extreme speed, LuaJIT is a good choice 