Skip to content

Syntax

Keywords

KeywordDescription
!Logical NOT
&&Logical AND
||Logical OR
< > <= >= == !=Comparison operators
* / + -Arithmetic operators
()Used to control the evaluation order of expressions. Example: v.a = (v.b + v.c) * v.d;
{}Braces for execution scope. It groups multiple statements into a single executable block, often within loops or conditions. Example: v.should_reset_a ? { v.a = 0; }
[]Square brackets for array access.
Note that custom variables cannot be arrays, this is for use within the render controller.
??Null coalescing operator. It returns the left-hand operand if the operand is not null; otherwise, it returns the right-hand operand. Example: v.a = v.a ?? 0;
?Binary conditional operator. It returns one of two values depending on the value of a Boolean expression. Example: v.a = (v.b > 5) ? 1 : 0;
? :Ternary conditional operator. It returns one of two values depending on the value of a Boolean expression. Example: v.a = (v.b > 5) ? 1 : 0;
thisThe current value that this expression will ultimately write to (context specific). Example: in "rotation" : [ 0.0, "-this", 0.0 ] this refers to the current rotation of the bone.
returnUsed to set the evaluation of complex expressions to the evaluation of the following statement. Example: v.a = q.life_time; return v.a > 5;
->Used to follow an actor reference to execute queries within the current scope while passing the referenced actor to the function. Example: c.owning_entity -> q.is_using_item. In this expression, c.owning_entity can be called from an item and refers to said entity who has equipped the item. By using the arrow operator, we can execute the is_using_item function to determine if the entity is using an item in their hand.
loopFor repeating an execution block n times.
Note that the max value n can be is 1024.
Example:
 // check inventory
"t.val = 0;
t.i = 0;
loop(27, {
t.val = q.is_item_name_any('slot.inventory', t.i, 'minecraft:splash_potion', 'minecraft:potion');
t.val ? {return t.val;}; t.i = t.i+1;
}
);
// check hotbar
t.val = 0;
t.i = 0;
loop(9, {
t.val = q.is_item_name_any('slot.hotbar', t.i, 'minecraft:splash_potion', 'minecraft:potion');
t.val ? {
return t.val;
};
t.i = t.i+1;
}
);"
for_eachUsed to iterate through an array of actors.
Note that there are no stable queries that can use this
This is an example from a previously experimental query that was removed:
"v.x = 0;
for_each(t.pig, query.get_nearby_entities(4, 'minecraft:pig'), {
v.x = v.x + 1;
});"
breakBreaks out of the inner-most active loop. So if you have nested loops and the inner-most one calls break, only that loop will break.
Example:
v.x = 1;
v.y = 1;
loop(10, {
t.x = v.x + v.y;
v.x = v.y;
v.y = t.x;
(v.y > 20) ? break;
}
);
continueFor skipping the rest of the set of statements of a loop/for_each iteration and moving to the next iteration.
Example:
v.x = 0;
loop(10, {
(v.x > 5) ? continue;
v.x = v.x + 1;
});
geometry.A reference to a geometry named in the client-side entity defintion.
material.A reference to a material named in the client-side entity defintion.
texture.A reference to a texture named in the client-side entity defintion.
math.Used to execute Math Functions.
query.Used to execute Query Functions.
variable.Used to reference Variables.
temp.Used to reference Temporary Varaibles.
context.Used to access Context.

Expressions

An expression is a variable assignment operation or a calcuation that returns a value. Expressions can be simple or complex.

Simple Expressions

A simple expression is a single statement, the return value of which is returned to the system. eg:

json
query.is_alive

Simple expressions must end with a ; when setting a variable. Omitting the ; or adding it to non-variable expressions will throw an error.

json
v.x = 1.0;

Complex Expressions

A complex statement is a series of statements, seperated using ;. Each statement is evaluated in order, the last statement must use the return keyword to define the resulting value of the expression. For example, this expression that checks if the entity has gained health without using multiple states:

c#
t.update_tick = math.mod(q.state_time, 0.05) < 0.0001; 
t.previous_health = t.update_tick ? q.health : (t.previous_health ?? q.health);
return t.update_tick ? 0 : (q.health > t.previous_health);

Order of Operations

Molang Operators follow this order to determine which thing is evaluated first when no parentheses are used. The table notates the precedence groups highest to lowest.

Precedence GroupDescription
Logical NotThe Logical Not ! operator
Multiplication and DivisionMultiplication * and Division /
Addition and SubtractionAddition + and Subtraction -
ComparisonsComparison operators < <= > >= (See 'Versioned Changes' below)
Equality checksEquality checking operators == != (See 'Versioned Changes' below)
Logical ANDThe Logical AND && operator (See 'Versioned Changes' below)
Logical ORThe Logical OR `
Ternary ConditionalTernary conditional operators using ? :. Evaluated right-to-left when there are multiple ternary operators. (See 'Versioned Changes' below)
Null CoalescingNull coalescing operator ??