LuaUtil.wiki
author Wuzzy
Fri, 19 Apr 2019 16:07:50 +0100
changeset 1831 365c30b4c669
parent 1811 9e4f2d65879e
child 1845 a6be83d2fe45
permissions -rw-r--r--
LuaGears: Add temporary mistake to test documentation script
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1749
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
     1
#summary List of utility functions in the Lua API
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
     2
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
     3
= Lua API: Utility functions =
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
     4
This page is a list of utility functions in the [LuaAPI Lua API] and other functions that don't fit anywhere else.
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
     5
1801
efa4f04f15eb LuaUtil: Add TOC
Wuzzy
parents: 1763
diff changeset
     6
<wiki:toc max_depth="3" />
efa4f04f15eb LuaUtil: Add TOC
Wuzzy
parents: 1763
diff changeset
     7
1749
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
     8
== File system functions ==
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
     9
=== <tt>!HedgewarsScriptLoad(scriptPath [, mustExist])</tt> ===
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    10
Loads a script (i.e. a [LuaLibraries library]) from the specified `scriptPath`. The root directory is here Hedgewars’ data directory. There will be a Lua error if the script does not exist.
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    11
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    12
If `mustExist` is `false`, no Lua error will happen even when the script does not exist.
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    13
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    14
Returns `true` if the script was loaded successfully, `false` otherwise.
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    15
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    16
Example:
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    17
<code language="lua">
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    18
HedgewarsScriptLoad("/Scripts/Locale.lua")  -- loads locale library
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    19
</code>
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    20
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    21
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    22
== Math functions ==
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    23
=== <tt>div(dividend, divisor)</tt> ===
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    24
Performs an integer division and returns the result.
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    25
The result is an integer and has the value of the first parameter (an integer) divided by the second parameter (another integer), rounded towards zero.
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    26
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    27
=== <tt>band(value1, value2)</tt> ===
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    28
Returns the bitwise logical AND of `value1` and `value2`.
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    29
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    30
=== <tt>bor(value1, value2)</tt> ===
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    31
Returns the bitwise logical OR of `value1` and `value2`.
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    32
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    33
=== <tt>bnot(value)</tt> ===
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    34
Returns the bitwise logical NOT of `value`.
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    35
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    36
== Debugging functions ==
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    37
=== <tt>WriteLnToConsole(string)</tt> ===
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    38
Writes `string` to `Logs/game0.log`, found in the user data directory.
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    39
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    40
=== <tt>WriteLnToChat(string)</tt> (0.9.24) ===
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    41
Writes `string` into the chat.
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    42
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    43
=== <tt>DumpPoint(x, y)</tt> (0.9.23) ===
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    44
Converts the whole numbers `x` and `y` to strings and writes them to `Logs/game0.log`, one line each.
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    45
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    46
=== <tt>StartGhostPoints(count)</tt> ===
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    47
Just prints out “GHOST_POINTS” and the argument on the console. This function might change in later versions.
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    48
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    49
=== <tt>DeclareAchievement(id, teamname, location, value)</tt> ===
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    50
Declares an achievement with the identifier `id` achieved by the team `teamname` on the map `location` with an achievement value (e.g. score) of `value`. `value` must be an integer. You are supposed to call this function inside an `onAchievementsDeclaration` callback.
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    51
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    52
Currently, this actually just triggers a console output, but it might be changed later. The idea is to track multiplayer records.
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    53
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    54
Example:
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    55
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    56
<code language="lua">DeclareAchievement("height reached", teamname, "ClimbHome", -score)</code>
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    57
Records a team's best height in !ClimbHome.
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    58
1805
d18970cf6ca2 Update other pages for new Lua documentation structure
Wuzzy <almikes@aol.com>
parents: 1801
diff changeset
    59
=== <tt>!ParseCommand(string)</tt> ===
d18970cf6ca2 Update other pages for new Lua documentation structure
Wuzzy <almikes@aol.com>
parents: 1801
diff changeset
    60
Makes the game client parse and execute the specified internal game engine command.
d18970cf6ca2 Update other pages for new Lua documentation structure
Wuzzy <almikes@aol.com>
parents: 1801
diff changeset
    61
d18970cf6ca2 Update other pages for new Lua documentation structure
Wuzzy <almikes@aol.com>
parents: 1801
diff changeset
    62
The available commands depend on the current engine protocol version. The *engine protocol can (and will) change* between releases.
d18970cf6ca2 Update other pages for new Lua documentation structure
Wuzzy <almikes@aol.com>
parents: 1801
diff changeset
    63
d18970cf6ca2 Update other pages for new Lua documentation structure
Wuzzy <almikes@aol.com>
parents: 1801
diff changeset
    64
*Important*: If you use `ParseCommand` to overcome a shortcoming in our Lua API (e.g. a missing function), please make sure to [https://issues.hedgewars.org/enter_bug.cgi report the issue].
d18970cf6ca2 Update other pages for new Lua documentation structure
Wuzzy <almikes@aol.com>
parents: 1801
diff changeset
    65
d18970cf6ca2 Update other pages for new Lua documentation structure
Wuzzy <almikes@aol.com>
parents: 1801
diff changeset
    66
With your report we can fix the shortcoming in future releases. We will try to remove the reliance on `ParseCommand` as good as possible. This will allow scripts to use the previously missing feature in a way that won’t break!
d18970cf6ca2 Update other pages for new Lua documentation structure
Wuzzy <almikes@aol.com>
parents: 1801
diff changeset
    67
d18970cf6ca2 Update other pages for new Lua documentation structure
Wuzzy <almikes@aol.com>
parents: 1801
diff changeset
    68
There are many available commands, but actual use in scripting is rare, and even then it's discouraged for long-term use. As of 0.9.24, the only command used in official scripts is:
d18970cf6ca2 Update other pages for new Lua documentation structure
Wuzzy <almikes@aol.com>
parents: 1801
diff changeset
    69
d18970cf6ca2 Update other pages for new Lua documentation structure
Wuzzy <almikes@aol.com>
parents: 1801
diff changeset
    70
 * `"draw <map>"`: Draws a hand-drawn map. `MapGen` must be `mgDrawn` for this to work. `<map>` is a string which must follow the format specified in [DrawnMapFormat]
d18970cf6ca2 Update other pages for new Lua documentation structure
Wuzzy <almikes@aol.com>
parents: 1801
diff changeset
    71
d18970cf6ca2 Update other pages for new Lua documentation structure
Wuzzy <almikes@aol.com>
parents: 1801
diff changeset
    72
Moreover, the control action names as listed in [ConfigurationFiles] (under “Binds”) can be used. Note we will eventually try to remove all `ParseCommand`s in the official scripts.
d18970cf6ca2 Update other pages for new Lua documentation structure
Wuzzy <almikes@aol.com>
parents: 1801
diff changeset
    73
d18970cf6ca2 Update other pages for new Lua documentation structure
Wuzzy <almikes@aol.com>
parents: 1801
diff changeset
    74
=== <tt>!EndLuaTest(success)</tt> ===
d18970cf6ca2 Update other pages for new Lua documentation structure
Wuzzy <almikes@aol.com>
parents: 1801
diff changeset
    75
This function is used by the Hedgewars developers in testing scripts in order to test the game engine. The testing scripts can be found in the Hedgewars source code under `tests/lua`. This function is useless for anything else.
d18970cf6ca2 Update other pages for new Lua documentation structure
Wuzzy <almikes@aol.com>
parents: 1801
diff changeset
    76
d18970cf6ca2 Update other pages for new Lua documentation structure
Wuzzy <almikes@aol.com>
parents: 1801
diff changeset
    77
Calling this function ends an engine test and reports a test result.
d18970cf6ca2 Update other pages for new Lua documentation structure
Wuzzy <almikes@aol.com>
parents: 1801
diff changeset
    78
d18970cf6ca2 Update other pages for new Lua documentation structure
Wuzzy <almikes@aol.com>
parents: 1801
diff changeset
    79
`success` is either one of `TEST_SUCCESSFUL` to report a successful test or `TEST_FAILED` for a failed test.
d18970cf6ca2 Update other pages for new Lua documentation structure
Wuzzy <almikes@aol.com>
parents: 1801
diff changeset
    80
d18970cf6ca2 Update other pages for new Lua documentation structure
Wuzzy <almikes@aol.com>
parents: 1801
diff changeset
    81
See [EngineTestCases] to learn more about testing the engine.
d18970cf6ca2 Update other pages for new Lua documentation structure
Wuzzy <almikes@aol.com>
parents: 1801
diff changeset
    82
1749
91756d20ce3e Separate core Lua stuff into new Lua pages for better maintainability. Main LuaAPI page not touched yet
Wuzzy <almikes@aol.com>
parents:
diff changeset
    83
== Library functions ==
1811
9e4f2d65879e LuaUtil: fix bg
Wuzzy
parents: 1805
diff changeset
    84
For more utility functions, you might want to check out the [LuaLibraries Lua libraries].