LuaEvents.wiki
author Wuzzy <Wuzzy@disroot.org>
Mon, 27 May 2024 19:59:44 +0200
changeset 2270 22a964a1fe85
parent 2231 f00c578955f7
permissions -rw-r--r--
Add Worms/Hedgewars taunt conversions

#summary List of event handlers in the Lua API
#labels !LuaFunctions

= Lua API: Event handlers =

Event handlers are [LuaAPI Lua] functions that Hedgewars calls on certain events. Lua scripts are supposed to _define_ these functions to do something. The functions are then _called_ by Hedgewars when a certain event has occured. For an example of how this works, see [LuaGuide].

This page is a list of all supported event handlers in Hedgewars.

<wiki:toc max_depth="3" />

== Initialization ==
=== <tt>onGameInit()</tt> ===
This function is called before the game loads its resources. One can read and modify various [LuaGlobals#Game_variables game variables] here. These variables will become globally available after `onGameInit` has been invoked, but changing them has only an effect in `onGameInit`.

Most game variables are optional, but for missions, `Theme` must be set by the scripter if you want to use a random map, rather than an image map. All other variables do not need to be set by the scripter and have default values. For a list of game variables that can be set here, see [LuaGlobals#Game_variables].

If you want to add teams or hogs manually, you have to do it here. If you want to draw your own map using `AddPoint` and `FlushPoints`, you have to do this within this function as well.

=== <tt>onGameStart()</tt> ===
This function is called when the first round starts.

Can be used to show the mission and for more setup, for example initial target spawning.

At this stage, the global variables `LeftX`, `RightX`, `TopY`, `LAND_WIDTH` and `LAND_HEIGHT` become available.

=== <tt>onPreviewInit()</tt> ===
This function is called when the map preview in the frontend is initialized. This happens when the script is selected or you change a map generator parameter.

It is useful for scripts which create their own maps (see `AddPoint` and `FlushPoints`). If you create a map in this function, a preview will be generated from this map and is exposed to the frontend.

=== <tt>onParameters()</tt> ===
This function is called when the script parameters (as specified in the game scheme) become available. The script parameter string is stored in the global variable `ScriptParam`.

Please note that it is normally not safe to call many of the other functions inside this function, this function is called very early in the game, only use this to initialize variables and other internal stuff like that.

*Tip*: If you use the Params library  (`/Scripts/Params.lua`), you can make the task of dissecting the string into useful values a bit easier, but it’s not required. (The Params library is not documented yet, however).

*Tip*: If you use this callback, make sure to document the interpretation of the parameters so others know how to set the parameters properly.

=== <tt>onAmmoStoreInit()</tt> ===
This function is called when the game is initialized to request the available ammo, ammo probabilities and other ammo settings. Use `SetAmmo` here.

If you add this event handler, then the weapon scheme (in a multiplayer game) will be ignored, and the settings of all ammos be initialized to the following values:

 * Initial ammo: 0
 * Crate probability: 0
 * Number in crate: 0
 * Delay: 0

If this event handler is *not* present, then the game will use the weapon scheme that the player selected (in multiplayer games). In missions, all ammos are always initialized at 0 by default, even without this event handler.

=== <tt>onNewAmmoStore(teamOrClanIndex, hogIndex)</tt> ===
This function is identical to `onAmmoStoreInit` in functionality, but is called once per ammo store.  This allows different ammo sets for each clan, team or hedgehog depending on the mode.

 * If `gfSharedAmmo` is set, then `teamOrClanIndex` is the clan index and `hogIndex` is `-1`. The function will be called once for each clan.
 * If `gfPerHogAmmo` is set, then`teamOrClanIndex` is the team index and `hogIndex` is the hog index in that team. The function will be called once for each hedgehog.
 * If neither is set, then `teamsOrClanIndex` is the team index and `hogIndex` is `-1`. The function will be called once for each team.

These indexes can be used to look up details of the clan/team/hedgehog prior to gear creation. Routines to do these lookups will be created as needed.
If you add this hook, the expectation is that you will call `SetAmmo` appropriately. Any values from `onAmmoStoreInit` are ignored.

== Time events ==
=== <tt>onGameTick()</tt> ===
This function is called on every game tick, i.e. 1000 times a second. If you just need to check on something periodically, consider `onGameTick20`.

=== <tt>onGameTick20()</tt> ===
This function is called every 20 game ticks, which equals 50 times a second. It reduces Lua overhead for simple monitoring that doesn’t need to happen every single tick.

== Game events ==
=== <tt>onNewTurn()</tt> ===
This function calls at the start of every turn. You can set `ReadyTimeLeft` here to change the ready time for this turn. (See also: `Ready`)

=== <tt>onEndTurn()</tt> ===
This function calls at the end of every turn. The end of a turn is defined as the point of time after the current hedgehog lost control and all the important gears are either gone or have settled.

`CurrentHedgehog` holds the gear ID of the hedgehog whose turn just ended.

This function is called at one of the earliest possible moment after the end of a turn. After this callback, Hedgewars then performs all the other stuff between turns. This includes things like: Applying poison or Sudden Death damage, calculating total hog damage, rising the water in Sudden Death, dropping a crate, checking victory, giving control to the next hog.

Because this function is called *before* victories are checked, this is useful to set up your victory conditions here.

=== <tt>onSkipTurn()</tt> ===
This function calls when a hog skips its turn.

=== <tt>onCaseDrop(gear)</tt> ===
This function calls between two turns right after the moment at which the game *might* drop a crate according to the game scheme settings. It does not matter if it actually wants to drop a crate.

If a crate was dropped, `gear` is the crate gear that was dropped, if no crate was dropped, `gear` is `nil`.

This function is useful to add custom crate drops as well.

=== <tt>onSuddenDeath()</tt> ===
This function is called on the start of Sudden Death.

=== <tt>onGameResult(winningClan)</tt> ===
This function calls when the game ends with a winner or in a draw. If a clan wins, `winningClan` is the clan ID of the winning clan. If the game ends in a draw, `winningClan` is set to -1.

=== <tt>onAchievementsDeclaration()</tt> ===
This function is called after the stats for the stats screen (after the game) have been generated. You are supposed to call `DeclareAchievement` here.

=== <tt>onHogAttack(ammoType)</tt> ===
This function is called when a hedgehog attacks. Beginning with 0.9.21, the parameter `ammoType` is provided. It contains the ammo type of the weapon used for the attack. 

Note: If you want to detect when a turn was skipped, use `onSkipTurn()`. There is no guarantee that `onHogAttack(amSkip)` is called in such an event.

=== <tt>onHogSwitch(oldHog)</tt> ===
Called when hogs have been switched using the “switch hedgehog” utility. `oldHog` is the gear ID of the previous hedgehog and the current hedgehog gear ID is stored in `CurrentHedgehog`.

Note: If you want to detect when a turn was skipped, use `onSkipTurn()`. There is no guarantee that `onHogAttack(amSkip)` is called in such an event.

=== <tt>onUsedAmmo(ammoType)</tt> ===
Called after a weapon has been used completely, with `ammoType` as the used ammo type.

For example, it is called right after a bazooka is fired, when both shots of a shotgun have been fired, when extra time is used, or when all 4 shots of a portable portal device have been fired. It is also called when using a multi-shot ammo has been aborted by changing the weapon selection mid-way, because this still uses up the ammo.

== Controls ==
=== <tt>onAttack()</tt> ===
This function is called when the current player presses the attack key.

=== <tt>onHJump()</tt> ===
This function is called when the current player presses the high jump key.

=== <tt>onLJump()</tt> ===
This function is called when the current player presses the long jump key.

=== <tt>onPrecise()</tt> ===
This function is called when the current player presses the precise key.

=== <tt>onLeft()</tt> ===
This function is called when the current player presses the left key.

=== <tt>onRight()</tt> ===
This function is called when the current player presses the right key.

=== <tt>onUp()</tt> ===
This function is called when the current player presses the up key.

=== <tt>onDown()</tt> ===
This function is called when the current player presses the down key.

=== <tt>onAttackUp()</tt> ===
This function is called when the current player releases the attack key.

=== <tt>onLeftUp()</tt> ===
This function is called when the current player releases the left key.

=== <tt>onRightUp()</tt> ===
This function is called when the current player releases the right key.

=== <tt>onDownUp()</tt> ===
This function is called when the current player releases the down key.

=== <tt>onUpUp()</tt> ===
This function is called when you release the up key. 

=== <tt>onPreciseUp()</tt> ===
This function is called when the current player releases the precise key.

=== <tt>onSwitch()</tt> ===
This function is called when the current player presses the switch key.

=== <tt>onSetWeapon(msgParam)</tt> ===
It is get called when a weapon is selected or switched.

`msgParam` tells you which ammo type was selected.

=== <tt>onSlot(msgParam)</tt> ===
This function is called when one of the weapon slot keys has been pressed.

`msgParam` tells the slot number minus 1 (i.e. `0` is for slot number 1, `1` is for slot number 2, etc.).

=== <tt>onTaunt(msgParam)</tt> ===
This function is called when the player uses an animated emote for example by using the chat commands `/wave`, `/juggle`, etc.

`msgParam` tells you which animation was played:

|| *`msgParam`* || *Animation* || *Associated chat command* ||
|| 0 || Rolling up || `/rollup` ||
|| 1 || Sad face || `/sad` ||
|| 2 || Waving hand || `/wave` ||
|| 3 || Stupid winner's grin / “Awesome” face || `/hurrah` ||
|| 4 || Peeing || `/ilovelotsoflemonade` ||
|| 5 || Shrug || `/shrug` ||
|| 6 || Juggling || `/juggle` ||

=== <tt>onTimer(msgParam)</tt> ===
This function is called when one of the timer keys is pressed.

`msgParams` tells the set timer in seconds (i.e. `3` for the 3 seconds timer key).

=== <tt>onScreenResize()</tt> ===
This function is called when you resize the screen. Useful place to put a redraw function for any `vgtHealthTag`s you're using.

== Gears ==
=== <tt>onGearAdd(gearUid)</tt> ===
This function is called when a new gear is added. Useful in combination with `GetGearType(gearUid)`.

=== <tt>onGearDelete(gearUid)</tt> ===
This function is called when a new gear is deleted. Useful in combination with `GetGearType(gearUid)`.

=== <tt>onVisualGearAdd(vgUid)</tt> ===
This function is called when a new visual gear is added. Useful in combination with `GetVisualGearType(vgUid)`.

=== <tt>onVisualGearDelete(vgUid)</tt> ===
This function is called when a new visual gear is deleted. Useful in combination with `GetVisualGearType(vgUid)`.

=== <tt>onGearDamage(gearUid, damage)</tt> ===
This function is called when a gear is damaged.

Example:

<code language="lua">    function onGearDamage(gear, damage)
        if (GetGearType(gear) === gtHedgehog) then
            -- adds a message saying, e.g. "Hoggy H took 25 points of damage"
            AddCaption(GetHogName(gear) .. ' took ' .. damage .. ' points of damage')
        end
    end</code>
=== <tt>onGearResurrect(gearUid, spawnedVGear) </tt> ===
This function is called when a gear is resurrected due to the hog effect `heResurrectable` being set (see `SetEffect`) and/or being an AI hog when the game modifier “AI Survival” (`gfAISurvival`) is active. It is *not* called when a hog was resurrected by the resurrector tool you can use in the game.

`spawnedVGear` is a visual gear handle of the “resurrection effect”. You can use this handle to modify or delete the resurrection animation.

=== <tt>onGearWaterSkip(gear)</tt> ===
This function is called when the gear `gear` skips over water.

=== <tt>onHogHide(gearUid)</tt> ===
This function is called when a hedgehog with the gear ID `gearUid` is hidden (removed from the map).

=== <tt>onHogRestore(gearUid)</tt> ===
This function is called when a hedgehog with the specified gear ID `gearUid` is restored (unhidden).

== Map changes ==
=== <tt>onSpritePlacement(spriteId, centerX, centerY)</tt> ===
This function is called when a [Sprites Sprite] has been placed.

`spriteID` is the type of the sprite, you find a list at [Sprites Sprites]. `centerX` and `centerY` are the coordinates of the center of the sprite.

=== <tt>onGirderPlacement(frameIdx, centerX, centerY)</tt> ===
This function is called when a girder has been placed.

`frameIdx` is used for the length and orientation of the girder. The possible values are explained in `PlaceGirder`. `centerX` and `centerY` are the coordinates of the girder’s center.

=== <tt>onRubberPlacement(frameIdx, centerX, centerY)</tt> ===
This function is called when a rubber has been placed.

`frameIdx` is used for the rubber orientation. The possible values are explained in `PlaceRubber`. `centerX` and `centerY` are the coordinates of the rubber’s center.

=== <tt>onSpecialPoint(x, y, flags)</tt> ===
This is used while a special hand-drawn map is loaded. The engine is building these hand-drawn maps by reading points from the map definition. Optionally, some of these points may be “special”. These are not actually drawn on the map, but are used to store additional information for a position on the map. Special points currently need to be added manually in the map, the in-game editor is not able to add those yet.
Now, when such a special point at the coordinates `x` and `y` with an assigned value of `flags` is added, this function is called. `flags` is a whole number between `0` and `127` inclusive.

This function is used in Racer and !TechRacer to define waypoints.