Functions
Actionable and accessible members in a script.
Functions are a useful way to structure and re-use code, similar to Java methods. They are used to provide a lot of Skript's behaviour and functionality.
A simple introduction to their features is given below.

Function Structure

Functions can take a verifier and a trigger entry.
1
function example_function:
2
verify:
3
assert "hello" is a string
4
trigger:
5
print "hello"
Copied!
Functions may also provide a return value, which is given back to the code that executed the function.
1
function example_function:
2
trigger:
3
if "hello" is a string:
4
return 12
5
return 64
Copied!
An empty return effect will provide an empty null value to the executor.
1
function example_function:
2
trigger:
3
print "hello"
4
return
Copied!
Functions may accept named parameters, which are accessible inside that function as variables.
1
function example_function (number_1, number_2):
2
trigger:
3
print "The first number is: " + {number_1}
4
return {number_1} + {number_2}
Copied!

Function Use

Functions can be run directly in code, in the run effect.
1
run example_function
2
run example_function(1, 2)
Copied!
If the result of a function is needed, it can be used as an expression.
1
set {var} to example_function(1, 2)
Copied!
This means that functions can be used to run each other.
1
function first: // This function has no parameters, so we don't need the ( )
2
trigger:
3
set {var} to 1
4
set {result} to add_together({var}, 3)
5
print "The result is: " + {result}
6
โ€‹
7
function add_together (number_1, number_2):
8
trigger:
9
return {number_1} + {number_2}
Copied!
Functions can be used to do repetitive actions, to avoid writing the same code multiple times.
1
function write_file (name, value):
2
trigger:
3
set {file} to a new file at {name} + ".txt"
4
set the contents of {file} to {value}
5
โ€‹
6
function get_file (name):
7
trigger:
8
set {file} to the file at {name} + ".txt"
9
return the contents of {file}
10
โ€‹
11
function my_function:
12
trigger:
13
write_file("test", "hello there") // calls our write function
14
set {contents} to get_file("test") // uses our get function
15
assert {contents} is "hello there" // checks it matches
16
print {contents} // prints the contents
17
delete the file at "test.txt" // gets rid of our file
Copied!

Remote Function use

Functions from other scripts are also available, but need to be run using a special syntax.
The target script is specified according to its file path from the code folder (called skript by default.) A file called my_script would be found at skript/my_script.
1
run example_function(1, 2) from skript/my_other_skript
Copied!

Dynamic Function Use

Occasionally, functions may need to be run when you do not know their name. The dynamic function expression can be used for this.
1
function test_function:
2
trigger:
3
set {func} to function "my_target"
4
run {func} with "hello"
5
โ€‹
6
function my_target:
7
trigger:
8
print "hello"
Copied!
This expression provides a handle or function 'object', rather than the value of the function. It is slightly slower than using the function directly, but not by a significant amount.
The handle is provided by Mirror, an advanced dynamic call library for the JVM.
As the handle is found by string pattern, this could be assembled from variables or function parameters.
The parameter indicators in the function string are only placeholders for clarity.
1
function test_function:
2
trigger:
3
set {func} to function "my_target(a, b)"
4
set {func} to function "my_target(thing1, thing2)"
5
// the parameter count is important, not the value
6
run {func} with ("hello ", "there")
7
โ€‹
8
function my_target(a, b):
9
trigger:
10
print {a} + {b}
Copied!
Running a dynamic function with a single argument can be done by providing it.
1
set {func} to function "my_target(var)"
2
run {func} with "hello"
Copied!
Running a function with multiple arguments can be done using an implicit array.
1
set {func} to function "my_target(a, b, c)"
2
run {func} with (1, 2, {var})
Copied!
It may also be run using a list of the arguments.
1
set {list} to a new list
2
add 1 to {list}
3
add "hello" to {list}
4
set {func} to function "my_target(a, b)"
5
run {func} with {list} // this is smartly unpacked to (1, "hello")
Copied!
There is a rare occasion where your only argument for the function must be a list, and you do not want the list to be unpacked. In this case you must use the explicit array creator.
1
set {arg} to a new array ({list})
2
set {func} to function "my_target(thing)"
3
run {func} with {arg} // this is an array of one thing
Copied!
This is because the implicit array creator cannot take only one argument - that would be the bracket expression instead!
External functions can also be obtained dynamically.
1
set {func} to function "my_target(thing) from skript/my_other_script"
2
run {func} with "hello"
Copied!

Return Types

A function may specify an explicit return type of what it will give back to the trigger that ran it.
1
function test:
2
return: string
3
trigger:
4
return "hello"
Copied!
This is unnecessary for most programs, but can help to prevent unexpected errors. Specifying a return type will allow only that type to be returned from the function. Returning the wrong value will error.
This is designed for programs that wish to override Java methods.

Return None

Functions may specify none as a return type if they have no result value. If this is specified, the return %Object% effect may not be used.
If a trigger tries to get a return value from this function, it will get an empty null value.
The none return type counts as a Java void.

Parameter Types

Functions may specify one or more explicit types for their parameters.
1
function test (a, b):
2
parameters: string, integer
3
trigger:
4
assert {a} is a string
5
assert {b} is a number
6
loop {b} times:
7
print {a}
Copied!
If a function has explicit type parameters, the arguments are guaranteed to be of this type. The Skript runtime will attempt to convert incorrect values (e.g. "3" -> 3) but if there is no available converter an error will be given instead.
1
function test (name, age):
2
parameters: string, integer
3
trigger:
4
print {name} + " is " + {age} + " years old."
5
โ€‹
6
run test("Henry", 92)
7
run test("Paula", "31") // "31" -> 31
8
run test(3.5, 4) // 3.5 -> "3.5"
Copied!
Libraries may register additional type converters.