DocsMegalithLanguage

Events and timers

Everything outside an event runs on every tick of the game, after that tick's deaths and spawns. A paused game has no ticks. An event has one handler, at the top level of the file; on object damaged may have more:

Eventcurrent_… in itValues in it
The script's bodyNone until a loop sets oneRuns every tick.
on init: do … endNoneOnce, as each round starts.
on widget click: do … endThe player who clickedclicked_widget: the widget's number. See script menus.
on button press: do … endThe player who pressedpressed_button: button.melee and so on. See buttons.
on player join: do … endThe player joiningA player joins the game, before their first body. Those already there as the round's script begins get it too.
on player spawn: do … endThe player who spawnedTheir new body is there. selected_deploy_object: the object they chose to deploy at, or no_object.
on player quit: do … end (or player leave)The player leavingOnce as a player leaves the game, while their variables are still there to read and write; again if they come back and leave again.
on player team change: do … endThe player who movedprevious_team: the team they left. current_team: theirs now.
on vehicle enter: do … end, on vehicle exit: do … endThe player; current_object: the vehicleTheir body takes or leaves a seat. vehicle_seat: the seat's number in the vehicle. A seat left for another raises both.
on objective pickup: do … endThe carrier; current_object: the flag, bomb or ballA player takes one up.
on objective drop: do … endThe player who carried it; the itemThey let it go or died. killer_player: their killer, else no_player.
on objective reset: do … endNo player; the itemThe game type puts one back: Capture the Flag's flag, Assault's bomb, Oddball's ball. current_team: its team, no_team for the ball.
on objective scored: do … endThe player who scoredA capture in Capture the Flag, a detonation in Assault. current_team: theirs. Points a script gives don’t count as one.
on territory captured: do … endNone; current_object: the territory's markerA Territories game's territory changes hands: previous_team, current_team.
on vip change: do … endThe new VIPA team’s VIP changes in the VIP game type: current_team, previous_player (no_player for the first).
on juggernaut change: do … endThe new Juggernautprevious_player.
on bomb armed: do … endThe player who armed itAssault's bomb is armed: current_team, their team.
on deployment selected: do … endThe player who choseThey chose where to deploy on the deployment map: selected_deploy_object, no_object for nowhere in particular. current_player.get_selected_deploy_point() reads it later.
on object damaged: do … endcurrent_object: the object hit (no_object once gone). current_player: who's responsible (no_player for the world or the AI).damage_category: damage.bullet and so on. damage_source_object: the attacker's body or vehicle the damage came from, or no_object. damage_incoming and damage_inflicted: the damage as it came and as the mode's rules left it, in tenths of a point; a hit an object's rules refuse whole reads 0 for both. See damage.
on object death: do … endThe player whose body diedkilled_object: the body, while it lies there. killer_player: no_player after a suicide, a betrayal or a death to the world or the AI. killer_object: the killer's body.

Outside their events, clicked_widget, pressed_button and damage_category are −1. on object damaged runs once for every hit on every object while a round is played: each round of automatic fire, and each object a blast reaches. Keep its handler short.

Beside the plain handler, a source may have handlers for the hits on objects of one label, or of one damage category, alone: on object damaged with label "target": do … end and on object damaged with type damage.plasma: do … end. Each runs in the order written.

objective pickup and objective drop come from the items themselves, the map's flag, bomb and ball and any a script makes, so they play under every game type. objective reset, objective scored, vip change, juggernaut change and bomb armed are the game type's own, and come only in the game types that have them (mode in the header).

A death, as it happens
on object death: do
   if killer_player != no_player then
      killer_player.score += 1
      current_player.score -= 1
   end
end

Reach's pregame runs before init as a round starts, and local each tick once for each player: see Reach's calls.

Timers

  • Timers count seconds, fractions included. set_rate(-100%) counts down one second per second of play, 100% counts up, 0% stops. Other rates: 10, 25, 50, 75, 125, 150, 175, 200, 300, 400, 500 and 1000%, either sign, written as a plain value.
  • A timer counting down stops at 0. reset() puts back its declared value but keeps its rate, so call set_rate again for a timer that repeats. is_zero() is a test, not an event: check it every tick.
  • A timer counting up stops at its declared value if that value is above 0. Assignment and arithmetic can change a timer's value; it can't go below zero. Reading a timer into a number drops the fraction.
  • For something that repeats on a fixed beat, game.get_tick() gives the ticks since the round began, and game.get_ticks_per_second() the game's ticks a second, 30 or 60. if mod(game.get_tick(), game.get_ticks_per_second() * 5) == 0 then holds once every five seconds, with no timer to reset.