DocsMegalithLanguage

The language

A script is built from alias, declare, enum, function, on EVENT: do … end, for each and if … then … elseif … then … else … end, with arithmetic, break, continue and return, functions that take values and local variables. Megalo sources, such as those ReachVariantTool writes, read as written, their altif and alt included. Comments start with --; Lua's block comments, --[[ … ]] and --[==[ … ]==], work anywhere. do … end makes a block of its own. Text goes in double or single quotes; \n, \t, \\, \" and \' are escapes.

Variables

Scripts keep whole numbers, references to objects, players and teams, and timers, in numbered slots that belong to the round (global), to a player, a team or an object, or to the current tick only (temporaries):

Belongs toNumbersObjectsPlayersTeamsTimers
global6432161616
object328848
player328888
team328888
temporaries25612864640
  • Slots count from 0 and must be plain numbers: global.number[63] is the last global number. A player's slots belong to that player: current_player.number[0]. A reference holds at most two indexed levels. Halo: Reach's own counts are smaller: a script written to be loaded by Reach itself keeps to those.
  • declare is only needed for a starting value: declare player.number[0] = 3. Numbers and timers otherwise start at 0, references at no_object, no_player or no_team. Only numbers and timers take a starting value, and temporaries none. with network priority low or high is accepted and ignored.
  • local number total, or local object nearest = current_object with a first value, makes a variable of the block's own, gone with the block. Without a value it starts empty (0, no_object, no_player or no_team) each time its block is entered: each pass of a loop, each run of an event. Locals are temporaries: a block's are free again after it.
  • alias points = player.number[0] gives a slot a name, used as current_player.points. alias scratch = allocate temporary number or allocate global.number takes a free one for you; don't also write to that slot by number. An alias can name a constant or a script_option[n] too. Aliases last to the end of their block.
  • References stay on what they point at: when a player leaves or an object is deleted, a saved reference becomes no_player or no_object, never someone or something else. A call on one does nothing and gives 0 or nothing back. A player's variables stay through respawns, and every round starts fresh.
  • Inside loops, current_player, current_object and current_team are the one being visited; team[0] to team[7] (to the last of the teams a script declares) and neutral_team are fixed teams.

Numbers, enums and functions

Numbers are whole, from about −2.1 billion to 2.1 billion, and wrap around past that. Write them as decimal, 0x hexadecimal or 0b binary. Arithmetic is written between values, a * 3 / 4 + 1: see arithmetic and mathematics. The assignments are =, +=, -=, *=, /= (rounds toward zero), %=, and the bit operations &=, |=, ^=, ~= (clear bits), <<= and >>=. References only take =. rand(4) is a whole number from 0 to 3, following the mode's seed; zero or less gives 0. Dividing by zero stops the script (in a Halo: Reach game variant it gives 0, as in Reach).

An enum
enum Points
   ordinary = 1
   bonus
end

Enum entries count up from the last value, starting at 0.

Functions

A function may take values, each written TYPE NAME (number, object, player or team), up to 16. Inside it they're its own names. It works on the caller's current player, team and object too, and returns nothing: return leaves it early.

Part of a script
function award(number amount, player who)
   if who == no_player then
      return
   end
   who.score += amount
end

on object death: do
   award(2, killer_player)
end

Declare a function before using it. A function without values may call itself, within the step limits; one that takes values doesn't, since its values live in its own variables. A function's values and locals are temporaries that stay its own for the whole source, so all functions' together count against the temporaries in the table above.

Values and conditions

  • A call that gives a value is a value: current_player.biped.get_z() + 1 goes in an assignment, a condition or another call's arguments. It can't stand alone as a statement. A call's result isn't a value to call on: current_player.get_weapon(primary).get_x() is two steps, through a variable.
  • Condition calls go in an if: is_zero, is_not_respawning, has_any_players, has_alliance_status, shape_contains, is_of_type, has_forge_label, is_button_held, is_driver, is_airborne, is_crouched, is_grounded, has_crosshair_point and the others marked (condition) in these docs.
  • An action, a call that gives nothing, can't be compared.
Part of a script
for each player do
   if current_player.get_death_count() > 3 and current_player.is_not_respawning() then
      current_player.score += 1
   end
end

if … then … end tests comparisons (== != < <= > >=, and only == and != on references) and condition calls, joined with not, and and or. or binds first, as in Megalo, so older sources keep their meaning: A or B and C means (A or B) and C; nest ifs for anything else.

Branches and leaving early

if … then … elseif … then … else … end
The first branch whose condition holds runs, and no condition is asked twice.
if … then … altif … then … alt … end
Megalo's form, read so its sources play as written: altif and alt check the earlier conditions again rather than remembering which branch ran, so a branch that changes the value being tested can let a later one run too. An if takes one kind or the other.
break, continue
In a for each: leave the loop, or go on with its next player, team or object.
return
Leaves the function or event being run; in the script's body, the rest of this tick's run.

Loops

for each player do
Every player in the game, dead ones too. Check current_player.is_not_respawning() for the living.
for each player randomly do
The same players in a shuffled order.
for each player with team[0] do
The players of that team.
for each player with vehicle do
The players in a seat.
for each team do
The playable teams (8, or as many as the mode declares) and the neutral team, empty ones too.
for each object do
Every object in the world, up to 8,192: players' bodies, the map's objects, the script's own and its zones.
for each object with label "gift" do
The objects with that label: the mode's own, the script's, and Forge's. A label is a text or a whole number.
for each object with type vehicle do
The objects of that type.

A loop gives its current player, team or object back when it ends.

Arithmetic and mathematics

Arithmetic is written between values: * and / before + and -, each from the left, parentheses, and - before any value. It's taken wherever a number is: an assignment, a condition, a call's arguments. A call that gives a number is a value too.

Part of a script
alias speed = player.number[0]
for each player do
    current_player.speed = current_player.biped.get_speed() * 3 / 4
    if current_player.is_button_held(button.ability_2) and current_player.biped.get_z() + 5 < 200 then
        current_player.biped.set_position(current_player.biped.get_x(), current_player.biped.get_y(), current_player.biped.get_z() + 1)
    end
end

Each operator is a function too, which a script may call by name beside the rest of the whole-number mathematics. Fractions are carried as thousandths: sin(30) is 500.

add(a, b), sub(a, b), mul(a, b), div(a, b), neg(a)
What +, -, *, / and a leading - stand for. They wrap at 32 bits as += does; a division by zero stops the script, as /= by zero does.
mod(a, b)
The remainder of a / b. A % between values would be read as a percent sign.
min(a, b), max(a, b), clamp(n, least, most), abs(n)
The smaller, the larger, n kept between two bounds, and a number without its sign.
muldiv(a, b, c)
a * b / c, worked in 64 bits: the way to multiply two numbers in thousandths without the product overflowing first, as in muldiv(speed, cos(angle), 1000).
sqrt(n)
The whole square root, rounded down; 0 for a negative.
pow(n, exponent)
Held to 32 bits; no negative exponent.
sin(angle), cos(angle), tan(angle)
In thousandths; the angle in degrees, or thousandths of a degree in fine units.
atan2(y, x)
The angle of the direction (x, y), from the x axis toward y: −180 to 180 degrees, in thousandths in fine units.
length(x, y, z)
The length of a vector, in its own units.
rand(n)
A whole number from 0 to n − 1.

A function of the script's own with one of these names is still called by its name as a statement.