Language Reference
Functions — fn()
Section titled “Functions — fn()”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
returnto return a value f.is a shorthand prefix for calling functions, likeq.for queries andv.for variables
Conditionals — if / else / else if
Section titled “Conditionals — if / else / else if”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';};Switch — switch()
Section titled “Switch — switch()”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
Loops — while()
Section titled “Loops — while()”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, andreturninside the body - Hard-capped at 1024 iterations to prevent infinite loops
Structs — struct()
Section titled “Structs — struct()”Create empty data containers for grouping related values.
t.result = struct();t.result.winner = 'player';t.result.xp = 500;Imports — import()
Section titled “Imports — import()”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
Compound Assignment Operators
Section titled “Compound Assignment Operators”| Operator | Example | Equivalent |
|---|---|---|
+= | v.x += 5 | v.x = v.x + 5 |
-= | v.x -= 3 | v.x = v.x - 3 |
*= | v.x *= 2 | v.x = v.x * 2 |
/= | v.x /= 4 | v.x = v.x / 4 |
Member Access on Expressions
Section titled “Member Access on Expressions”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.nameq.player_winners[0].player.tell('Victory!')Comments
Section titled “Comments”// Single-line comment
/* Block comment */Comments are stripped during tokenization and have no effect on execution.