Skip to content

Functions

function name(a, b) {
return a + b;
}

A function can be marked public or private, and async when it uses await. Function and event names are not case-sensitive. Two functions in one file whose names differ only by case are a compile error (E-DUPLICATE-FUNCTION).

Parameters are temp. variables of the call. You may also write temp.a in the parameter list.

Two forms that the legacy server compiles are accepted so old scripts load unchanged. A return type after the parameters, function f() : void, is ignored. A parameter written with a dot that is not temp., such as function f(player.account), binds nothing: the argument is dropped and the name reads empty. Two functions with the same handler object, function Obj.onEvent() twice, are allowed and the later one replaces the earlier one; a repeated plain function is still an error.

A parameter may carry a type and a function a return type, as in function easeOut(t:Number, b:Number):Number. The types are accepted and ignored, so scripts written for the legacy server compile as they are. (For types that the editor does check, see type annotations.)

  • A missing argument is 0. An empty value passed as an argument also arrives as 0.
  • return;, or reaching the end of the function, gives 0 to a script caller.
  • An extra argument is ignored by the parameter list.
  • params is an array of all arguments, but only when the function runs as an event (started by the engine, by trigger, by scheduleevent, or by a trigger from the other side). In an ordinary call, params is empty.
function add(a, b) {
return a + b;
}
function onCreated() {
echo(add(2, 3));
// expect: 5
echo(add(2));
// expect: 2
echo(add(2, 3, 4));
// expect: 5
}

Run this example in the playground

return value; gives a value. return; and reaching the end of the function give empty.

A call name(args) with no receiver looks for:

  1. A function on the current object: its own code first, then the classes it joined.
  2. A function value stored in the script-global variable name (OQ-LANG-23).
  3. A built-in function.
  4. Nothing: the engine reports R-UNKNOWN-FUNCTION and the call gives empty. The compiler warns before that (W-UNKNOWN-FUNCTION) when the name is neither declared in the file, nor built in, nor provided by a class that is joined in the file.

Your own function hides a built-in with the same name. The compiler warns (W-SHADOWS-BUILTIN).

Functions that a class adds with join(...) at run time are not visible to the compiler. Call them through this, as in this.rankFor(points), to avoid the warning, and see reuse.

  • Inside one object (its own code and its joined classes), every function can be called.
  • From another object (obj.fn(), findnpc("X").fn(), (@"X").fn()), only public functions run. Calling a non-public function from outside gives 0 and reports nothing.
  • private behaves like a plain function. Use it for readability.
  • obj.trigger("Name", ...) reaches onName whatever its visibility.

obj.fn(args) runs fn with this set to obj and thiso set to the caller. When obj is empty or destroyed, the call gives empty and the engine reports R-CALL-ON-NULL.

function(params) { ... } is an expression that makes a function value.

  • There are no closures. The body cannot read the temp. variables of the function that created it.
  • this inside the body is the object that runs it.
  • A named function is not a value: temp.k = myFunc; stores 0. Pass names as strings.
function onCreated() {
temp.outer = 5;
temp.f = function(x) {
return x + temp.outer;
};
echo(temp.f(1));
// expect: 1
temp.g = function(x) {
return x * 2;
};
echo(temp.g(21));
// expect: 42
}

Run this example in the playground

Where a built-in takes a callback, it accepts either a function value or the name of a function as a string.

function Name.onEvent(...) declares the handler for the event onEvent of the named object Name, usually a window or a control. Both parts ignore case.

Statements outside any function run once each time the file is attached to an object: when an NPC is created or reloaded, or each time a class is joined, before onCreated (OQ-LANG-9). A class file often starts with join("other_class");.

Engine version 0.1.0. Built 2026-11-18.