Jump to content

How to use Functions with Skript


UntitledGoose

Recommended Posts

 

M I N E H U T  C O M M U N I T Y  F A Q

HOW TO USE FUNCTIONS WITH SKRIPT

REQUIREMENTS: Skript
DIFFICULTY: MEDIUM

Introduction

Functions are little doodads in Skript that hold snippets of code, and that function can be executed to run that code. Functions also take parameters, and can return values. You’ll learn all about functions here.

 

What you’ll learn

  • General look on functions

  • Parameters

  • Returning

  • Examples

 

General look on functions

Functions have 4 components to them: name, parameters, return type, and code.

Name is the name of the function, you will see more about this later.

Parameters are inputs that the function can take in when executed, the functions can interact with these inputs to make diverse outcomes.

Return types are required if you want to return something. It basically says what something should expect a function to return. What the function returns must follow this type. A few examples of types are string, numbers, player, etc.

Code is the snippet that the function executes when it is called.

 

This is what a function looks like:

function name(parameter_name: parameter_type) :: return_type:

____# code___________________________________________________

 

Note that after putting the parameter’s name and type in the function’s declaration, you can also tack on a default value. The parameter’s content becomes the default value if that parameter was not filled in when the function was called. It looks like this:

 

_______________________________________________

parameter_name: parameter_type = default value_

_______________________________________________

 

To call a function in code, you put the name, and then round brackets, and put your parameters inside of the brackets, separated by commas. It looks like this:

  ___ _______________________

name({_par1}, {_par2}, ...) _

  ____ ______________________

 

Parameters

Parameters can be passed when calling a function. These parameters can be anything, as long as it follows the type specified in the function’s declaration.

 

A function can access the value of a parameter in a local variable, with the parameter’s name as the label. This is what it looks like:

________________

{_parametername}

________________

 

Returning

The return keyword can be used to return a value from a function, this can be thought of a conversation between the function and the code, with parameters and returns.

 

Code calls function -> Passes in parameters -> Function does magic with parameters and whatnot -> Returns value -> Code does stuff with new value

 

Note here that even when a return type is defined in the function, the function does not have to return something. It can choose to return nothing.

 

Here is the syntax for returning:

______________ _+

return %objects% _

_______________ +

 

Examples

______________________________________________________________________function hello(name: text) :: text:___________________________________

____return “Hello, %{_name}%!”________________________________________

______________________________________________________________________

hello(“Goose”) # Returned value: “Hello, Goose!”______________________

______________________________________________________________________

function set(name: text, value: object):______________________________

____set {vars::%{_name}%} to {_value}_________________________________

______________________________________________________________________

set(“foo”, “bar”)_____________________________________________________

{vars::foo} # Value: “bar”____________________________________________

 

Require further assistance?:

If you need more help on this topic, head over to this section of forums, or join our Discord!

Compiled by the Minehut Staff Team

 

Edited by TheGoose
  • Like 1

VIP since January 7th, 2020

Support since September 19th, 2020

Helper since November 3rd, 2020

Moderator since March 8th, 2021

unknown.png

5.png.e14b1bf83edf0ac2edc1bfb822177184.png

image.png.fe30b7b2c0be5a541ba4ed2187b23030.png

Link to comment
Share on other sites

Awesome tutorial, however it doesn't really work with dark theme.

Founder - Minehut.xyz

The leading Minehut support website. We aim to provide top quality tutorials, provide answers to top questions, and help in any way we can. Our site is community driven, and we will continue to make it better and better.

Support | 28/08/20 -13/09/20

Senior Support | 13/09/20 - 19/10/20

 

forums.gif.5d3db2db4a05c9465b84fa9bf0293df4.gif

Link to comment
Share on other sites

5 hours ago, TheGoose said:

I'm using Dark Theme and it looks fine for me? Can you clarify

Sorry, meant light theme. In light mode you can't see the code blocks.

Founder - Minehut.xyz

The leading Minehut support website. We aim to provide top quality tutorials, provide answers to top questions, and help in any way we can. Our site is community driven, and we will continue to make it better and better.

Support | 28/08/20 -13/09/20

Senior Support | 13/09/20 - 19/10/20

 

forums.gif.5d3db2db4a05c9465b84fa9bf0293df4.gif

Link to comment
Share on other sites

5 minutes ago, TheGoose said:

Looks fine for me?

image.thumb.png.a0baf15af32f032f43dfd3899eaac8d4.png

You would call that 'fine'? Half of the code blocks are just black lines, could be made to look nicer.

Founder - Minehut.xyz

The leading Minehut support website. We aim to provide top quality tutorials, provide answers to top questions, and help in any way we can. Our site is community driven, and we will continue to make it better and better.

Support | 28/08/20 -13/09/20

Senior Support | 13/09/20 - 19/10/20

 

forums.gif.5d3db2db4a05c9465b84fa9bf0293df4.gif

Link to comment
Share on other sites

1 minute ago, Minehut said:

You would call that 'fine'? Half of the code blocks are just black lines, could be made to look nicer.

that was on purpose lol. It looks a lot better on Google Docs but Forums messed it up. there isn't anything I can do about it

VIP since January 7th, 2020

Support since September 19th, 2020

Helper since November 3rd, 2020

Moderator since March 8th, 2021

unknown.png

5.png.e14b1bf83edf0ac2edc1bfb822177184.png

image.png.fe30b7b2c0be5a541ba4ed2187b23030.png

Link to comment
Share on other sites

Just now, TheGoose said:

that was on purpose lol. It looks a lot better on Google Docs but Forums messed it up. there isn't anything I can do about it

Yeah I know that the formatting is a pain, I was just saying.

Founder - Minehut.xyz

The leading Minehut support website. We aim to provide top quality tutorials, provide answers to top questions, and help in any way we can. Our site is community driven, and we will continue to make it better and better.

Support | 28/08/20 -13/09/20

Senior Support | 13/09/20 - 19/10/20

 

forums.gif.5d3db2db4a05c9465b84fa9bf0293df4.gif

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...