Variables
Variables are a key component in any useful programming language, and Skript is no exception. They allow you to store data, move data around, make code readable, and much more.
At their core, variables are a label for information. If you've taken algebra in math, you probably understand the concept. Instead of using an actual number, you use x
or y
as a stand-in for an unknown or changing number. This is the same concept as variables in programming. Variables are representations, or stand-ins, of unknown or changing data.
Variables in Skript
In Skript, variables are represented by a variable name contained within two curly braces, like so:
Variables can have nearly any name you like. They have have spaces, dashes, symbols, whatever. You can put the whole Bee Movie script into a variable name, if you so choose. There are a few special ways to name variables though.
By default, variables are global
, which means they can be seen and changed by any part of your script. Often we want variables to exist only in a small section of script, which can be achieved by local
variables (those that begin with _
, like {_variable}
). We'll get further into this difference in the Global and Local page.
Using Variables
Let's use a very simple example to start with. We have a command that does a small math equation and outputs the answer. However, we have a section of the equation that repeats a few times. Let's make it easier to deal with using variables.
That's much easier to read! Plus, it means if we need {_x} again later in the command, we have it ready to use, without having to retype it. And if we want to change 7
to 6
, we only have to change 1 number instead of 2 or more. Be careful with using too many variables, though. If overused, they can make code more cluttered and messy than necessary:
Using Variables to Store Data
Global variables in particular are great for storing and transferring data. Here we'll use a very basic /home
command system to demonstrate.
Perfect, right? We save the player's location to a variable, and then when they want to go home, we can just use the variable to look up the current location the home is set to.
Well, this is only partially correct. You see, since global variables are, in fact, global, there's only one of them at any time. So if someone else comes along and does /sethome
, the {home}
variable now has their home location, and if you use /home
, you're getting teleported to their home instead!
Note on Using Expressions in Variable Names
Notice that in the home example, we're using the expression player's uuid
in the variable name. Remember, we can't just write that directly in the variable name. Skript would think we literally mean "player's uuid" instead of replacing it with the uuid we actually want.
So we make sure to surround it with %
, so that Skript know it's an expression, not a literal bit of text.
Last updated
Was this helpful?