Skip to content

Commit 5e01a10

Browse files
committed
add functions tutorial
1 parent 50ff98d commit 5e01a10

File tree

1 file changed

+160
-0
lines changed

1 file changed

+160
-0
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
---
2+
title: Functions
3+
author: Efnilite
4+
date: 9/7/2024
5+
url: https://github.com/SkriptLang/docs/blob/master/src/assets/tutorials/functions.md
6+
section: API
7+
index: 1
8+
---
9+
10+
# Functions
11+
12+
Skript's functions allow users to perform specific actions at multiple points in a code base.
13+
To allow addons to easily add their own functions, Skript 2.13 introduced a new API for registering functions,
14+
which uses the DefaultFunction class. Compared to the previous way of dealing with functions,
15+
this new API is a drastic improvement in several key ways.
16+
17+
As an example, let's register a function that removes a specific character from some strings,
18+
which defaults to removing all spaces.
19+
20+
```applescript
21+
# Intended result
22+
set {_x::*} to remove(" hello ") # "hello"
23+
set {_y::*} to remove(" hello ", " hey ") # "hello", "hey"
24+
set {_z::*} to remove((" hello ", " hey "), "h") # " ello ", " ey "
25+
```
26+
27+
## Creating a new builder
28+
29+
First, create a new builder to start building our function. This builder takes the `SkriptAddon`
30+
instance of your addon, the name of the function, and the return type of the function. Since our
31+
function always returns an array of strings, we can specify the return type as `String[].class`.
32+
If our function only returned a single string value, the return type would be `String.class`.
33+
34+
```java
35+
SkriptAddon addon = SkriptAddon.of(YourMainClass.class);
36+
37+
DefaultFunction.builder(addon, "remove", String[].class)
38+
```
39+
40+
## Documentation
41+
42+
After creating the builder, the documentation should be specified.
43+
This allows documentation sites to view more information about the function.
44+
45+
- The description features a brief description of the function and information about how it works.
46+
- The examples contain example use cases or examples of what the function returns.
47+
- The since method specifies the version history of the function. Any changes or additions to the function's workings should be added here.
48+
- The keywords contain words that may be associated with this function.
49+
50+
```java
51+
DefaultFunction.builder(addon, "remove", String[].class)
52+
.description("Removes a specific character from the strings.",
53+
"Removes spaces if no character is specified.")
54+
.examples("""
55+
set {_x::*} to remove(" hello ") # "hello"
56+
set {_y::*} to remove(" hello ", " hey ") # "hello", "hey"
57+
set {_z::*} to remove((" hello ", " hey "), "h") # " ello ", " ey "
58+
""")
59+
.since("3.0")
60+
.keywords("format", "strings")
61+
```
62+
63+
## Parameters
64+
65+
Next, we should specify what arguments our function can take. To do this, we use the `parameter` function.
66+
This starts by specifying the parameter name, and then the type it accepts. Like the return type,
67+
using an array indicates that multiple values can be passed to the argument.
68+
69+
Additionally, the `char` parameter contains a modifier, which changes the behaviour of a parameter.
70+
The optional modifier indicates that no value has to be passed to the argument.
71+
72+
```java
73+
DefaultFunction.builder(addon, "remove", String[].class)
74+
.description("Removes a specific character from the strings.",
75+
"Removes spaces if no character is specified.")
76+
.examples("""
77+
set {_x::*} to remove(" hello ") # "hello"
78+
set {_y::*} to remove(" hello ", " hey ") # "hello", "hey"
79+
set {_z::*} to remove((" hello ", " hey "), "h") # " ello ", " ey "
80+
""")
81+
.since("3.0")
82+
.keywords("format", "strings")
83+
.parameter("strings", String[].class)
84+
.parameter("char", String.class, Modifier.OPTIONAL)
85+
```
86+
87+
## Implementation
88+
89+
To complete our function, we add the implementation. This is the part that will handle the actual function logic.
90+
The `build` method provides you with a `FunctionArguments` instance, called `args` in our example. This contains
91+
all the values for the parameters that are passed when a function is called. These values are associated by the name
92+
of the parameter.
93+
94+
```java
95+
DefaultFunction.builder(addon, "remove", String[].class)
96+
.description("Removes a specific character from the strings.",
97+
"Removes spaces if no character is specified.")
98+
.examples("""
99+
set {_x::*} to remove(" hello ") # "hello"
100+
set {_y::*} to remove(" hello ", " hey ") # "hello", "hey"
101+
set {_z::*} to remove((" hello ", " hey "), "h") # " ello ", " ey "
102+
""")
103+
.since("3.0")
104+
.keywords("format", "strings")
105+
.parameter("strings", String[].class)
106+
.parameter("char", String.class, Modifier.OPTIONAL)
107+
.build(args -> {
108+
// what the user entered for strings
109+
String[] strings = args.get("strings");
110+
111+
// what the user entered, or, if the user passed nothing, returns " "
112+
// this is only necessary because char has Modifier.OPTIONAL
113+
String chr = args.getOrDefault("char", " ");
114+
115+
// actually replace all the characters
116+
for (int i = 0; i < strings.length; i++) {
117+
strings[i] = strings[i].replaceAll(chr, "");
118+
}
119+
120+
// return the value
121+
return strings;
122+
});
123+
```
124+
125+
## Final step
126+
127+
Now that we have our function, we should register it. To do this, use `Functions#register`.
128+
129+
```java
130+
DefaultFunction<String[]> remove = DefaultFunction.builder(addon, "remove", String[].class)
131+
.description("Removes a specific character from the strings.",
132+
"Removes spaces if no character is specified.")
133+
.examples("""
134+
set {_x::*} to remove(" hello ") # "hello"
135+
set {_y::*} to remove(" hello ", " hey ") # "hello", "hey"
136+
set {_z::*} to remove((" hello ", " hey "), "h") # " ello ", " ey "
137+
""")
138+
.since("3.0")
139+
.keywords("format", "strings")
140+
.parameter("strings", String[].class)
141+
.parameter("char", String.class, Modifier.OPTIONAL)
142+
.build(args -> {
143+
// what the user entered for strings
144+
String[] strings = args.get("strings");
145+
146+
// what the user entered, or, if the user passed nothing, returns " "
147+
// this is only necessary because char has Modifier.OPTIONAL
148+
String chr = args.getOrDefault("char", " ");
149+
150+
// actually replace all the characters
151+
for (int i = 0; i < strings.length; i++) {
152+
strings[i] = strings[i].replaceAll(chr, "");
153+
}
154+
155+
// return the value
156+
return strings;
157+
});
158+
159+
Functions.register(remove);
160+
```

0 commit comments

Comments
 (0)