LuaLibraries.wiki
changeset 500 dc66ea78b36d
parent 246 7b1a6c46c3b5
child 501 04b4b463bf33
equal deleted inserted replaced
499:58b5889fbce4 500:dc66ea78b36d
     3 = Introduction =
     3 = Introduction =
     4 
     4 
     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.
     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.
     6 
     6 
     7 To use a library you only need to add one row at the top of the script:
     7 To use a library you only need to add one row at the top of the script:
     8 <code lang="lua">
     8 <code language="lua">
     9 loadfile(GetDataPath() .. "Scripts/<Library Name>.lua")()
     9 loadfile(GetDataPath() .. "Scripts/<Library Name>.lua")()
    10 </code>
    10 </code>
    11 Where <Library Name> is replaced by the name. Do not forget the "()" at the end as this initialises the file.
    11 Where <Library Name> is replaced by the name. Do not forget the "()" at the end as this initialises the file.
    12 
    12 
    13 
    13 
    41 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.
    41 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.
    42 
    42 
    43 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.
    43 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.
    44 
    44 
    45 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:
    45 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:
    46 <code lang="lua">
    46 <code language="lua">
    47 function killall(gear)
    47 function killall(gear)
    48     SetHealth(gear, 0)
    48     SetHealth(gear, 0)
    49 end
    49 end
    50 
    50 
    51 function onGearDelete(gear)
    51 function onGearDelete(gear)
   117 
   117 
   118 This library provides functions that aid cinematic/cut-scene creation and functions for handling events. 
   118 This library provides functions that aid cinematic/cut-scene creation and functions for handling events. 
   119 
   119 
   120 In order to use it's full potential, the following lines need to be at the beginning of onGameTick.
   120 In order to use it's full potential, the following lines need to be at the beginning of onGameTick.
   121 
   121 
   122 <code lang="lua">function onGameTick()
   122 <code language="lua">function onGameTick()
   123     !AnimUnWait()
   123     !AnimUnWait()
   124     if !ShowAnimation() == false then
   124     if !ShowAnimation() == false then
   125         return
   125         return
   126     end
   126     end
   127     !ExecuteAfterAnimations()
   127     !ExecuteAfterAnimations()
   143 
   143 
   144   * _args_ is a table containing the arguments that need to be passed to the function given
   144   * _args_ is a table containing the arguments that need to be passed to the function given
   145 
   145 
   146   * _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.</blockquote>
   146   * _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.</blockquote>
   147 Example:
   147 Example:
   148 <code lang="lua">cinem = {
   148 <code language="lua">cinem = {
   149     {func = AnimSay, args = {myHog, "Snails! SNAILS!", SAY_SAY, 3000}},
   149     {func = AnimSay, args = {myHog, "Snails! SNAILS!", SAY_SAY, 3000}},
   150     {func = AnimMove, args = {myhog, "Left", 2000, 0}},
   150     {func = AnimMove, args = {myhog, "Left", 2000, 0}},
   151     {func = AddCaption, swh = false, args = {"But he found no more snails..."}}
   151     {func = AddCaption, swh = false, args = {"But he found no more snails..."}}
   152     }
   152     }
   153 AddAnim(cinem)</code>
   153 AddAnim(cinem)</code>
   156 <blockquote>Removes steps from the animations array.</blockquote>
   156 <blockquote>Removes steps from the animations array.</blockquote>
   157 
   157 
   158 === !AddSkipFunction(anim, func, args) ===
   158 === !AddSkipFunction(anim, func, args) ===
   159 <blockquote>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.</blockquote>
   159 <blockquote>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.</blockquote>
   160 Example:
   160 Example:
   161 <code lang="lua">AddSkipFunc(cinem, SkipCinem, {})</code>
   161 <code language="lua">AddSkipFunc(cinem, SkipCinem, {})</code>
   162 
   162 
   163 === !RemoveSkipFunction(anim) ===
   163 === !RemoveSkipFunction(anim) ===
   164 <blockquote> Removes the skip function associated with _anim_.</blockquote>
   164 <blockquote> Removes the skip function associated with _anim_.</blockquote>
   165 
   165 
   166 === !SetAnimSkip(bool) ===
   166 === !SetAnimSkip(bool) ===
   167 <blockquote> Sets the state of animation skipping to _bool_. It is useful in case the player is allowed to skip the animation.</blockquote>
   167 <blockquote> Sets the state of animation skipping to _bool_. It is useful in case the player is allowed to skip the animation.</blockquote>
   168 Example:
   168 Example:
   169 <code lang="lua">function onPrecise()
   169 <code language="lua">function onPrecise()
   170     if !AnimInProgress() then
   170     if !AnimInProgress() then
   171         SetAnimSkip(true)
   171         SetAnimSkip(true)
   172     end
   172     end
   173 end</code>
   173 end</code>
   174 
   174 
   179 <blockquote> Calls the functions (added with !AddFunction) that need to be executed after the cinematic. The best location for this function call is in onGameTick.</blockquote>
   179 <blockquote> Calls the functions (added with !AddFunction) that need to be executed after the cinematic. The best location for this function call is in onGameTick.</blockquote>
   180 
   180 
   181 === !AddFunction(element) ===
   181 === !AddFunction(element) ===
   182 <blockquote> Adds _element_ to the functions array that are to be called after the cinematic. _element_ is a table with the keys: func, args.</blockquote>
   182 <blockquote> Adds _element_ to the functions array that are to be called after the cinematic. _element_ is a table with the keys: func, args.</blockquote>
   183 Example:
   183 Example:
   184 <code lang="lua">AddFunction({func = AfterCinem, args = {2}})</code>
   184 <code language="lua">AddFunction({func = AfterCinem, args = {2}})</code>
   185 
   185 
   186 === !RemoveFunction() ===
   186 === !RemoveFunction() ===
   187 <blockquote> Removes the first function from the aforementioned list.</blockquote>
   187 <blockquote> Removes the first function from the aforementioned list.</blockquote>
   188 
   188 
   189 === !AnimInit() ===
   189 === !AnimInit() ===
   201 <blockquote> Increases the wait time by _time_. _gear_ is just for compatibility with !ShowAnimation.</blockquote>
   201 <blockquote> Increases the wait time by _time_. _gear_ is just for compatibility with !ShowAnimation.</blockquote>
   202 
   202 
   203 === !AnimSay(gear, text, manner, time) ===
   203 === !AnimSay(gear, text, manner, time) ===
   204 <blockquote> Calls HogSay with the first three arguments and increses the wait time by _time_.</blockquote>
   204 <blockquote> Calls HogSay with the first three arguments and increses the wait time by _time_.</blockquote>
   205 Example:
   205 Example:
   206 <code lang="lua">cinem = {
   206 <code language="lua">cinem = {
   207     {func = AnimSay, args = {gear1, "You're so defensive!", SAY_SAY, 2500}},
   207     {func = AnimSay, args = {gear1, "You're so defensive!", SAY_SAY, 2500}},
   208     {func = AnimSay, args = {gear2, "No, I'm not!", SAY_SAY, 2000}}
   208     {func = AnimSay, args = {gear2, "No, I'm not!", SAY_SAY, 2000}}
   209    }</code>
   209    }</code>
   210 
   210 
   211 === !AnimSound(gear, sound, time) ===
   211 === !AnimSound(gear, sound, time) ===
   239 <blockquote> Adds _text_ as caption and increases wait time by _time_.</blockquote>
   239 <blockquote> Adds _text_ as caption and increases wait time by _time_.</blockquote>
   240 
   240 
   241 === !AnimCustomFunction(gear, func, args) ===
   241 === !AnimCustomFunction(gear, func, args) ===
   242 <blockquote> 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.</blockquote>
   242 <blockquote> 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.</blockquote>
   243 Example:
   243 Example:
   244 <code lang="lua">function NeedToTurn(hog1, hog2)
   244 <code language="lua">function NeedToTurn(hog1, hog2)
   245    if GetX(hog1) < GetX(hog2) then
   245    if GetX(hog1) < GetX(hog2) then
   246       HogTurnLeft(hog1, false)
   246       HogTurnLeft(hog1, false)
   247       HogTurnLeft(hog2, true)
   247       HogTurnLeft(hog2, true)
   248    else
   248    else
   249       HogTurnLeft(hog2, false)
   249       HogTurnLeft(hog2, false)
   254 cinem = {{func = AnimCustomFunction, args = {hog1, NeedToTurn, {hog1, hg2}}}}</code>
   254 cinem = {{func = AnimCustomFunction, args = {hog1, NeedToTurn, {hog1, hg2}}}}</code>
   255 
   255 
   256 === !AnimInsertStepNext(step) ===
   256 === !AnimInsertStepNext(step) ===
   257 <blockquote> 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.</blockquote>
   257 <blockquote> 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.</blockquote>
   258 Example:
   258 Example:
   259 <code lang="lua">function BlowHog(deadHog)
   259 <code language="lua">function BlowHog(deadHog)
   260   AnimInsertStepNext({func = DeleteGear, args = {deadHog}, swh = false}) 
   260   AnimInsertStepNext({func = DeleteGear, args = {deadHog}, swh = false}) 
   261   AnimInsertStepNext({func = AnimVisualGear, args = {deadHog, GetX(deadHog), GetY(deadHog), vgtBigExplosion, 0, true}, swh = false})
   261   AnimInsertStepNext({func = AnimVisualGear, args = {deadHog, GetX(deadHog), GetY(deadHog), vgtBigExplosion, 0, true}, swh = false})
   262   AnimInsertStepNext({func = AnimWait, args = {deadHog, 1200}})
   262   AnimInsertStepNext({func = AnimWait, args = {deadHog, 1200}})
   263   AnimInsertStepNext({func = AnimVisualGear, args = {deadHog, GetX(deadHog) + 20, GetY(deadHog), vgtExplosion, 0, true}, swh = false})
   263   AnimInsertStepNext({func = AnimVisualGear, args = {deadHog, GetX(deadHog) + 20, GetY(deadHog), vgtExplosion, 0, true}, swh = false})
   264   AnimInsertStepNext({func = AnimWait, args = {deadHog, 100}})
   264   AnimInsertStepNext({func = AnimWait, args = {deadHog, 100}})
   275 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.
   275 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.
   276 
   276 
   277 === !AddEvent(condFunc, condArgs, doFunc, doArgs, evType) ===
   277 === !AddEvent(condFunc, condArgs, doFunc, doArgs, evType) ===
   278 <blockquote> 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).</blockquote>
   278 <blockquote> 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).</blockquote>
   279 Example:
   279 Example:
   280 <code lang="lua">function CheckPos()
   280 <code language="lua">function CheckPos()
   281    return GetX(myHog) > 1500
   281    return GetX(myHog) > 1500
   282 end
   282 end
   283 function CheckAmmo()
   283 function CheckAmmo()
   284    return GetAmmoCount(myHog, amGrenade) == 0
   284    return GetAmmoCount(myHog, amGrenade) == 0
   285 end
   285 end
   296 <blockquote> Does the same as !AddEvent, but first checks if the event is already in the list so that it isn't added twice.</blockquote>
   296 <blockquote> Does the same as !AddEvent, but first checks if the event is already in the list so that it isn't added twice.</blockquote>
   297 
   297 
   298 === !RemoveEventFunc(cFunc, cArgs) ===
   298 === !RemoveEventFunc(cFunc, cArgs) ===
   299 <blockquote> Removes the event(s) that have _cFunc_ as the condition checking function. If _cArgs_ is not nil then only those events get removed that have _cArgs_ as arguments for _cFunc_, too.</blockquote>
   299 <blockquote> Removes the event(s) that have _cFunc_ as the condition checking function. If _cArgs_ is not nil then only those events get removed that have _cArgs_ as arguments for _cFunc_, too.</blockquote>
   300 Example:
   300 Example:
   301 <code lang="lua">AddEvent(condFunc1, condArgs1, doFunc, doArgs)
   301 <code language="lua">AddEvent(condFunc1, condArgs1, doFunc, doArgs)
   302 AddEvent(condFunc1, condArgs2, doFunc, doArgs)
   302 AddEvent(condFunc1, condArgs2, doFunc, doArgs)
   303 AddEvent(condFunc1, condArgs2, doFunc, doArgs)
   303 AddEvent(condFunc1, condArgs2, doFunc, doArgs)
   304 AddEvent(condFunc2, condArgs1, doFunc, doArgs)
   304 AddEvent(condFunc2, condArgs1, doFunc, doArgs)
   305 AddEvent(condFunc2, condArgs2, doFunc, doArgs)
   305 AddEvent(condFunc2, condArgs2, doFunc, doArgs)
   306 RemoveEventFunc(condFunc1) --Removes all three events that have condFunc1
   306 RemoveEventFunc(condFunc1) --Removes all three events that have condFunc1