author | almikes@aol.com |
Sun, 21 Dec 2014 01:29:43 +0000 | |
changeset 529 | f3e3229af8bf |
parent 528 | 88cc64932e78 |
child 530 | 9d1b7bddb6d2 |
permissions | -rw-r--r-- |
84
6fa418422e57
Created wiki page through web user interface.
henrik.rostedt@gmail.com
parents:
diff
changeset
|
1 |
#summary Libraries for writing Lua scripts in Hedgewars. |
6fa418422e57
Created wiki page through web user interface.
henrik.rostedt@gmail.com
parents:
diff
changeset
|
2 |
|
6fa418422e57
Created wiki page through web user interface.
henrik.rostedt@gmail.com
parents:
diff
changeset
|
3 |
= Introduction = |
6fa418422e57
Created wiki page through web user interface.
henrik.rostedt@gmail.com
parents:
diff
changeset
|
4 |
|
6fa418422e57
Created wiki page through web user interface.
henrik.rostedt@gmail.com
parents:
diff
changeset
|
5 |
Libraries in scripts in Hedgewars are lua files that are used by many scripts to add a common function, as an example the Locale library that allows scripts to translate text. The variables in these files are not exposed to the script using it but all the functions can be called. |
6fa418422e57
Created wiki page through web user interface.
henrik.rostedt@gmail.com
parents:
diff
changeset
|
6 |
|
6fa418422e57
Created wiki page through web user interface.
henrik.rostedt@gmail.com
parents:
diff
changeset
|
7 |
To use a library you only need to add one row at the top of the script: |
501
04b4b463bf33
Add HedgewarsScriptLoad, warn about the old and rusty loadfile function.
almikes@aol.com
parents:
500
diff
changeset
|
8 |
<code language="lua">HedgewarsScriptLoad("Scripts/<Library Name>.lua")</code> |
04b4b463bf33
Add HedgewarsScriptLoad, warn about the old and rusty loadfile function.
almikes@aol.com
parents:
500
diff
changeset
|
9 |
Where `<Library Name>` is replaced by the name. |
84
6fa418422e57
Created wiki page through web user interface.
henrik.rostedt@gmail.com
parents:
diff
changeset
|
10 |
|
501
04b4b463bf33
Add HedgewarsScriptLoad, warn about the old and rusty loadfile function.
almikes@aol.com
parents:
500
diff
changeset
|
11 |
*Note*: In old scripts, you will find this line instead: |
04b4b463bf33
Add HedgewarsScriptLoad, warn about the old and rusty loadfile function.
almikes@aol.com
parents:
500
diff
changeset
|
12 |
<code language="lua">loadfile(GetDataPath() .. "Scripts/<Library Name>.lua")()</code> |
525
b051cfd45c23
Edited wiki page LuaLibraries through web user interface.
almikes@aol.com
parents:
501
diff
changeset
|
13 |
This does not work with new Hedgewars versions anymore and causes the script to break. You have to replace it with `HedgewarsScriptLoad`. *Calls to `loadfile` are one of the most common reasons why old scripts do not work in recent Hedgewars versions.* |
85
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
14 |
|
527 | 15 |
= Table of Contents = |
16 |
||
17 |
<wiki:toc max_depth="2" /> |
|
84
6fa418422e57
Created wiki page through web user interface.
henrik.rostedt@gmail.com
parents:
diff
changeset
|
18 |
= Locale = |
6fa418422e57
Created wiki page through web user interface.
henrik.rostedt@gmail.com
parents:
diff
changeset
|
19 |
|
6fa418422e57
Created wiki page through web user interface.
henrik.rostedt@gmail.com
parents:
diff
changeset
|
20 |
This library allows you to translate string and should be used whenever a script has text. It loads the appropriate locale file and check automatically. |
6fa418422e57
Created wiki page through web user interface.
henrik.rostedt@gmail.com
parents:
diff
changeset
|
21 |
|
529 | 22 |
=== `loc(text)` === |
528 | 23 |
|
529 | 24 |
Returns the localised string of `text` or, if it is not found, it returns `text`. |
528 | 25 |
|
85
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
26 |
|
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
27 |
|
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
28 |
= Utils = |
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
29 |
|
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
30 |
This library includes miscellaneous functions to use, they are all independent of each other and can be used everywhere. |
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
31 |
|
528 | 32 |
=== `gearIsInBox(gear, x, y, w, h)` === |
33 |
||
85
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
34 |
Returns whether the gear is inside (centre point of the gear) a box with x and y as the top left corner and having the width and height of w and h respectively. |
528 | 35 |
|
85
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
36 |
|
528 | 37 |
=== `gearIsInCircle(gear, x, y, r, useRadius)` === |
38 |
||
85
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
39 |
Returns whether the gear is inside a circle with x and y being the centre point and r being the radius of the circle. The boolean useRadius determine whether only the centre point of the gear will be used or the radius of the gear will be checked too. |
528 | 40 |
|
85
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
41 |
|
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
42 |
|
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
43 |
= Tracker = |
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
44 |
|
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
45 |
This library is intended to be used if you need to keep track of gears. It can also keep track of teams and clans and as an extra functionality it can also store variables for a gear, team or clan. |
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
46 |
|
528 | 47 |
To set it up you need to add some hooks in different events. The hooks `trackGear` and `trackDeletion` to `onGearAdd` and `onGearDelete` respectively. It is strongly recommended to only track the gears you are interested in as, especially with snow on, the amount of gears can go up high and that will slow down the script. To keep track of teams you need to keep track of `gtHedgehog` and `gtResurrector` (if resurrection might be used) and add `trackTeams` to `onGameStart`. |
85
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
48 |
|
528 | 49 |
If you want to call a function on a couple of gears you will have to call the “`runOn`” functions. To these you will pass the function you want to be run as a variable to the function. The function must take a gear as a parameter, nothing else, for example: |
50 |
<code language="lua">function killall(gear) |
|
85
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
51 |
SetHealth(gear, 0) |
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
52 |
end |
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
53 |
|
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
54 |
function onGearDelete(gear) |
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
55 |
if GetGearType(gear) == gtTarget then |
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
56 |
runOnHogs(killall) |
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
57 |
end |
528 | 58 |
end</code> |
85
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
59 |
This will kill all hogs if a target is destroyed. |
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
60 |
|
103
023ff3c709ac
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
86
diff
changeset
|
61 |
To see a commented example of the tracker in use by a script you can look at |
528 | 62 |
[http://code.google.com/p/hedgewars/source/browse/share/hedgewars/Data/Scripts/Multiplayer/Random_Weapon.lua Random Weapons]. |
103
023ff3c709ac
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
86
diff
changeset
|
63 |
|
86 | 64 |
== Tracking functions == |
85
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
65 |
|
528 | 66 |
=== `trackGear(gear)` === |
67 |
||
85
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
68 |
Will keep track of the gear. |
528 | 69 |
|
70 |
||
71 |
=== `trackDeletion(gear)` === |
|
85
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
72 |
|
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
73 |
Will stop keeping track of the gear (gears not tracked will be ignored). |
528 | 74 |
|
85
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
75 |
|
528 | 76 |
=== `trackTeams()` === |
77 |
||
85
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
78 |
Will start the tracking of teams, clans and hedgehogs (hogs can be tracked without this). |
528 | 79 |
|
85
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
80 |
|
528 | 81 |
== “`runOn`” functions == |
85
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
82 |
|
528 | 83 |
=== `runOnGears(func)` === |
84 |
||
85 |
Runs the function `func` on all gears. |
|
86 |
||
85
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
87 |
|
528 | 88 |
=== `runOnHogs(func)` === |
89 |
||
90 |
Runs the function `func` on all hedgehogs. |
|
91 |
||
85
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
92 |
|
528 | 93 |
=== `runOnHogsInTeam(func, team)` === |
94 |
||
95 |
Runs the function `func` on all hedgehogs in the specified team. |
|
96 |
||
85
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
97 |
|
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
98 |
=== runOnHogsInOtherTeams(func, team) === |
528 | 99 |
|
100 |
Runs the function `func` on all hedgehogs but those in the specified team. |
|
101 |
||
102 |
||
103 |
=== `runOnHogsInClan(func, clan)` === |
|
85
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
104 |
|
528 | 105 |
Runs the function `func` on all hedgehogs in the specified clan. |
106 |
||
85
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
107 |
|
528 | 108 |
=== `runOnHogsInOtherClans(func, clan)` === |
109 |
||
110 |
Runs the function `func` on all hedgehogs but those in the specified clan. |
|
111 |
||
85
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
112 |
|
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
113 |
== Variable functions == |
9f1d2d3db844
Edited wiki page LuaLibraries through web user interface.
henrik.rostedt@gmail.com
parents:
84
diff
changeset
|
114 |
|
528 | 115 |
_To be continued …_ |
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
116 |
|
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
117 |
|
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
118 |
= Animate = |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
119 |
|
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
120 |
This library provides functions that aid cinematic/cut-scene creation and functions for handling events. |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
121 |
|
528 | 122 |
In order to use its full potential, the following lines need to be at the beginning of `onGameTick`. |
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
123 |
|
500 | 124 |
<code language="lua">function onGameTick() |
528 | 125 |
AnimUnWait() |
126 |
if ShowAnimation() == false then |
|
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
127 |
return |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
128 |
end |
528 | 129 |
ExecuteAfterAnimations() |
130 |
CheckEvents() |
|
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
131 |
end</code> |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
132 |
|
528 | 133 |
Also, `AnimInit()` needs to be called in `onGameInit()`. |
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
134 |
Each of these functions will be explained below. |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
135 |
|
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
136 |
== Cinematic Handling == |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
137 |
|
528 | 138 |
=== `ShowAnimation()` === |
139 |
Performs the current action in the cinematic and returns `true` if finished, otherwise `false`. It needs to be used in `onGameTick`. Cut-scenes need to be added with `AddAnim(steps)`. |
|
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
140 |
|
528 | 141 |
=== `AddAnim(steps)` === |
142 |
Adds `steps` to the array of animations, where `steps` is a table with numbers as keys and elements of the following form. Each step is a table having the following keys: `func`, `args`, `swh`. |
|
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
143 |
|
528 | 144 |
* `func` is the function to be executed. It can be any function that returns false when it needs to be called again in following game ticks (e.g. `AnimMove`). It can be one of the cinematic functions. |
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
145 |
|
528 | 146 |
* `args` is a table containing the arguments that need to be passed to the function given |
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
147 |
|
528 | 148 |
* `swh` is an optional boolean value that defaults to `true` and denotes whether the current hedgehog should be switched to the hog given as argument. |
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
149 |
Example: |
500 | 150 |
<code language="lua">cinem = { |
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
151 |
{func = AnimSay, args = {myHog, "Snails! SNAILS!", SAY_SAY, 3000}}, |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
152 |
{func = AnimMove, args = {myhog, "Left", 2000, 0}}, |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
153 |
{func = AddCaption, swh = false, args = {"But he found no more snails..."}} |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
154 |
} |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
155 |
AddAnim(cinem)</code> |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
156 |
|
528 | 157 |
=== `RemoveAnim(steps)` === |
158 |
Removes `steps` from the animations array. |
|
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
159 |
|
528 | 160 |
=== `AddSkipFunction(anim, func, args)` === |
161 |
Adds `func` to the array of functions used to skip animations, associating it with `anim`. When the animation is skipped (see below), the function is called with `args` as arguments. |
|
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
162 |
Example: |
500 | 163 |
<code language="lua">AddSkipFunc(cinem, SkipCinem, {})</code> |
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
164 |
|
528 | 165 |
=== `RemoveSkipFunction(anim)` === |
166 |
Removes the skip function associated with `anim`. |
|
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
167 |
|
528 | 168 |
=== `SetAnimSkip(bool)` === |
169 |
Sets the state of animation skipping to `bool`. It is useful in case the player is allowed to skip the animation. |
|
170 |
||
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
171 |
Example: |
500 | 172 |
<code language="lua">function onPrecise() |
528 | 173 |
if AnimInProgress() then |
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
174 |
SetAnimSkip(true) |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
175 |
end |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
176 |
end</code> |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
177 |
|
528 | 178 |
=== `AnimInProgress()` === |
179 |
Returns `true` if a cinematic is currently taking place, `false` otherwise. |
|
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
180 |
|
528 | 181 |
=== `ExecuteAfterAnimations()` === |
182 |
Calls the functions (added with `AddFunction`) that need to be executed after the cinematic. The best location for this function call is in `onGameTick`. |
|
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
183 |
|
528 | 184 |
=== `AddFunction(element)` === |
185 |
Adds `element` to the functions array that are to be called after the cinematic. `element` is a table with the keys: `func`, `args`. |
|
186 |
||
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
187 |
Example: |
500 | 188 |
<code language="lua">AddFunction({func = AfterCinem, args = {2}})</code> |
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
189 |
|
528 | 190 |
=== `RemoveFunction()` === |
191 |
Removes the first function from the aforementioned list. |
|
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
192 |
|
528 | 193 |
=== `AnimInit()` === |
194 |
Initializes variables used by the other functions. Needs to be called in `onGameInit`. |
|
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
195 |
|
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
196 |
=== !AnimUnWait() === |
528 | 197 |
Decreases the wait time used by cinematics. It is best called in `onGameTick`. |
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
198 |
|
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
199 |
== Cinematic Functions == |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
200 |
|
528 | 201 |
=== `AnimSwitchHog(gear)` === |
202 |
Switches to `gear` and follows it. |
|
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
203 |
|
528 | 204 |
=== `AnimWait(gear, time)` === |
205 |
Increases the wait time by `time`. `gear` is just for compatibility with `ShowAnimation`. |
|
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
206 |
|
528 | 207 |
=== `AnimSay(gear, text, manner, time` === |
208 |
Calls `HogSay` with the first three arguments and increses the wait time by `time`. |
|
209 |
||
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
210 |
Example: |
500 | 211 |
<code language="lua">cinem = { |
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
212 |
{func = AnimSay, args = {gear1, "You're so defensive!", SAY_SAY, 2500}}, |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
213 |
{func = AnimSay, args = {gear2, "No, I'm not!", SAY_SAY, 2000}} |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
214 |
}</code> |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
215 |
|
528 | 216 |
=== `AnimSound(gear, sound, time)` === |
217 |
Plays the sound `sound` and increases the wait time by `time`. |
|
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
218 |
|
528 | 219 |
=== `AnimTurn(hog, dir)` === |
220 |
Makes `hog` face in direction `dir`, where `dir` equals either `"Right"` or `"Left"`. |
|
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
221 |
|
528 | 222 |
=== `AnimMove(hog, dir, x, y)` === |
223 |
Makes `hog` move in direction `dir` until either his horizontal coordinate is `x` or his vertical coordinate is `y`. |
|
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
224 |
|
528 | 225 |
=== `AnimJump(hog, jumpType)` === |
226 |
Makes `hog` perform a jump of type `jumpType`, where `jumpType` equals either `"long"`, `"high"` or `"back"`. |
|
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
227 |
|
528 | 228 |
=== `AnimSetGearPosition(gear, x, y, fall)` === |
229 |
Sets the position of `gear` to (`x`, `y`). If the optional argument `fall` does not equal `false` then the gear is given a small falling velocity in order to get around exact positioning. |
|
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
230 |
|
528 | 231 |
=== `AnimDisappear(gear, x, y)` === |
232 |
Teleports the gear to (`x`, `y`), adding some effects at the previous position and sounds. Doesn’t follow the gear. |
|
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
233 |
|
528 | 234 |
=== `AnimOutOfNowhere(gear, x, y)` === |
235 |
Teleports the gear to (`x`, `y`), adding effects and sounds at the final position. Follows gear. |
|
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
236 |
|
528 | 237 |
=== `AnimTeleportGear(gear, x, y)` === |
238 |
Teleports the gear to (`x`, `y`), adding effects and sounds both at the starting position and the final position. Follows gear. |
|
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
239 |
|
528 | 240 |
=== `AnimVisualGear(gear, x, y, vgType, state, critical, follow)` === |
241 |
Calls `AddVisualGear` with arguments second to sixth. If the optional argument `follow` equals `true` then the gear is followed. `gear` is for compatibility only. |
|
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
242 |
|
528 | 243 |
=== `AnimCaption(gear, text, time)` === |
244 |
Adds `text` as caption and increases wait time by `time`. |
|
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
245 |
|
528 | 246 |
=== `AnimCustomFunction(gear, func, args)` === |
247 |
Calls the function `func` with `args` as arguments. This function is useful, for instance, when the cinematic uses the position of a gear at the moment of execution. If `func` needs to be called in following game ticks then it should return false. |
|
248 |
||
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
249 |
Example: |
500 | 250 |
<code language="lua">function NeedToTurn(hog1, hog2) |
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
251 |
if GetX(hog1) < GetX(hog2) then |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
252 |
HogTurnLeft(hog1, false) |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
253 |
HogTurnLeft(hog2, true) |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
254 |
else |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
255 |
HogTurnLeft(hog2, false) |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
256 |
HogTurnLeft(hog1, true) |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
257 |
end |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
258 |
end |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
259 |
|
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
260 |
cinem = {{func = AnimCustomFunction, args = {hog1, NeedToTurn, {hog1, hg2}}}}</code> |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
261 |
|
528 | 262 |
=== `AnimInsertStepNext(step)` === |
263 |
Inserts `step` after the current step (read: action) in the cinematic array. Useful when an action depends on variables (e.g. positoins). It can be used mostly with `AnimCustomFunction`. Note: In case of multiple consecutive calls, steps need to be added in reverse order. |
|
264 |
||
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
265 |
Example: |
500 | 266 |
<code language="lua">function BlowHog(deadHog) |
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
267 |
AnimInsertStepNext({func = DeleteGear, args = {deadHog}, swh = false}) |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
268 |
AnimInsertStepNext({func = AnimVisualGear, args = {deadHog, GetX(deadHog), GetY(deadHog), vgtBigExplosion, 0, true}, swh = false}) |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
269 |
AnimInsertStepNext({func = AnimWait, args = {deadHog, 1200}}) |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
270 |
AnimInsertStepNext({func = AnimVisualGear, args = {deadHog, GetX(deadHog) + 20, GetY(deadHog), vgtExplosion, 0, true}, swh = false}) |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
271 |
AnimInsertStepNext({func = AnimWait, args = {deadHog, 100}}) |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
272 |
AnimInsertStepNext({func = AnimVisualGear, args = {deadHog, GetX(deadHog) + 10, GetY(deadHog), vgtExplosion, 0, true}, swh = false}) |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
273 |
AnimInsertStepNext({func = AnimWait, args = {deadHog, 100}}) |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
274 |
AnimInsertStepNext({func = AnimVisualGear, args = {deadHog, GetX(deadHog) - 10, GetY(deadHog), vgtExplosion, 0, true}, swh = false}) |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
275 |
AnimInsertStepNext({func = AnimWait, args = {deadHog, 100}}) |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
276 |
AnimInsertStepNext({func = AnimVisualGear, args = {deadHog, GetX(deadHog) - 20, GetY(deadHog), vgtExplosion, 0, true}, swh = false}) |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
277 |
end |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
278 |
cinem = {{func = AnimCustomFunction, args = {liveHog, BlowHog, {deadHog}}}}</code> |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
279 |
|
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
280 |
== Event Handling == |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
281 |
|
528 | 282 |
Events are pairs of functions that are used to check for various conditions. One of them is for verification and, if it returns `true`, the second function is called. |
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
283 |
|
528 | 284 |
=== `AddEvent(condFunc, condArgs, doFunc, doArgs, evType)` === |
285 |
Adds the functions `condFunc` and `doFunc` to the events list. They get called with the respective args (`condArgs` and `doArgs`). `condFunc` will get called in every game tick until it returns `true` or is removed. Once it returns `true`, `doFunc` is called and they are or are not removed from the list, depending on `evType` (`0` for removal, `1` for keeping). An `evType` of `1` is useful for repeating actions (e.g. every time a hog gets damaged, do something). |
|
286 |
||
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
287 |
Example: |
500 | 288 |
<code language="lua">function CheckPos() |
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
289 |
return GetX(myHog) > 1500 |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
290 |
end |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
291 |
function CheckAmmo() |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
292 |
return GetAmmoCount(myHog, amGrenade) == 0 |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
293 |
end |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
294 |
function DoPos() |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
295 |
ShowMission("Scooter", "Mover", "Nice Work", 0, 0) |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
296 |
end |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
297 |
function DoAmmo() |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
298 |
AddAmmo(myHog, amGrenade, 5) |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
299 |
end |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
300 |
AddEvent(CheckPos, {}, DoPos, {}, 0) -- Add event that gets removed on completion |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
301 |
AddEvent(CheckAmmo, {}, DoAmmo, {}, 1) -- Add repeating event</code> |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
302 |
|
528 | 303 |
=== `AddNewEvent(condFunc, condArgs, doFunc, doArgs, evType)` === |
304 |
Does the same as `AddEvent`, but first checks if the event is already in the list so that it isn’t added twice. |
|
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
305 |
|
528 | 306 |
=== `RemoveEventFunc(cFunc, cArgs)` === |
307 |
Removes the event or events that have `cFunc` as the condition checking function. If `cArgs` does not equal `nil` then only those events get removed that have `cArgs` as arguments for `cFunc`, too. |
|
308 |
||
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
309 |
Example: |
500 | 310 |
<code language="lua">AddEvent(condFunc1, condArgs1, doFunc, doArgs) |
246
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
311 |
AddEvent(condFunc1, condArgs2, doFunc, doArgs) |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
312 |
AddEvent(condFunc1, condArgs2, doFunc, doArgs) |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
313 |
AddEvent(condFunc2, condArgs1, doFunc, doArgs) |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
314 |
AddEvent(condFunc2, condArgs2, doFunc, doArgs) |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
315 |
RemoveEventFunc(condFunc1) --Removes all three events that have condFunc1 |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
316 |
RemoveEventFunc(condFunc2, condArgs1) --Removes a single event</code> |
7b1a6c46c3b5
Edited wiki page LuaLibraries through web user interface.
szabibibi@gmail.com
parents:
103
diff
changeset
|
317 |
|
528 | 318 |
=== `CheckEvents` === |
319 |
<blockquote> Verifies all the condition functions in the events list and calls the respective ‘action’ functions if the conditions are met. If the `evType` of a completed event equals `0` then it is removed from the list. This function is best placed in `onGameTick`. |
|
526 | 320 |
|
321 |
||
322 |
= Params = |
|
323 |
The Params library is a small library introduced in 0.9.21. It provides a function to parse the parameter string `ScriptParam` and it is intended to simplify using this string. |
|
324 |
||
325 |
This library requires the parameter string to be in a certain format. It must be a comma-seperated list of kev-value pairs in the `key=value` format. Examples: |
|
326 |
{{{ |
|
327 |
speed=1 |
|
328 |
health=100, ammo=5 |
|
329 |
param1=hello, param2=whatever, param3=5325.4 |
|
330 |
}}} |
|
331 |
||
332 |
Using this library is by no means neccessary. |
|
333 |
||
528 | 334 |
=== `parseParams()` === |
526 | 335 |
Parses `ScriptParam` and writes the result into the global table `params`. The table will follow the `key=value` pattern. Both keys and pairs are stored as string. |
336 |
||
528 | 337 |
==== Example ==== |
526 | 338 |
<code language="lua"> |
339 |
function onParameters() |
|
340 |
parseParams() |
|
341 |
if params["myparam1"] == "hello" then |
|
342 |
WriteLnToConsole("Hello World!") |
|
343 |
end |
|
528 | 344 |
end</code> |
526 | 345 |
|
346 |
If the key-value pair `myparam1=hello` is present, this script writes “Hello World!” in the console. All these inputs would trigger the event: |
|
347 |
||
348 |
{{{ |
|
349 |
myparam1=hello |
|
350 |
myparam2=whatever, myparam1=hello |
|
351 |
g=4, f=56, m=56, myparam1=hello |
|
352 |
}}} |