Events

Everything mods can react to. Listen with on(name, function(e) ... end).

Everything that happens in the game reaches mods as an event. A handler is a function that gets one argument, the event table e: its fields describe what happened (see the Events page for every event). Some fields are writable (e.g. e.damage of hit): change them and the game uses your value. Setting e.cancelled = true on a cancellable event stops what it announces (e.g. a key reaching the game). Handlers run in priority order, then in the order they were added; later handlers see what earlier ones changed.

Custom events work the same way: events.emit("mymod:boss_spawned", {creature = c}) reaches every mod's on("mymod:boss_spawned", ...). Prefix custom names with your mod's id.

EventWhenCancellable
mods_loadedOnce after every script mod has loaded (at start and after each reload).
unloadBefore the scripts are unloaded: F9 / Apply in the Mods screen, or quitting the game.
frameEvery frame (about 60 times a second), in menus and hunts.
stageThe game screen changed (main menu, hunt menu, loading, hunt ...).
new_huntA hunt is being set up, before the game places its creatures.
hunt_startThe first frame of a hunt (the hunter is on the map).
hunt_endThe hunt is over (back to the menus).
spawnA creature was added: by the game's hunt setup, a mod, or the world API.
shotThe hunter fired.
hitA shot of the hunter hit a creature.yes
creature_damageA creature hurt another one (creature fights: world.fights).yes
killA creature died.
player_deathThe hunter is about to die.yes
creature_aiEvery frame, for every creature, before its AI runs.yes
creature_after_aiEvery frame, for every creature, after its AI (and the world API's fights / possession) ran.
keyA key went down or up (menus and hunts; not while the mod menu or the console is open).yes
backEsc was pressed: the game's back key (menus: back; hunt: the menu opens, or closes when it is open).yes
drawEvery frame while some mod listens, inside the UI frame.
noticeA creature notices the hunter: it sees him, smells him, or hears a noise he made.yes
noiseThe hunter made a noise creatures can hear (a shot).yes
trophyThe hunter took a trophy (the drop ship picks the creature up).
callThe hunter used the call (decoy).yes
weapon_changeThe hunter changes weapons.yes
player_damageThe hunter loses health to poisonous fog or drowning (every frame it goes on).yes
mouseA mouse button went down or up, or the wheel turned (not while the mod menu or the console is open).yes
net_joinedA player joined the session (see Multiplayer).
net_leftA player left the session.
net_messageA message from another game arrived (every topic; net.on(topic, fn) for one).
net_consentThe player answered a mod's net.request, or changed a permission in the Network tab.
settingA player changed a setting of some mod in the mod menu.

# on("mods_loaded", function(e) ... end)

When: Once after every script mod has loaded (at start and after each reload).

The place for setup that needs other mods: mods.get(id) returns their exports by now.

on("mods_loaded", function()
  local lib = mods.get("uilib")
  if lib then lib.register("My mod") end
end)

# on("unload", function(e) ... end)

When: Before the scripts are unloaded: F9 / Apply in the Mods screen, or quitting the game.

Save what you need with mod.save here; everything of the old run (timers, windows, listeners) is dropped right after.

# on("frame", function(e) ... end)

When: Every frame (about 60 times a second), in menus and hunts.

For continuous logic. Keep it fast; for anything slower use timer.every.

e.TypeAccess
dtnumberreadreal seconds since the last frame
local t = 0
on("frame", function(e)
  t = t + e.dt
end)

# on("stage", function(e) ... end)

When: The game screen changed (main menu, hunt menu, loading, hunt ...).

Stages: menu_main menu_hunt menu_options menu_more menu_help menu_about menu_shop loading hunt menu_privacy privacy_terms privacy_policy.

e.TypeAccess
fromstringreadthe previous stage (game.stage names)
tostringreadthe new stage

# on("new_hunt", function(e) ... end)

When: A hunt is being set up, before the game places its creatures.

Every creature handle of the previous hunt becomes invalid here. The game's own creatures then fire spawn before hunt_start.

# on("hunt_start", function(e) ... end)

When: The first frame of a hunt (the hunter is on the map).

World setup goes here: spawn creatures, change rules, open your HUD.

on("hunt_start", function()
  for i = 1, 3 do world.spawnNear("velociraptor", 2000 + i * 300) end
end)

# on("hunt_end", function(e) ... end)

When: The hunt is over (back to the menus).

# on("spawn", function(e) ... end)

When: A creature was added: by the game's hunt setup, a mod, or the world API.

Change its stats right away (scale, health, team) or give it data. During the game's hunt setup (before hunt_start) world.spawn returns nil; c:kill() / c:remove() inside this handler happen at the next frame.

e.TypeAccess
creatureCreaturereadthe new creature
on("spawn", function(e)
  if e.creature.species == "trex" then e.creature.scale = e.creature.scale * 1.5 end
end)

# on("shot", function(e) ... end)

When: The hunter fired.

e.TypeAccess
weaponintegerreadthe weapon slot (0 = the first weapon of the hunt)

# on("hit", function(e) ... end)

When: A shot of the hunter hit a creature.

Every creature has the game's own health (a T-Rex has 1024).

e.TypeAccess
creatureCreaturereadthe creature hit
weaponintegerreadthe weapon slot
damagenumberread / writethe damage the game will apply; change it

e.cancelled = true: no damage at all

on("hit", function(e)
  if e.creature.species == "trex" then e.damage = e.damage * 0.5 end  -- tougher T-Rex
end)

# on("creature_damage", function(e) ... end)

When: A creature hurt another one (creature fights: world.fights).

e.TypeAccess
creatureCreaturereadthe victim
attackerCreaturereadthe attacker
damagenumberread / writethe damage; change it

e.cancelled = true: no damage

# on("kill", function(e) ... end)

When: A creature died.

e.TypeAccess
creatureCreaturereadthe dead creature (still valid: its body)
killerCreature|nilreadthe creature that killed it, nil otherwise
byHunterbooleanreadtrue when the hunter's shot killed it
on("kill", function(e)
  if e.byHunter then kills = kills + 1 end
end)

# on("player_death", function(e) ... end)

When: The hunter is about to die.

e.TypeAccess
killerCreature|nilreadthe creature attacking him, nil for drowning / poisonous fog
causestringread"creature" or "environment"

e.cancelled = true: the hunter lives: the creature's attack misses; after drowning or fog his health is full again

-- three lives
local lives = 3
on("player_death", function(e)
  if lives > 0 then lives = lives - 1; e.cancelled = true; game.message(lives .. " lives left") end
end)

# on("creature_ai", function(e) ... end)

When: Every frame, for every creature, before its AI runs.

For per-frame control of particular creatures. This runs very often (hundreds of creatures x 60 frames), so return quickly for creatures you do not care about. To take a creature over for good, c.ai = false is cheaper.

e.TypeAccess
creatureCreaturereadthe creature

e.cancelled = true: its AI does not run this frame (you move and animate it: c:move, c:animate)

# on("creature_after_ai", function(e) ... end)

When: Every frame, for every creature, after its AI (and the world API's fights / possession) ran.

Correct or hold what its AI did (positions, animations).

e.TypeAccess
creatureCreaturereadthe creature

# on("key", function(e) ... end)

When: A key went down or up (menus and hunts; not while the mod menu or the console is open).

F1, F9 and ` are taken by the mod menu, reload and the console. keys.bind is the easy way to react to one key. Esc comes here first, then as the back event.

e.TypeAccess
keystringreadthe key name, e.g. "G", "Space", "F5" (see Keys)
scancodeintegerreadthe SDL scancode
downbooleanreadtrue when pressed, false when released

e.cancelled = true: (key down) the game does not see the key

# on("back", function(e) ... end)

When: Esc was pressed: the game's back key (menus: back; hunt: the menu opens, or closes when it is open).

Close your own window first, or give Esc a new job. Without a cancel the game does what it always does.

e.TypeAccess
menuOpenbooleanreadwhether the hunt's menu is open

e.cancelled = true: the game does not see it

-- Esc closes the shop window before it opens the game's menu
on("back", function(e)
  if shopOpen then
    shopOpen = false
    e.cancelled = true
  end
end)

# on("draw", function(e) ... end)

When: Every frame while some mod listens, inside the UI frame.

The only place (besides ui.panel functions) where draw.* and ui.window work.

e.TypeAccess
widthnumberreadthe window width in UI units
heightnumberreadthe window height
on("draw", function(e)
  draw.text(e.width / 2, 20, "Wave " .. wave, "yellow", 28, "center")
end)

# on("notice", function(e) ... end)

When: A creature notices the hunter: it sees him, smells him, or hears a noise he made.

Stealth mods start here. The strength of each sense per creature is c.sight, c.smell, c.hearing.

e.TypeAccess
creatureCreaturereadthe creature
sensestringread"sight", "smell" or "hearing"

e.cancelled = true: it does not notice him (this time)

-- raptors are blind: only smell and hearing give the hunter away
on("notice", function(e)
  if e.creature.species == "velociraptor" and e.sense == "sight" then e.cancelled = true end
end)

# on("noise", function(e) ... end)

When: The hunter made a noise creatures can hear (a shot).

e.TypeAccess
xnumberreadwhere
ynumberread
znumberread
radiusnumberread / writehow far it carries; change it

e.cancelled = true: nobody hears it

on("noise", function(e) e.radius = e.radius * 0.3 end)  -- silencers for everyone

# on("trophy", function(e) ... end)

When: The hunter took a trophy (the drop ship picks the creature up).

e.TypeAccess
creatureCreaturereadthe creature
pointsintegerread / writepoints (= credits) it gives; change them
on("trophy", function(e)
  if e.creature.scale > 1.2 then e.points = e.points * 2 end  -- big ones pay double
end)

# on("call", function(e) ... end)

When: The hunter used the call (decoy).

e.TypeAccess
speciesstringreadthe species the call imitates

e.cancelled = true: no call

# on("weapon_change", function(e) ... end)

When: The hunter changes weapons.

e.TypeAccess
fromintegerreadthe slot in hand (-1 = none)
tointegerreadthe slot he takes

e.cancelled = true: he keeps what he has

-- pistols only
on("weapon_change", function(e) if e.to ~= 0 then e.cancelled = true end end)

# on("player_damage", function(e) ... end)

When: The hunter loses health to poisonous fog or drowning (every frame it goes on).

Creatures' attacks do not hurt gradually: they kill (see player_death).

e.TypeAccess
amountnumberread / writehealth lost this frame; change it
causestringread"fog" or "drowning"

e.cancelled = true: no damage this frame

# on("mouse", function(e) ... end)

When: A mouse button went down or up, or the wheel turned (not while the mod menu or the console is open).

In a hunt the left button fires and the right one holds the binoculars.

e.TypeAccess
buttonstringread"left", "right", "middle", "x1", "x2" or "wheel"
downbooleanreadpressed (buttons)
xnumberread
ynumberread
wheelnumberreadwheel steps (+ away from you)

e.cancelled = true: (press / wheel) the game does not see it

# on("net_joined", function(e) ... end)

When: A player joined the session (see Multiplayer).

e.TypeAccess
peerintegerreadtheir id
playerNamestringread

# on("net_left", function(e) ... end)

When: A player left the session.

e.TypeAccess
peerintegerread
reasonstringread

# on("net_message", function(e) ... end)

When: A message from another game arrived (every topic; net.on(topic, fn) for one).

e.TypeAccess
fromintegerreadthe sender's id
topicstringread
dataanyreadstring, number, boolean or table
reliablebooleanread

# on("net_consent", function(e) ... end)

When: The player answered a mod's net.request, or changed a permission in the Network tab.

e.TypeAccess
modstringreadthe mod's id
allowedbooleanread

# on("setting", function(e) ... end)

When: A player changed a setting of some mod in the mod menu.

e.TypeAccess
modstringreadthe mod's id
keystringreadthe setting
valueanyreadits new value