Difference between revisions of "LuaAPI"
(→Context Specifiers: Update description to say icon instead of parens) |
(Bump up the header level of the toplevel sections to make the section boundaries more defined) |
||
Line 2: | Line 2: | ||
All Lua API functionality is available in one of several global module tables, as well as some global functions which form the basic Lua API. Wesnoth uses [http://www.lua.org/manual/5.4/manual.html Lua 5.4] which is documented in detail on the Lua website. | All Lua API functionality is available in one of several global module tables, as well as some global functions which form the basic Lua API. Wesnoth uses [http://www.lua.org/manual/5.4/manual.html Lua 5.4] which is documented in detail on the Lua website. | ||
− | + | == Conventions used in this manual == | |
On this page and any page linked from it, variable values will be shown in italic type, while literal names will be shown in bold type. Variable values may be parameters, self values, indexes, or field names on a table. Return values will be indicated by a right arrow (→) followed by a list of names in italics. The arrow will be omitted for a function that returns nothing. Optional portions will be enclosed in square brackets. The commas for optional parameters will not be included in the brackets, even though they must be omitted if the optional parameter is omitted. | On this page and any page linked from it, variable values will be shown in italic type, while literal names will be shown in bold type. Variable values may be parameters, self values, indexes, or field names on a table. Return values will be indicated by a right arrow (→) followed by a list of names in italics. The arrow will be omitted for a function that returns nothing. Optional portions will be enclosed in square brackets. The commas for optional parameters will not be included in the brackets, even though they must be omitted if the optional parameter is omitted. | ||
Line 59: | Line 59: | ||
* {{LuaPluginOnly}} Available in the plugins context. | * {{LuaPluginOnly}} Available in the plugins context. | ||
− | + | == Built-in Modules == | |
The following built-in Lua modules are available: | The following built-in Lua modules are available: | ||
Line 72: | Line 72: | ||
* [http://www.lua.org/manual/5.4/manual.html#6.10 debug] — only the <tt>traceback</tt> function is available | * [http://www.lua.org/manual/5.4/manual.html#6.10 debug] — only the <tt>traceback</tt> function is available | ||
− | + | == Wesnoth Modules == | |
Wesnoth also provides several modules of its own, some of which are loaded by default while others require you to load them as needed. | Wesnoth also provides several modules of its own, some of which are loaded by default while others require you to load them as needed. | ||
Line 98: | Line 98: | ||
* [[LuaAPI/unit_test|unit_test]] - Functions that can be used to write unit tests; only available in [test] scenarios. | * [[LuaAPI/unit_test|unit_test]] - Functions that can be used to write unit tests; only available in [test] scenarios. | ||
− | + | == Wesnoth Types == | |
Wesnoth defines a large number of ''userdata'' types, some of which are quite common and complex. Most of them are documented alongside the function that creates them, but some of the most common or complex ones are listed below. | Wesnoth defines a large number of ''userdata'' types, some of which are quite common and complex. Most of them are documented alongside the function that creates them, but some of the most common or complex ones are listed below. |
Revision as of 04:25, 5 July 2021
All Lua API functionality is available in one of several global module tables, as well as some global functions which form the basic Lua API. Wesnoth uses Lua 5.4 which is documented in detail on the Lua website.
Conventions used in this manual
On this page and any page linked from it, variable values will be shown in italic type, while literal names will be shown in bold type. Variable values may be parameters, self values, indexes, or field names on a table. Return values will be indicated by a right arrow (→) followed by a list of names in italics. The arrow will be omitted for a function that returns nothing. Optional portions will be enclosed in square brackets. The commas for optional parameters will not be included in the brackets, even though they must be omitted if the optional parameter is omitted.
Functions
For example, the following line would be seen in the documentation of a function that returns two values and has an optional parameter:
- wesnoth.some_function(some_parameter, [some_optional_parameter]) → first_return_value, second_return_value
Methods
The following example documents a function that can be called as a method on a some_value, taking one additional parameter and returning one value.
- some_value:some_method(some_parameter) → some_new_value
Variadic Functions
Functions that take a variable number of parameters or return a variable number of values where the specific meaning of the parameters is unspecified will use the string "..." to indicate this. For example:
- wesnoth.variadic_function(required_param, [...])
- wesnoth.get_several_values(param) → ...
The first of these takes a variable number of parameters, but requires at least one parameter. The second takes one parameter and returns a variable number of values.
Member Variables
Not all documented APIs are functions. When documenting userdata types, as well as some complex tables returned by API functions, the following syntax is used for member variables. The right arrow may be replaced with a left arrow (←) if it is write-only, or a double-ended arrow (↔) if it is read-write. Read-only members may return multiple values. The following examples document a read-write member, a read-only member that returns two values, and a write-only member respectively.
- some_value.some_member ↔ result
- some_value.read_only_member → first_result, second_result
- some_value.write_only_member ← input
Iterators
For functions that return an iterator, a double arrow indicates the values yielded when you use the iterator in a for loop. The following example documents a function returning an iterator that yields three values.
- wesnoth.some_function() → iterator ⇒ a, b, c
Hooks
Some parts of the API are so-called hooks, meaning that you define a function in a special location and it has some effect on the game. The following example documents a hook which takes one parameter and returns a value. Usually the double-ended arrow is used for hooks, but some may be write-only.
- wesnoth.some_table.name ↔ function(parameter) → expected return value
Context Specifiers
Many functions and variables are only available in certain contexts. Currently, there are three possible contexts - plugins, map generators, and the game. Most Lua will run in the game kernel. If a function is only available in certain contexts, that will be indicated with an icon at the beginning of the definition. For example:
- ♟ wesnoth.game_exclusive_function(parameter) → result
The possible icons are:
- ♟ Available in the game context.
- ⌖ Available in the map generation context.
- 🔌 Available in the plugins context.
Built-in Modules
The following built-in Lua modules are available:
- basic — except dofile, require, and loadfile; also note that print redirects to the Lua console interface rather than standard output; you can use std_print if you need the default Lua behavior.
- coroutine
- string
- utf8
- table
- math — note that math.random is unsafe for MP usage; wesnoth provides a separate MP-safe random function
- os — only the clock, date, time, and difftime functions are available (but keep in mind that they are not MP safe)
- debug — only the traceback function is available
Wesnoth Modules
Wesnoth also provides several modules of its own, some of which are loaded by default while others require you to load them as needed.
- wesnoth (Core) - The wesnoth module contains most of the core Wesnoth API. It is always loaded and available.
- wesnoth.interface (Game Interface) - (Version 1.15.0 and later only) The interface submodule contains functions for querying or manipulating the in-game UI.
- wesnoth.units (Units) - (Version 1.15.0 and later only) The units submodule contains functions for manipulating units on the map or recall list.
- wesnoth.schedule (Schedule) - (Version 1.15.14 and later only) The schedule submodule contains functions for manipulating the time of day schedule in the scenario.
- wesnoth.sides (Sides) - (Version 1.15.3 and later only) The sides submodule contains functions for manipulating sides in the scenario.
- wesnoth.sync (Synchronization) - (Version 1.15.3 and later only) The sync submodule contains functions for keeping multiplayer games synchronized.
- wesnoth.map (Game Map) - (Version 1.15.11 and later only) The map submodule contains functions for querying and manipulating the game map.
- wesnoth.audio (Audio) - (Version 1.15.13 and later only) The audio submodule contains functions for playing music and sound effects.
- wesnoth.paths (Pathfinding) - (Version 1.15.14 and later only) The paths submodule contains functions related to pathfinding.
- wesnoth.game_events (Event Handling) - The game_events submodule contains functions and hooks related to event handling.
- gui (User Interface) - (Version 1.15.0 and later only) The gui module contains routines for showing dialogs on the screen. It does not contain anything for manipulating the in-game UI, however.
- gui.widget (GUI Widget Support) - (Version 1.15.6 and later only) The widget submodule contains routines for operating on dialog widgets. Since they take a widget reference an argument, they can only be called while a dialog is onscreen.
- wml (WML Tables) - The wml module contains functions for working with WML tables.
- location_set (Location Sets) - A module for working with sets of locations, optionally associating data to each location. Not loaded by default.
- functional - Higher-order functions module. Not loaded by default.
- stringx - Additional string support functions, to augment the built-in string module.
- mathx - (Version 1.15.13 and later only) Additional math and randomization support functions, to augment the built-in math module.
- filesystem - (Version 1.15.13 and later only) Functions that allow read-only access to arbitrary files in the data folder.
- ai - Functions that instruct the AI on how to move.
- wml-utils - Useful functions for implementing custom WML tags.
- unit_test - Functions that can be used to write unit tests; only available in [test] scenarios.
Wesnoth Types
Wesnoth defines a large number of userdata types, some of which are quite common and complex. Most of them are documented alongside the function that creates them, but some of the most common or complex ones are listed below.
- translatable strings - a string that will be translated by the engine.
- vconfig - a WML object that automatically substitutes variables on the fly.
- unit - represents a reference to Wesnoth unit on the map, on the recall list, or private to Lua.
- weapon - represents a unit's weapon.
- race - represents a unit's race.
- unit type - represents a unit's type.
- side - represents a single side (player) in the game.
- widget - represents a GUI2 widget.
- map - represents the game map.
- hex - represents a single hex on the map.