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.
Functions may also provide a return value, which is given back to the code that executed the function.
An empty return effect will provide an empty null
value to the executor.
Functions may accept named parameters, which are accessible inside that function as variables.
Function Use
Functions can be run directly in code, in the run
effect.
If the result of a function is needed, it can be used as an expression.
This means that functions can be used to run each other.
Functions can be used to do repetitive actions, to avoid writing the same code multiple times.
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
.
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.
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.
Running a dynamic function with a single argument can be done by providing it.
Running a function with multiple arguments can be done using an implicit array.
It may also be run using a list of the arguments.
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.
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.
Return Types
A function may specify an explicit return type of what it will give back to the trigger that ran it.
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.
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.
Libraries may register additional type converters.
Last updated