-
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.
Advanced Skript Tutorial
-
Hello and welcome to this tutorial!
You've taken a look at Skript, you've made some cool Commands, and you generally know what you're doing (if you don't, check this out!), and you're ready to upgrade your scripting game.
In this tutorial, I'll be showing you some advanced Skript tactics, code efficiency, and some more useful stuff that you might not have known.
Chapter 1 - Changers
Most expressions can be changed, and you probably recognize these changers: set, add, remove, delete and clear. They're used for changing expressions, such asCode (Skript):- add 1 to {list::*}
Code (Skript):- add 1 to {_list::*}
Code (Skript):- set {_list::1} to 1
Code (Skript):- trigger:
- delete {test::*} # resets the values
- set {_n} to now
- loop 10000 times:
- add loop-number to {test::*} # here the 'add' changer is used
- delete {test::*} # resets the values
- set {_now} to difference between {_n} and now
- set {_n2} to now
- loop 10000 times:
- set {test::%loop-number%} to loop-number # here the 'set' changer is used
- set {_now2} to difference between {_n2} and now
- message ""
- message "&e&l10000 &rvariables adding: %{_now}%"
- message "&a&l10000 &rvariables setting: %{_now2}%"
- message ""
- delete {test::*}
The 'set' changer was in this test a whapping 1099.4% faster.
In conclusion: you should use the 'set' changer above all changers (except delete and clear)
Chapter 2 - Loops
Loops are being used all the time. They're a great way for sorting information 1 by 1, and you've probably used them before. You might think the most optimal Loops look something like this:
Code (Skript):- loop all players:
- if player is in world "Lobby":
- send "Like this tutorial!!" to loop-player
Code (Skript):- set {_loop index} to 0
- loop {players::*}:
- set slot {_loop index} of player's current inventory to loop-value
- add 1 to {_loop index}
Code (Skript):- send "Like this tutorial!!" to all players in world "Lobby"
Code (Skript):- loop {players::*}:
- set slot (loop-index parsed as int - 1) of player's current inventory to loop-value
Some shortcuts:
Loop-index
The expression loop-index is the index where the loop currently is, e.g.:
Code (Skript):- set {_index} to 1
- loop {values::*}:
- # some code..
- set {_index} to {_index} + 1
Code (Skript):- loop {values::*}:
- # some code
If you want indices to start, for example, at 0 (maybe for GUIs) you can achieve this with some simple math:
Code (Skript):- loop ..:
- broadcast "%loop-index parsed as int - 1%"
Code (Skript):- set {_index} to 0
- loop ..:
- broadcast "%{_index}%"
- set {_index} to {_index} + 1
This expression is basically just all players. You can use this expression instead of looping all players, for example:
Code (Skript):- loop all players in world "world":
- teleport loop-player to {my location}
Code (Skript):- teleport all players in world "world" to {my location}
Chapter 3 - Packets
Packets.. for most people one of the hardest things to understand in Skript. But it's actually easier than you thought, thanks to some websites. Here, I'll be showing you how to use the protocol wiki from http://wiki.vg/Protocol
You'll want to use MundoSK for packets, this addon currently supports packets the best.
For this tutorial I'll be showing you how to modify the basics, and I'm gonna be using the packet below.
As you can see, the packet above will spawn an experience orb only for specific players.
You can see that it's a client packet ('Bound To' section) and to the right you can see the fields you will need to change. The Entity ID, the location, and the count. In the 'Field Type' section you can see what things you need to modify.
These 'Field Types' will make your life easier, since they literally tell you what things you need to change.
The current changers are these:
Code (Skript):- %type% pinfo %number% of %packet%
- %type% array pinfo %number% of %packet%
- %string% pinfo %number% of %packet%
- %world% pentity %number% of %packet%
- byte pnum %number% of %packet%
- short pnum %number% of %packet%
- int pnum %number% of %packet%
- long pnum %number% of %packet%
- float pnum %number% of %packet%
- double pnum %number% of %packet%
- byte array pnum %number% of %packet%
- int array pnum %number% of %packet%
- %string% pjson %number% of %packet%
- %string% array pjson %number% of %packet%
- %string% penum %number% of %packet%
Code (Skript):- set int pnum %number% of %packet% to %integer%
Code (Skript):- int pnum %number% of %packet%
Code (Skript):- double pnum %number% of %packet%
You may have figured out that in every syntax of modifying a packet, there's a %number% expression, and this is for keeping values apart. These count from 0, and you just have to follow the order the wiki lists them in.
So now I'll show you an example of the picture above with notes, to make it more clear.
Code (Skript):- command /I want an experience orb:
- trigger:
- # create a new packet, most of the times it looks like this: play_(client/server)_(packet name)
- set {_packet} to new play_server_spawn_entity_experience_orb
- # it's the first VarInt in the list, so use number 0
- set int pnum 0 of {_packet} to 1 # sets the Entity ID of the exp orb
- # this is the first Double in the list, so use number 0
- set double pnum 0 of {_packet} to player's x coordinate # sets the x coordinate of where the orb will be spawned to the player's x coordinate
- # this is the second Double in the list, so use 1
- set double pnum 1 of {_packet} to player's y coordinate + 1 # sets the y coordinate of the orb to player's y coord + 1
- # this is the third Double in the list, so use 2
- set double pnum 2 of {_packet} to player's z coordinate # sets the z coordinate of the orb to player's z coord
- # this is the first Short in the list, so use 0
- set short pnum 0 of {_packet} to 30 # spawn 30 exp orbs
- # now send the packet to the player
- send player packet {_packet}
For more information take a look at this tutorial
Mr. Darth, Efnilite, Vrganj and 1 other person like this.