SkriptTips and Techniques#1c7
Skript - Introduction to Functions
__Melvin__
In this blog post, we'll cover the basics of functions within Skript.
First off, what are Functions?
Functions allow you to create reusable sections of code. Functions should be used for code that is repeated, rather than copying and pasting the code. Not only does it make the script smaller and improves performance, it makes it much easier for yourself and others to read.
Creating a Function
In order to create a function, simply use function myFunctionName():
. Then, after the colon, we go on to the next line, indent, and write the code that we want to run when the function is called. In this blog, we'll create a simple function that broadcasts a welcome message.
Firstly, we'll create a function called welcome: function welcome():
. On the next line, we'll indent and add the broadcast effect with the message "Welcome to the server". The code should look like this:
function welcome():
broadcast "Welcome to the server!"
Calling a Function
In order to call a function, we simply type it's name in the code, followed by its brackets. For example if your function was called 'welcome', you'd use welcome()
in your code.
Furthermore, if the function accepts parameters (discussed below), you can also pass the parameters in, just make sure they are of the same type as specified when the function was created. Such as: welcome(player)
or kill(arg-1)
.
If the function returns something (discussed below), you can use it as an expression, such as set {variable} to function()
, or broadcast function()
.
Parameters in Functions
As there are no parameters in the code we've just created, we can't actually specify a player to welcome. Within the brackets, we can add parameters into the function. The syntax is: function welcome(p: player):
. Here, we have created a local variable (the variable can only be used within the function), and set it to type player, meaning that only a player can be passed in.
Now when we add code to our function we can use the local variable we created to reference whatever we pass into the function when called. In order to use the local variable, we use {_p}
(local variables are prefixed automatically with an underscore). This means in our broadcast, we can specify the player we want to welcome: broadcast "Welcome %{_p}% to the server!"
.
Returning data from a Function
Functions are also able to return data. In order to do this, we must declare what the function will return as we create it, and add it to the code. For example if our function will return a string, we will code: function welcome() :: string:
, or if our function will return an integer, we would code: function welcome() :: integer:
. Note that you can return any valid type that Skript has, much more than just string and integer. Now in our code, we must use the return effect, which simply returns whatever you input. In our welcome example, it would be as follows:
function welcome() :: string:
return "Welcome to the server!"
on join:
send welcome()
You should note that when you use the return effect, the function will stop running and will exit.
Your Turn! Test Yourself.
Note: the answer is directly below the instructions. No cheating!
- Create a function named calculate that takes in two integers and returns the two numbers added.