Skip to content

Commit d6afce1

Browse files
sovdeethgitbook-bot
authored andcommitted
GITBOOK-80: weird broken link
1 parent b32f37f commit d6afce1

File tree

5 files changed

+246
-20
lines changed

5 files changed

+246
-20
lines changed

.gitbook/assets/image (2).png

214 KB
Loading

README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
---
2-
description: An entry point to Skript.
3-
---
4-
51
# Introduction
62

73
### Welcome!
@@ -13,7 +9,7 @@ We'll walk you through writing your first script, and hopefully get you more con
139
For those of you who are already familiar with Skript, go ahead and use the navigation table on the left to jump to what you're interested in. If you're interested in helping out, the github repo is linked on the right. Pull requests are welcome!
1410

1511
{% hint style="warning" %}
16-
**Note:** These tutorials are for the current version of the SkriptLang Skript fork (2.8.X) and are not guaranteed to be correct for other versions or forks. This site does not include any tutorials for addons, either.
12+
**Note:** These tutorials are for the current version of the SkriptLang Skript fork (2.9.X) and are not guaranteed to be correct for other versions or forks. This site does not include any tutorials for addons, either.
1713
{% endhint %}
1814

1915
## Installation
@@ -24,7 +20,8 @@ Installing Skript is no different from any other plugin. You download the latest
2420
You should ensure your server meets the requirements for running Skript:
2521

2622
* A Spigot, Paper, or Paper fork server jar. Spigot is the absolute minimum and Skript includes some features that are only accessible when using Paper or a fork of Paper.
27-
* Minecraft Version. Skript 2.8 works for versions 1.13+. 
23+
* Java 11 or higher
24+
* Minecraft Version. Skript 2.9 works for versions 1.13+. 
2825
* 1.8 support can be found [here](https://github.com/Matocolotoe/Skript-1.8/releases), but is not official, nor recommended, nor guaranteed to work.
2926
* 1.9-1.12 support is limited to old versions of Skript that do not receive updates (2.6.4) 
3027
{% endhint %}

core-concepts/indentation/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
---
2+
description: In Progress
3+
---
4+
15
# Indentation and Program Flow
26

37
Like any programming language, Skript needs some way of recognizing when code is meant to be 'within' other code, like what code a command should run, or what code should be run if an if statement succeeds. Many languages solve this with brackets, like `{}` or `()`. Skript, like Python, decides to solve this with indentation to keep the code you write cleaner and less cluttered.

core-concepts/indentation/functions.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ We've looked at functions as ways to simply run code, but one of the most powerf
118118
on join:
119119
give player customItem((name of player), golden apple)
120120
121-
function customItem(name: string, base-item: item) :: item:
121+
function customItem(name: string, base-item: item) returns item:
122122
set {_item} to {_base-item} named {_name}
123123
set lore of {_item} to "&fWelcome back!"
124124
return {_item}
@@ -128,7 +128,7 @@ This would give a golden apple with the player's name and some custom lore to an
128128

129129
There's a few things to pay attention too. First, notice that now we're putting the function call in the `give` effect. It's no longer on its own line. This means we're using it as an `expression`, not an `effect`. Functions are one of, if not the only, syntax in Skript with this property. 
130130

131-
Secondly, notice how the function definition has changed. We now have this `:: type:` bit on the end, which tells Skript what type the function is going to return. Again, if you make this plural you can return a list.
131+
Secondly, notice how the function definition has changed. We now have this `returns type` bit on the end, which tells Skript what type the function is going to return. Again, if you make this plural you can return a list.
132132

133133
Finally, notice the new syntax at the end of the function: `return {_item}`. This is how we tell the function what value it should return. In this case, it's `{_item}`. Return will also stop execution of the function there, like `stop` does. 
134134

@@ -179,15 +179,15 @@ When a player quits, the `quit` event in `script-2.sk` runs. This can't see the
179179
Now that we've explained all the parts, let's show the whole function at once:
180180

181181
```applescript
182-
[local] function functionName(parameterName: parameterType = defaultValue) :: returnType:
182+
[local] function functionName(paramName: paramType = defaultValue) returns returnType:
183183
```
184184

185185
Hopefully by now you know all these parts, but let's recap:
186186

187187
* **\[local]**: Whether this function is local or global. (global is default)
188188
* **functionName:** the name of the function, starts with a letter and can use letters, numbers, and underscores
189-
* **parameterName:** Optional. The name of the parameter, follows the same rules as variable names. Will be accessible as a local variable of the same name in the function.
190-
* **parameterType:** Required if you have a parameter. Tells Skript what type the parameter will be. Use `object` if you're unsure.
189+
* **paramName:** Optional. The name of the parameter, follows the same rules as variable names. Will be accessible as a local variable of the same name in the function.
190+
* **paramType:** Required if you have a parameter. Tells Skript what type the parameter will be. Use `object` if you're unsure.
191191
* **defaultValue:** Optional. Allows the function calls to omit this parameter, and Skript will automatically use this value instead.
192192
* **returnType:** Optional, only use if you intend to return something. Again, use `object` if you're unsure. Remember, functions that return values can't use `wait`.
193193

core-concepts/indentation/loops.md

Lines changed: 234 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,248 @@
1-
---
2-
description: In Progress
3-
---
4-
51
# Loops
62

3+
Loops are fantastic tools for anyone to have in their skill set. Loops allow you to repeat code, create countdowns, draw things, look for certain entities or items, and much more. They, along with conditionals, are your bread and butter for controlling how your program flows.
4+
5+
In essence, loops repeat code. They have a block of code inside them and they run it, and then run it again, and again, until they're done with their job. The details about how many times they run it (and what changes between each iteration) depend on the type of the loop.
6+
77
## Types of Loops
88

9-
Loop x times, loop lists, while loops
9+
Skript, like most languages, has two main types of loops: _for_ loops and _while_ loops. Each has their own use case.
10+
11+
### For Loops
12+
13+
For loops are often just called "loops", but I'll be calling them for loops to ensure no one gets them confused with while loops. They look like this:
14+
15+
```
16+
loop something:
17+
# do something
18+
```
19+
20+
The _something_ will be some sort of list. It could be `integers from 0 to 4`, or `all players`, or even `blocks in radius 10 of player`. There's even a special expression intended to be used in loops: `x times`. This will loop the numbers from 1 to x, or in the following example, 1 to 3.
21+
22+
```
23+
loop 3 times:
24+
broadcast "loop!"
25+
```
26+
27+
Loops will run the code inside them once per item in their list. So for `3 times`, which has `1, 2, 3`, it'll run the code 3 times. 
28+
29+
However, it'd be really nice if we could, say, access what number it's currently on. This is where `loop-value` comes in. When you're looping something, you can get the current thing (number, player, whatever) by using `loop-value`. 
30+
31+
```
32+
loop all players:
33+
send "hey!" to loop-value
34+
```
35+
36+
{% hint style="info" %}
37+
If you're looping a specific expression, like `all players`, you can use `loop-type` as another version:
38+
39+
```
40+
loop all players:
41+
send "hey!" to loop-player
42+
```
43+
{% endhint %}
44+
45+
#### Looping Over Lists
46+
47+
Loops go hand in hand with lists, as every loop needs a list of something to loop over. The `%number% times` expression actually creates a list of numbers, so `set {_x::*} to 3 times` is equivalent to `set {_x::*} to 1, 2, and 3`. Use this knowledge wisely. 
48+
49+
This means that variable lists can also be looped, and they come with some special features.
50+
51+
```applescript
52+
set {_my-list::1} to "hey"
53+
set {_my-list::2} to "how"
54+
set {_my-list::are} to "you"
1055
11-
### X Times
56+
loop {_my-list::*}:
57+
broadcast "%loop-index%: %loop-value%"
58+
# this prints the following:
59+
# 1: hey
60+
# 2: how
61+
# are: you
62+
```
1263

13-
### Looping Lists
64+
Really, just one special feature, which is `loop-index`. This lets you access the index of the current element of the list, just like how `loop-value` gives you the value.
1465

1566
### While Loops
1667

68+
While loops are the for loop's more temperamental cousin. They keep looping as long as a condition is true, which makes them great for repeating things over time, doing a specific action a dynamic number of times, or crashing your server.
69+
70+
{% hint style="danger" %}
71+
Since while loops will not stop until the condition fails, they can run infinitely, potentially crashing your server. Make sure your while loop **will always exit** **or add a wait to it.** Adding a wait stops the while loop until the delay is done, allowing your server to actually run.
72+
73+
```applescript
74+
# crashes your server
75+
while true is true:
76+
send "hi"
77+
78+
# doesn't crash
79+
while {_i} < 100:
80+
add 1 to {_i}
81+
82+
# doesn't crash
83+
while true is true:
84+
send "hi"
85+
wait 1 tick
86+
```
87+
{% endhint %}
88+
89+
While loops are most often used to do periodic work while a condition is true, say, to do something every 5 ticks while a player is online. However, you do need to be careful that you can stop a while loop at will, since **reloading a script will not stop a running while loop**.
90+
91+
{% hint style="warning" %}
92+
While loops will not stop when you reload the script that they are in. They will doggedly run until their condition is no longer true. This is a good reason to either use a periodical event (when appropriate) or to add a special case to abort a loop.
93+
94+
```applescript
95+
while player is connected:
96+
send "hi"
97+
if {abort-while-loop} is true:
98+
set {abort-while-loop} to false
99+
exit loop
100+
wait 10 seconds
101+
102+
...
103+
104+
command abort-loop:
105+
trigger:
106+
set {abort-while-loop} to true
107+
```
108+
{% endhint %}
109+
110+
### Do While
111+
112+
Like while loops, but they check the condition at the end of the loop, rather than the start:
113+
114+
```applescript
115+
# bad, normal while loop
116+
add 1 to {_test}
117+
while {check::%{_test}%} is set:
118+
add 1 to {_test}
119+
120+
# cool, epic do while loop
121+
do while {check::%{_test}%} is set:
122+
add 1 to {_test}
123+
```
124+
17125
## Loop-specific Syntax
18126

19-
continue, exit loop
127+
As briefly referenced in the while loop section, loops have special syntaxes that can be used to control them. These syntaxes are critical to making efficient and readable code with loops. These come in two main categories, the information syntaxes and the control syntaxes.
128+
129+
### Informational
130+
131+
`loop-value` and `loop-index` give you information on what's currently being looped. `loop-value` is the value, and will always be present. `loop-index` is only available when looping variable lists, and gives the index of the element in the list. As previously mentioned, you can also use `loop-%type%` when looping specific types, like `loop-number` when looping `integers between 0 and 10`.
132+
133+
```applescript
134+
set {_my-list::1} to "hey"
135+
set {_my-list::2} to "how"
136+
set {_my-list::are} to "you"
137+
138+
loop {_my-list::*}:
139+
broadcast "%loop-index%: %loop-value%"
140+
# this prints the following:
141+
# 1: hey
142+
# 2: how
143+
# are: you
144+
```
145+
146+
`loop-iteration` is useful for keeping track of what loop you're on. This can also be accessed with `loop-counter`.
147+
148+
```applescript
149+
# loop the first 25 elements of {_list::*}
150+
loop {_list::*}:
151+
if loop-iteration > 25:
152+
stop
153+
```
154+
155+
### Control
156+
157+
`continue` is for when you want to skip the rest of the loop and _continue_ to the next element.
158+
159+
```applescript
160+
# broadcast the square of all even numbers between 1 and arg 1:
161+
loop integers between 1 and arg 1:
162+
# skip odd numbers
163+
# (mod() gives the remainder, so dividing odd numbers by 2 gives a remainder of 1)
164+
if mod(loop-number, 2) == 1:
165+
continue
166+
167+
broadcast loop-number * loop-number
168+
```
169+
170+
`exit loop` is for, well, exiting loops. This is useful for preventing runaway while loops, as we've seen earlier, or just exiting once you find what you need.
171+
172+
```applescript
173+
# searching for {_needle} in {_haystack::*}:
174+
loop {_haystack::*}:
175+
if loop-value is {_needle}:
176+
set {_index} to loop-index
177+
exit loop # exit early, since the rest of the list doesn't matter.
178+
broadcast "Found needle at %{_index}%!"
179+
```
20180

21181
## When to Use Loops
22182

23-
while loops compared to periodicals
183+
{% hint style="info" %}
184+
This is a more of the in-the-weeds performance comparison, so if you're just here to learn about loops, you can skip this.
185+
{% endhint %}
186+
187+
Often, people will compare the performance of these two patterns:
188+
189+
```applescript
190+
every 10 ticks:
191+
loop all players:
192+
# do something
193+
194+
on join:
195+
while player is connected:
196+
# do something
197+
wait 10 ticks
198+
```
199+
200+
Before you keep reading, try to think about what the actual difference here is!
201+
202+
I'll wait.
203+
204+
Don't worry, I'll still be here.
205+
206+
Alright.
207+
208+
So, behind the scenes, there's a thing called a Scheduler. This is in charge of scheduling when things run on your server. When we make an `every x` periodical event, Skript tells the scheduler to run this thing every x time. This is pretty simple and straightforward, which means it's very easy for Skript to come back later and tell the scheduler to stop running it. The scheduler just gives Skript a number to call if it wants the task ended.
209+
210+
The downside here is that it runs the `do something` for every player, all at once. If you have a lot of players, or a lot of work to do per player, this can sometimes result in small lag spikes every time the code runs.
211+
212+
While loops, meanwhile, are a bit more complicated. Skript has to run the code within the loop to determine what the next behavior is, so when it hits the `wait 10 ticks`, it tells the scheduler "hey, can you restart this code in 10 ticks?" and the scheduler does just that. The downside, though, is that Skript doesn't really have control over the code anymore. Since every single iteration of the loop creates a new scheduled task, Skript doesn't know what number to call to stop the loop. This means that the while loop itself is the only thing that can stop it, which it does by failing the condition and not running the code inside itself, therefore not running the `wait`. **All this to say, `/sk reload` will not stop while loops.**
213+
214+
However, it also comes with some benefits. Since players don't usually all join at the exact same time, the while loops running for each individual player are going to trigger at slightly different times. This helps solve our problem with `every x`, where we were getting lag spikes.
215+
216+
<figure><img src="../../.gitbook/assets/image (2).png" alt=""><figcaption></figcaption></figure>
217+
218+
{% hint style="info" %}
219+
Note that we didn't change the amount of work that we were doing, we just spread that work out over multiple ticks.
220+
{% endhint %}
221+
222+
This means the `on join, while online` pattern is good for spreading work out, but comes with the downsides of a) not stopping with a reload, and b) potentially starting multiple loops for one player if you're not careful.&#x20;
223+
224+
It also means that for short waits, like 1 to 5 ticks, the benefits of spreading work out will be very small, and a periodical event will be your better bet. While loops also can't address sustained lag, only lag spikes.
225+
226+
{% hint style="info" %}
227+
TL/DR:
228+
229+
**every x, loop all players:**
230+
231+
* simple to set up
232+
* safe
233+
* reload-friendly
234+
* can cause lag spikes if the work done is too much to do in one tick
235+
236+
**on join, while online:**
237+
238+
* can spread work over multiple ticks if the delay is long enough ( >5 ticks)
239+
* not reload-friendly
240+
* needs extra work to make safe (prevent multiple loops from running at once for one player)
241+
242+
**conclusion:**
243+
244+
prefer using `every x, loop all players` when you are working with fast timings or smaller amounts of work.&#x20;
245+
246+
prefer `on join, while online` when you are working with slower timings or larger amounts of work (updating scoreboards is a good example, you only need to do this at most once a second.)
247+
{% endhint %}
248+

0 commit comments

Comments
 (0)