Download - Lua in games
![Page 1: Lua in games](https://reader033.vdocuments.net/reader033/viewer/2022052220/554d285eb4c905ab268b4d64/html5/thumbnails/1.jpg)
Lua in Games
Mr. Nguyễn Ngọc VânAss Software Manager – VNG Corporation
![Page 2: Lua in games](https://reader033.vdocuments.net/reader033/viewer/2022052220/554d285eb4c905ab268b4d64/html5/thumbnails/2.jpg)
Agenda
1. Lua & Benefits2. Core engine & Lua model3. Lua integrations model4. Secure Lua code5. Disadvantages
![Page 3: Lua in games](https://reader033.vdocuments.net/reader033/viewer/2022052220/554d285eb4c905ab268b4d64/html5/thumbnails/3.jpg)
1. Lua & Benefits
![Page 4: Lua in games](https://reader033.vdocuments.net/reader033/viewer/2022052220/554d285eb4c905ab268b4d64/html5/thumbnails/4.jpg)
• 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?
![Page 5: Lua in games](https://reader033.vdocuments.net/reader033/viewer/2022052220/554d285eb4c905ab268b4d64/html5/thumbnails/5.jpg)
• 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
![Page 6: Lua in games](https://reader033.vdocuments.net/reader033/viewer/2022052220/554d285eb4c905ab268b4d64/html5/thumbnails/6.jpg)
• 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
![Page 7: Lua in games](https://reader033.vdocuments.net/reader033/viewer/2022052220/554d285eb4c905ab268b4d64/html5/thumbnails/7.jpg)
2. Core Engine & Lua Model
![Page 8: Lua in games](https://reader033.vdocuments.net/reader033/viewer/2022052220/554d285eb4c905ab268b4d64/html5/thumbnails/8.jpg)
ConfigsSettings
(XML, INI, TXT, …)
Game Logic Process(Behaviors, AI, …)
Control Flow
Core Engine
![Page 9: Lua in games](https://reader033.vdocuments.net/reader033/viewer/2022052220/554d285eb4c905ab268b4d64/html5/thumbnails/9.jpg)
ConfigsSettings(…, LUA)
Lua Engine
Control Flow
Core Engine
LUA Logic Process
(Behaviors, AI, …)
![Page 10: Lua in games](https://reader033.vdocuments.net/reader033/viewer/2022052220/554d285eb4c905ab268b4d64/html5/thumbnails/10.jpg)
3. Lua Integrations Model
![Page 11: Lua in games](https://reader033.vdocuments.net/reader033/viewer/2022052220/554d285eb4c905ab268b4d64/html5/thumbnails/11.jpg)
Core Logic(Logic Flow)
Lua Engine(LVM)
Lua codes
Lua APIs
Lua interface
Core Engine
![Page 12: Lua in games](https://reader033.vdocuments.net/reader033/viewer/2022052220/554d285eb4c905ab268b4d64/html5/thumbnails/12.jpg)
• 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
![Page 13: Lua in games](https://reader033.vdocuments.net/reader033/viewer/2022052220/554d285eb4c905ab268b4d64/html5/thumbnails/13.jpg)
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 * nPlayerHPif nAddHP > 0 and nAddHP < player.GetMaxHP() then
player.SetCurrentHP(nAddHP)player.Message('Player:
'..player.GetName()..' use item '..item.GetName())end
endend
Quick Sample
![Page 14: Lua in games](https://reader033.vdocuments.net/reader033/viewer/2022052220/554d285eb4c905ab268b4d64/html5/thumbnails/14.jpg)
4. Secure Lua Code
![Page 15: Lua in games](https://reader033.vdocuments.net/reader033/viewer/2022052220/554d285eb4c905ab268b4d64/html5/thumbnails/15.jpg)
• 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
![Page 16: Lua in games](https://reader033.vdocuments.net/reader033/viewer/2022052220/554d285eb4c905ab268b4d64/html5/thumbnails/16.jpg)
• Simple codefunction Add(a, b)
return a + bendprint(Add(100, 200))
• Byte-code
![Page 17: Lua in games](https://reader033.vdocuments.net/reader033/viewer/2022052220/554d285eb4c905ab268b4d64/html5/thumbnails/17.jpg)
5. Disadvantages
![Page 18: Lua in games](https://reader033.vdocuments.net/reader033/viewer/2022052220/554d285eb4c905ab268b4d64/html5/thumbnails/18.jpg)
• 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
![Page 19: Lua in games](https://reader033.vdocuments.net/reader033/viewer/2022052220/554d285eb4c905ab268b4d64/html5/thumbnails/19.jpg)