1. Welcome to skUnity!

    Welcome to skUnity! This is a forum where members of the Skript community can communicate and interact. Skript Resource Creators can post their Resources for all to see and use.

    If you haven't done so already, feel free to join our official Discord server to expand your level of interaction with the comminuty!

    Now, what are you waiting for? Join the community now!

Dismiss Notice
This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Skript Functions

Discussion in 'Tutorials' started by Wrong, Jan 24, 2017.

  1. Wrong

    Admin

    Joined:
    Jan 22, 2017
    Messages:
    47
    Likes Received:
    6
    Functions

    Skript Functions

    Creating a function



    Constructing Your Function:

    There are two important things to consider when constructing your function, that you should know when you make the first line. What are going to be the inputs of the function (the data it receives), and what the return (if any) of the function will be (the outputs). So let's make our first function.

    Code (Skript):
    1. function myFunction(i: number) :: number:
    2.     set {_i} to {_i} + 1
    3.     return {_i}
    So let's break this down so you know what is happening. First, what does this function do? This function will take a number and add 1 to it. Yes, you might not need a function to do this, but let's make it as simple as we can.

    Code (Skript):
    1. # First you declare your function name, this can be anything. In this case 'myFunction'
    2. # You will then need to determine your inputs. I want this function to take in a single number
    3. # In the that, I also declare what will be the reference name of the variable, in this case 'i' which can be reference by {_i}
    4. # I then determine my return, in this case a single number. The format is as follows:
    5. #    function %function name%(%variable name%: %variable type%) :: %return type%:
    6. function myFunction(i: number) :: number:
    7.  
    8.     #you simply indent the appropriate amount, spaces or tabs whatever you prefer and put in your code.
    9.     set {_i} to {_i} + 1
    10.  
    11.      #if you have a return, you simply return the value, it must match the value type you have specified
    12.     return {_i}
    So, that is probably the most basic function you can do. However, a function is not required to make a return (have an output). In most programming languages this is called a 'void' which means the executing code will not be looking for a return, but instead calls on the function to DO something.

    Let's say we want a function that gives players a set of gear.

    Code (Skript):
    1. function leathSet(p: player):
    2.    set helmet of {_p} to leather helmet
    3.    set chestplate of {_p} to leather chestplate
    4.    set leggings of {_p} to leather leggings
    5.    set boots of {_p} to leather boots
    This would give the reference player a full set of leather armor. Modifying this makes it easier to give kits and such for instances like KitPVP. You could also make a neat little function that lets you dye a full set of armor with a simple function.

    Code (Skript):
    1. function leatherDye(c: color , p: player):
    2.    #note the comma, this allows us to input multiple values
    3.    dye {_p}'s helmet {_c}
    4.    dye {_p}'s leggings {_c}
    5.    dye {_p}'s chestplate {_c}
    6.    dye {_p}'s boots {_c}
    Calling Your Function:

    So now that we have covered a few simple ways to construct your function. Lets see how we can call them.

    Shown is a demonstration of using the functions we have created in the same order as before.

    Code (Skript):
    1. set {this.number} to 1
    2. #{this.number} will equal 1 here
    3. set {this.number} to myFunction({this.number})
    4. #{this.number} will equal 2 here
    The above will call our function that simply adds 1 to our inputted value.

    Code (Skript):
    1. command /leatherset:
    2.    trigger:
    3.        leatherSet(player)
    4.  
    The above will give the executing player a full set of leather armor.

    Code (Skript):
    1. command /leatherset <color>:
    2.    trigger:
    3.        leatherSet(player)
    4.        leatherDye(argument 1 , player)
    Using the above, and our two leather set functions, we give the player a full set of leather armor, and then let them dye it whatever color they inputted.

    Built in functions:


    Skript 2.2 has a few simple built in functions. View them here

    Extra Notes:

    That's really all there is to it. Word of advice though, if you can do what you need to do with a simple command like the examples, I would personally just use a command. Functions are more useful if you are going to be executing the same code over and over from different areas.

    Giving the ability for multiple scripts to start a game or use a math function that is unique to your server, these are things that functions are great for. I personally use them a lot for logging, as my server has many scripters who contribute. By using functions, I ensure that the all of the logs look the same with the same data and Formatting provided, without having the scripters waste their time writing different log formats over and over.

    Functions have the unique ability of being able to be called from any other script regardless of location. They do not have to be in the same file as what is calling them.

    When a function is changed and it's change is no longer valid for something that is calling it (like changing a return from a number to a text), then the caller will use the old version of the function until either it is reloaded, or the server is reset.

    Skript will not allow to input the wrong values in the wrong slots. This allows you to avoid having to check if the parameters are the correct type or not (something you had to do in the skQuery Counterparts).
     
    TonyMaster21, Ayham Alali and Krazy like this.
  2. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,183
    Likes Received:
    234
  3. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,183
    Likes Received:
    234
  4. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,183
    Likes Received:
    234
    Is there a possibility to create a function without any arguments?
    I meant I've already seen this.
     
  5. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,183
    Likes Received:
    234
    Yeah, you just need to put an useless parameter, for example:
    Code (Skript):
    1.  
    2. function test(test: int = 0):
    3.       broadcast "test"
    4.  
    then you can call it without any parameters.
     
  6. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,183
    Likes Received:
    234
    Might want to add a note that functions must be declared higher in the script than they are used. Many scripting languages do not have this limitation and no compiled language that I'm familiar with does. Some, like myself, will assume they can declare functions at the bottom to keep the most often edited code near the top. I spent several hours fighting a problem and even posted for help in the forums only for it to turn out to be this (moved the function declaration to the top under the options section and the problem went away).

    Also how does this work when functions are intended to be called across scripts? Does skript load scripts in alphabetical order? I saw where someone had received the same error message when I was trying to find a solution to my problem. He was calling a function across scripts though. Given that declaration order matters seems like script load order would have to matter too in such cases. I may take time to test this later if no one knows.
    --- Double Post Merged, Feb 10, 2017, Original Post Date: Feb 7, 2017 ---
    I have taken time to test load order of scripts. Skript scripts are indeed loaded in alphabetical order with bensku's 2.2-dev23. And It does affect function declaration order.
     
  7. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,183
    Likes Received:
    234
    A couple things to note:
    • You can't return something if your function has a wait
    • The code that called the function will wait for it to finish as long as it doesn't have a wait.
      If you don't want this, you can add
      Code (Skript):
      1. wait 0 tick
      to the beginning of your function and it will return to the code that called it immediately
     
  8. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,183
    Likes Received:
    234
    Im studing programing (java) By my selft and are also int, double , void can I also use this?
     
  9. PureNuggets

    PureNuggets Member

    Joined:
    Jun 25, 2021
    Messages:
    17
    Likes Received:
    0

Share This Page

Loading...