Functions are an useful way to make reusable sections of code. If you have a bunch of code that is often repeated, instead of copying and pasting the code in many places you can just put it in a function then call the function when you need to run the code.
In many cases this allows for cleaner code, reduces the complexity of the system, and improves performance.
When creating a function, the code is written like a normal event in Skript.
The following code pattern can be used to create a function. Bold parts can be changed.
function functionName(parameterName: type = defaultValue) :: returnType:
: type
specifies the required type of the parameter to be entered.
object
type can be used.= defaultValue
specifies the value will be used if this parameter is not entered, so it makes the parameter optional. If a required parameter is not entered when calling the function, the script will give an error while loading.
:: returnType
used if the function will return something. Make sure to replace returnType
with an actual return type (eg. number, text, object, item).
Input parameters are taken as local variables in the function code. For example a parameter named player
would be {_player}
.
To return a value return %objects%
effect is used. Also this effect would stop the code like using the stop
effect.
Calling a function is like using an effect. But if the function has ability to return something, can also be used like an expression.
functionName(parameters)
To enter a raw list for a parameter, it must be in parentheses. Otherwise it might conflict with other function parameters as both use commas and "and". This is not needed if the function has only one parameter.
Example:
functionName(parameter1, (value1, value2 and value3), parameter3)
Of course if the list is comes from a variable or an expression this is not needed. For example {list::*}
or all players
can be entered for the parameter directly.
function clearChat(p: player):
loop 100 times:
send "" to {_p}
This function assigns the player in 1st parameter to variable {_p}
.
The 2nd line creates a loop that will run the code inside for 100 times.
The latest line sends an empty message to the player in {_p}
variable.
Usage
command /clearMyChat:
trigger:
clearChat(player)
Runs the clearChat
function using the player for the first parameter.
function sendMessage(players: players, message: text, times: number = 1):
loop {_times} times:
send {_message} to {_players::*}
This function sends the message in 2nd parameter to the player in 1st parameter for the number in 3th parameter times.
If the 3th parameter is not entered the {_times}
variable will be 1 by automatically.
Usages
sendMessage(player, "message", 10) #10 Times
sendMessage(all players, "&cmessage") #1 Time
function total(numbers: numbers) :: number:
broadcast "total function called"
loop {_numbers::*}:
add loop-value to {_total}
return {_total}
Usages
set {_numbersList::*} to 6, 3 and 9
set {_result} to total({_numbersList::*})
if total(1, 6 and 9) is 16:
send "True"
send "%total({_numbers::*})%"
function setVariables():
set {variables::1} to "value"
set {variables::list::2} to true
set {variables::3} to 3
Creates a function without any parameter.
Usage
on load: # Of course this event is not required
setVariables()
function reversed(list: objects) :: objects:
loop size of {_list::*} times:
set {_index} to size of {_list::*} - loop-number - 1
add {_list::%{_index}%} to {_reversed::*}
return {_reversed::*}
This function reverses the list in 1st parameter and returns it back. For example 1, 2 and 3 --> 3, 2 and 1
Usage
set {_list::*} to reversed({_list::*})
function listContains(list: objects, search: object) :: boolean:
loop {_list::*}:
if loop-value is {_search}:
return true
return false # If return true is triggered it will stop the code so this line will not be triggered.
This function checks if the list in the first parameter contains the value in the second parameter or not. If contains, returns true
, if doesn't then returns false
.
Usages
if listContains(("a", "b" and "c"), "d") is true: # Checks if "a", "b" and "c" contains "d" (no)
send "True"
else:
send "False"
set {_list::*} to "a", "b" and "c"
send "Check: %listContains({_list::*}, ""c"")%"
Skript registers many functions by default. You can find list of them with their syntaxes, descriptions and examples here.
You must be logged in to comment