2024-07-16 20:29:20 +02:00
|
|
|
# Lua Scripting System
|
|
|
|
|
|
|
|
|
|
## Configuration
|
2023-08-20 18:16:33 +03:00
|
|
|
|
2024-12-04 16:50:01 +00:00
|
|
|
```json
|
2023-08-20 18:16:33 +03:00
|
|
|
{
|
2026-02-10 23:19:46 +02:00
|
|
|
//general purpose script, Lua, runs on server
|
2023-08-20 18:16:33 +03:00
|
|
|
"myScript":
|
|
|
|
|
{
|
|
|
|
|
"source":"path/to/script/with/ext",
|
|
|
|
|
"implements":"ANYTHING"
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//custom battle spell effect, Lua only, runs on both client and server
|
|
|
|
|
//script ID will be used as effect 'type' (with mod prefix)
|
|
|
|
|
"mySpellEffect":
|
|
|
|
|
{
|
|
|
|
|
"source":"path/to/script/with/ext",
|
|
|
|
|
"implements":"BATTLE_EFFECT"
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
//TODO: object|building type
|
|
|
|
|
//custom map object or building, Lua only, runs on both client and server
|
|
|
|
|
//script ID will be used as 'handler' (with mod prefix)
|
|
|
|
|
"myObjectType":
|
|
|
|
|
{
|
|
|
|
|
"source":"path/to/script/with/ext",
|
|
|
|
|
"implements":"MAP_OBJECT"
|
|
|
|
|
},
|
|
|
|
|
//TODO: server query
|
|
|
|
|
//TODO: client query
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2024-07-16 20:29:20 +02:00
|
|
|
## Lua
|
2023-08-20 18:16:33 +03:00
|
|
|
|
2024-07-16 20:29:20 +02:00
|
|
|
### API Reference
|
2023-08-20 18:16:33 +03:00
|
|
|
|
2023-09-01 15:25:21 +03:00
|
|
|
TODO **In near future Lua API may change drastically several times. Information here may be outdated**
|
2023-08-20 18:16:33 +03:00
|
|
|
|
2024-07-16 20:29:20 +02:00
|
|
|
#### Globals
|
2023-08-20 18:16:33 +03:00
|
|
|
|
2023-09-01 15:25:21 +03:00
|
|
|
- DATA - persistent table
|
|
|
|
|
- GAME - IGameInfoCallback API
|
|
|
|
|
- BATTLE - IBattleInfoCallback API
|
|
|
|
|
- EVENT_BUS - opaque handle, for use with events API
|
|
|
|
|
- SERVICES - root "raw" access to all static game objects
|
|
|
|
|
- - SERVICES:artifacts()
|
|
|
|
|
- - SERVICES:creatures()
|
|
|
|
|
- - SERVICES:factions()
|
|
|
|
|
- - SERVICES:heroClasses()
|
|
|
|
|
- - SERVICES:heroTypes()
|
|
|
|
|
- - SERVICES:spells()
|
|
|
|
|
- - SERVICES:skills()
|
|
|
|
|
- require(URI)
|
|
|
|
|
- -works similar to usual Lua require
|
|
|
|
|
- -require("ClassName") - loads additional API and returns it as table (for C++ classes)
|
|
|
|
|
- -require("core:relative.path.to.module") - loads module from "SCRIPTS/LIB"
|
|
|
|
|
- -TODO require("modName:relative.path.to.module") - loads module from dependent mod
|
|
|
|
|
- -TODO require(":relative.path.to.module") - loads module from same mod
|
|
|
|
|
- logError(text) - backup error log function
|
2023-08-20 18:16:33 +03:00
|
|
|
|
2024-07-16 20:29:20 +02:00
|
|
|
#### Low level events API
|
2023-08-20 18:16:33 +03:00
|
|
|
|
2024-12-05 20:39:29 +00:00
|
|
|
```lua
|
2023-08-20 18:16:33 +03:00
|
|
|
|
|
|
|
|
-- Each event type must be loaded first
|
|
|
|
|
local PlayerGotTurn = require("events.PlayerGotTurn")
|
|
|
|
|
|
|
|
|
|
-- in this example subscription handles made global, do so if there is no better place
|
|
|
|
|
-- !!! do not store them in local variables
|
|
|
|
|
sub1 = PlayerGotTurn.subscribeAfter(EVENT_BUS, function(event)
|
|
|
|
|
--do smth
|
|
|
|
|
end)
|
|
|
|
|
|
|
|
|
|
sub2 = PlayerGotTurn.subscribeBefore(EVENT_BUS, function(event)
|
|
|
|
|
--do smth
|
|
|
|
|
end)
|
|
|
|
|
```
|
|
|
|
|
|
2024-07-16 20:29:20 +02:00
|
|
|
#### Lua standard library
|
2023-08-20 18:16:33 +03:00
|
|
|
|
2023-09-01 15:25:21 +03:00
|
|
|
VCMI uses LuaJIT, which is Lua 5.1 API, see [upstream documentation](https://www.lua.org/manual/5.1/manual.html)
|
2023-08-20 18:16:33 +03:00
|
|
|
|
|
|
|
|
Following libraries are supported
|
|
|
|
|
|
2024-11-30 20:20:15 +00:00
|
|
|
- base
|
|
|
|
|
- table
|
|
|
|
|
- string
|
|
|
|
|
- math
|
|
|
|
|
- bit
|