Lua API: Event handlers
Event handlers are 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.
Initialization
onGameInit()
This function is called before the game loads its resources. One can read and modify various
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.
onGameStart()
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.
onPreviewInit()
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.
onParameters()
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.
onAmmoStoreInit()
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.
onNewAmmoStore(teamOrClanIndex, hogIndex)
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
onGameTick()
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
.
onGameTick20()
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
onNewTurn()
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
)
onEndTurn()
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.
onSkipTurn()
This function calls when a hog skips its turn.
onCaseDrop(gear)
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.
onSuddenDeath()
This function is called on the start of Sudden Death.
onGameResult(winningClan)
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.
onAchievementsDeclaration()
This function is called after the stats for the stats screen (after the game) have been generated. You are supposed to call
DeclareAchievement
here.
onHogAttack(ammoType)
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.
onHogSwitch(oldHog)
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.
onUsedAmmo(ammoType)
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
onAttack()
This function is called when the current player presses the attack key.
onHJump()
This function is called when the current player presses the high jump key.
onLJump()
This function is called when the current player presses the long jump key.
onPrecise()
This function is called when the current player presses the precise key.
onLeft()
This function is called when the current player presses the left key.
onRight()
This function is called when the current player presses the right key.
onUp()
This function is called when the current player presses the up key.
onDown()
This function is called when the current player presses the down key.
onAttackUp()
This function is called when the current player releases the attack key.
onLeftUp()
This function is called when the current player releases the left key.
onRightUp()
This function is called when the current player releases the right key.
onDownUp()
This function is called when the current player releases the down key.
onUpUp()
This function is called when you release the up key.
onPreciseUp()
This function is called when the current player releases the precise key.
onSwitch()
This function is called when the current player presses the switch key.
onSetWeapon(msgParam)
It is get called when a weapon is selected or switched.
msgParam
tells you which ammo type was selected.
onSlot(msgParam)
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.).
onTaunt(msgParam)
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
|
onTimer(msgParam)
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).
onScreenResize()
This function is called when you resize the screen. Useful place to put a redraw function for any
vgtHealthTag
s you're using.
Gears
onGearAdd(gearUid)
This function is called when a new gear is added. Useful in combination with
GetGearType(gearUid)
.
onGearDelete(gearUid)
This function is called when a new gear is deleted. Useful in combination with
GetGearType(gearUid)
.
onVisualGearAdd(vgUid)
This function is called when a new visual gear is added. Useful in combination with
GetVisualGearType(vgUid)
.
onVisualGearDelete(vgUid)
This function is called when a new visual gear is deleted. Useful in combination with
GetVisualGearType(vgUid)
.
onGearDamage(gearUid, damage)
This function is called when a gear is damaged.
Example:
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
onGearResurrect(gearUid, spawnedVGear)
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.
onGearWaterSkip(gear)
This function is called when the gear
gear
skips over water.
onHogHide(gearUid)
This function is called when a hedgehog with the gear ID
gearUid
is hidden (removed from the map).
onHogRestore(gearUid)
This function is called when a hedgehog with the specified gear ID
gearUid
is restored (unhidden).
Map changes
onSpritePlacement(spriteId, centerX, centerY)
This function is called when a
Sprite has been placed.
spriteID
is the type of the sprite, you find a list at Sprites.
centerX
and
centerY
are the coordinates of the center of the sprite.
onGirderPlacement(frameIdx, centerX, centerY)
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.
onRubberPlacement(frameIdx, centerX, centerY)
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.
onSpecialPoint(x, y, flags)
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.