Skip to content

Language Reference

Define reusable functions with named parameters and return values. Call them via f.name().

fn('heal', v.target, v.amount, {
v.target.health = math.min(v.target.health + v.amount, v.target.max_health);
return v.target.health;
});
v.result = f.heal(v.pikachu, 20);
  • The first argument is the function name (a string)
  • Middle arguments are the parameter names
  • The last argument (in {}) is the function body
  • Use return to return a value
  • f. is a shorthand prefix for calling functions, like q. for queries and v. for variables

Block conditionals as an alternative to nested ternary chains.

if (v.health > 50) {
v.status = 'healthy';
} else if (v.health > 0) {
v.status = 'wounded';
} else {
v.status = 'dead';
};

Multi-way branching that produces a value. The first matching case wins.

t.bonus = switch(t.biome,
'minecraft:plains', 1.0,
'minecraft:jungle', 1.5,
'minecraft:desert', 0.7,
1.0
);
  • First argument is the value to match against
  • Subsequent pairs are case, result
  • A final unpaired argument is the default value
  • Supports nesting switches inside switches

Conditional loops with two arguments: a condition and a body.

t.i = 0;
while(t.i < t.party_size, {
t.mon = q.player.party.get_pokemon(t.i);
t.mon.add_exp(100);
t.i += 1;
});
  • Supports break, continue, and return inside the body
  • Hard-capped at 1024 iterations to prevent infinite loops

Create empty data containers for grouping related values.

t.result = struct();
t.result.winner = 'player';
t.result.xp = 500;

Load .molang files from data packs and execute them. Any fn() definitions in the imported file become available to the current script.

import('cobblemon:util/math_helpers');
t.clamped = f.clamp(v.value, 0, 100);
  • Files are resolved from the data pack path data/<namespace>/molang/<path>.molang
  • Circular imports are prevented via an internal cache
  • The cache clears on /reload
OperatorExampleEquivalent
+=v.x += 5v.x = v.x + 5
-=v.x -= 3v.x = v.x - 3
*=v.x *= 2v.x = v.x * 2
/=v.x /= 4v.x = v.x / 4

Standard MoLang only allows . inside name chains like q.player.party. This mod adds dot access after ) and ], so you can chain off the results of function calls and array lookups:

q.player.party.get_pokemon(0).species.name
q.player_winners[0].player.tell('Victory!')
// Single-line comment
/* Block
comment */

Comments are stripped during tokenization and have no effect on execution.