Variables Explanation

Variables

Variables are little names we give to store some information in. Remember solving for 'x' in your math class? Variables are just like that, but the computer does the solving!

I like to think of variables as boxes with a label. So I'm going to put the number 5 in a box labeled {box} When I look at the contents of the box later, I'm going to find the number 5. I don't need to remember what is inside, as the computer does all of that. All I need to do is say what to do depending on the value of the variable (otherwise known as the contents of the box). For example I could say "If the contents of {box} is 5 then kill all the players"

Now that you know what variables in general are, let's talk about what they can do in Skript!

Variables in Skript

In most programming languages, variables need to be declared, and can only be one type. (ie only a number or text ect) In Skript you can use a variable name anywhere you want, and if it doesn't exist, Skript will create one of the type needed.

All variables in Skript have curly brackets around them "{}" Then on the inside you put whatever you want to call the variable (again its the same as putting a label on a box). Usually you will put a dot between the name as such {this.is.a.variable.name} However it is not required and you could use spaces or whatever you would like except for '%' They have their own special purpose.

Nest Variables

Nest variables are when you put the value of an expression inside the variable name. An example could be :

{variable.%player%}

When Skript reads the variable, it will replace %player% with the name of the player. If you were to look in the variables.csv file (You should probably never look in there just in case something gets messed up, but for the sake of the example, I'm going to tell you what you might find) you would see

{variable.Demon_Penguin}

Skript would fill in the name of the player. In this case it was Demon_Penguin. This will essentially make a separate variable per player, which is used in most scripts. You can also put the value of another variable inside a variable name like this:

{favorite.%{color}%}

If {color} was set to green, then you would see {favorite.green}.

There may also be situations where you need multiple expressions to refer to the same player. For example, in a loop, you may need to refer to loop-player to get the correct player for a variable.

Local Variables

Local variables are used when you only want to use a variable for a little bit and don't want/need to save it. There can also exist multiple local variables of the same name at the same time. This is useful for things like log in events. While the code is executing for one player, another player may log in and need their own variable. In order to tell Skript you want this variable to be a local variable all you need to do is prefix the variable name with an _ Looks like this {_local} (Note that you may see it used as '*' in older posts, however in newer versions of Skript the underscore _ is used)

Option Variables

Option variables are a bit different from other variables. Any time they are used in code, they are replaced by their value. They also cannot be modified by the code in any way. Option variable (unlike other variables in Skript) need to be declared. There is a section you can add (usually at the top of the script) that declares the options and what their values are. They work almost like a little config file, which is what most scripters use them for.

options:
	Name: value

In order to tell Skript that you are using an option variable (outside of the options section) you prefix the name with the @ symbol. Looks like this {@Name}

List Variables

List variables are very special and complicated variables. They can hold an entire list of values, and not all of them need to be the same type. Their usage usually looks like {list::*}

Following my box example, I will compare a list variable to a room full of boxes. The room is called by the name of the list variable, and the name of a box in the room is the stuff after the two colons. Whenever you use the base name of the variable (for example {list::*}) you are referencing the whole room. The different parts of list variables are explained below:

Index

WARNING: List variables are by far the most complicated of all the variable types. Only attempt using them if you feel you have a good understanding of the other variable types.

List variables have two parts to them: Their name (The name of the room) and the Index (The name of a box in that room). All list variables (rooms) start off empty. So lets start by learning how to put a box in that room.

add the player to {list::*}

What this does is creates a box that holds the player in the event. You may notice that we didn't tell Skript what to name the box. All we told it to do was put it in the room we call {list::*}. Skript names these boxes by numbers. It starts at 1 and works its way up to infinity. So the actual name of the variable that is holding our player is "{list::1}" This works well when we really don't care what the box is called, but only the fact that it is in the room. For example, if we had a variable that held all of the mods on the server, it doesn't matter if they are mod 1 or mod 2, just that they are on the moderator list. However, in many cases, you will want to be able to name every box in the room so we know right where to get it when we want to access it.

All you need to do is set the name (The part after the colons) in the variable. You can write it out, or you can even use expressions to set it for you:

set {list::mod} to 7
set {list::%player%} to true

Notice that we no longer use the 'add' effect? This is because we are not simply adding a box to the room (Which would give us the default number system), but we are adding a box with a specific name. So we need to set that box's name to something, and give it a value. You can also use this method to access a specific box in the room (One value out of a list variable) at any time in your code. This makes them work very similar to nested variables.

This technique allows for the quick deletion of a list of variables. Rather than setting nested variables to a value, you can do the same thing with a list variable, but it will be set up in such a way to be able to easily delete the entire list:

set {mod.%player%} to true # Old method
set {mod.list::%player%} to true #New method

delete {mod.list::*} #When you use the * as the index it refers to the entire room, rather than a specific box
Looping

To start a loop of a list variable you start it off with:

loop {list::*}:

What this does is check all the different boxes in that room, one by one.

Now you have a few values to deal with.

  • The loop-value is the value of the variable. Whatever is in that box.
  • The loop-index is what ever is after the colons for this particular box (ie the name of that box)

Then you can deal with each value one by one as it loops through each box.

.

.

.

If you need help of any kind you should search through the help forum. If you can't find what you are looking for, then feel free to make your own post. http://dev.bukkit.org/server-mods/skript/forum/help/

-Demon