MinitScript  0.9.31 PRE-BETA
README-Functions

LOGO

1. Functions/Stacklets/Callables

See this example that shows functions and recursion.

Argument variables, parsed in function declarations are populated in function context with corresponding values.

...
# function of recursive factorial computation
function: factorial($value)
console.printLine("factorial(): $value = " + $value)
if ($value == 0)
return(1)
end
return($value * factorial($value - 1))
end
...
console.printLine("factorial(5) = " + factorial(5))
...

If a argument(variable) is prefixed with a & operator in the function declaration, the variable will not be copied into the function arguments, but a reference will be created, means if this variable changes within the function it will also change in the parent variable scope. See &$b and &$c.

Be aware that value by copy variables usually require more instantiation time due to the copy that needs to be made of the variable from parent variable scope to function variable scope.

...
# function to test references in user functions
function: assignTest($a, &$b, &$c)
$a = "a"
$b = "b"
$c = "c"
end
...
$a = "0"
$b = "1"
$c = "2"
console.printLine("assignTest(): pre: $a = " + $a + ", $b = " + $b + ", $c = " + $c)
assignTest($a, $b, $c)
console.printLine("assignTest(): post: $a = " + $a + ", $b = " + $b + ", $c = " + $c)
...

By default, variables are read from current (variable) context. The default context is the global context. Functions and callables have their own contexts. In this case global variables must be accessed by using the "$$." or "$GLOBAL." accessor.

...
# function to test global variable access
function: globalVariableTest()
console.printLine("globalVariableTest(): $GLOBAL.globalTest = " + $GLOBAL.globalTest)
$GLOBAL.globalTest = "Been there, done that, got the t-shirt"
end
...
$globalTest = "Global Test Variable"
console.printLine("globalVariableTest(): pre: $globalTest = " + $globalTest)
globalVariableTest()
console.printLine("globalVariableTest(): post: $globalTest = " + $globalTest)
...

Stacklets are basically functions without a own stack(or scope), that e.g. holds variables. But stacklets are able to read/write variables of root scope or of a defined scope function scope. Stacklets take no argument if used in root scope. It can take a single argument that names the function the stacklet is bound too, to use its scope.

TODO: Example

A special type of functions are callables. Callables are functions that are used to interact between MinitScript scripts.

Despite the fact that a callable function of a script can be called from another script, they have the limitation that they must not contain MinitScript C++ method calls that require a context function.

Context functions are functions that require a special context. You can ignore this fow now. Default MinitScript does not provide script methods by C++ that require a context.

...
# call this callable to select this unit
callable: select()
$GLOBAL.selectMode = "select"
end
...
# call this callable to unselect this unit
callable: unselect()
$GLOBAL.selectMode = "unselect"
end
...
# update engine (context) function
function: updateEngine()
...
# selection
if ($GLOBAL.selectMode == "select")
engine.entity.setEnabled(logic.getId(), true, "selection")
$GLOBAL.selectMode = "none"
elseif ($GLOBAL.selectMode == "unselect")
engine.entity.setEnabled(logic.getId(), false, "selection")
$GLOBAL.selectMode = "none"
end
...
end
...

2. Links

2.1. Language Documentation

2.2. Other Links