-
-
Notifications
You must be signed in to change notification settings - Fork 17
Variables
All variables (internal, local, global) can have a different type, and you have to be aware the types. For basic usage, you only have to know about these three types: String, Number, and Boolean.
String is a special datatype which represents collection of characters. To define a set of characters as a String, wrap them in double quotes. "HI"
Pretty much self explanatory. This type encompasses any number, and because the Number type is a number just like its name, you can do arithmetic operations on it.
For example, we might can have a Number 111 and a String "111". What are their difference? It becomes very obvious just looking at the example
Example Code:
#MESSAGE "111" + 1 // 1111
#MESSAGE 111 + 1 // 112
Do you see the difference? For the String, the + sign appended the 1 to the end of it, but For Number, the + sign did arithmetic operation, so the result is 112. For String, + is not addition but String Concatenation, which will append whatever value next to + sign to the left String operand.
Another example:
//Notice the bracket is used to do arithmetic operation first instead of
// String concatenation.
//If you remember the first chapter, codes read from left to right,
// hence, without the bracket,
// the result will be 'sum is 1020' instead of 'sum is 30'
#MESSAGE "sum is "+(10+20) //sum is 30
#MESSAGE "sub is "+(10-20) //sub is -10
#MESSAGE "product is "+(10*20) //product is 200
#MESSAGE "div is" + (10/20) //div is 0 *Strange huh
//Remark: For integer by integer division, decimal points will be dropped.
//For example, 2/5 will be 0 instead of 0.4
//If you specifically want it to be decimal, one of left operand or right operand
// must be decimal.
//For example, 2.0/5 or 2/5.0 will be 0.4
Boolean is a special kind of type that has only two values: true and false. You will learn more about these in next chapter, Conditions.
When you think about a local variable, you can imagine that a small storage that is placed inside a box. That means, you can access to this storage while you are in this box. The box here is the Trigger we make, and any local variables in this trigger can be only used by this trigger. Local variables will be lost when the trigger execution ends (This doesn't mean that the changes you made on the variables like 'player' will be gone! Just anything in this box called trigger will disappear).
To make a variable, Use assignment operator '='. Anything on the right side of '=' will be assigned into left side variable.
temp = 5
#MESSAGE temp
Here we are creating a new local variable, and are assigning the value of 5 to it. If we ran the following script, it would output 5. Notice that there is no quotation marks around temp. More on that later.
Numbers aren’t the only variable type that can be assigned to a variable, other types can also be used. We can also parse math expressions.
temp2 = temp - 3
#MESSAGE temp2
This would output 2, because 5 - 3 equals 2.
Example of variable assignment with a String. Strings are just a fancy term for text.
temp3 = "text"
#MESSAGE temp3
This would output "text".
Note that these kinds of variables are temporary. When your script is complete, they will be deleted.
Some variables are called Internal Variables, and they represent actual components of your server. For example, player
variable pretty much exists in every Trigger, and this variable allow you to do operations related to the player. You may want to check Methods for more detail, but it's not mandatory for now.
The most important thing to point out is that you still can replace this player
variable with any value you want. If we try to do the following:
player = 5.0
You are going to lose access to all the player information within the trigger. So be careful not use any internal variable names when creating local variables.
To find out what internal variable are available, there is/are section/sections under every Trigger wiki pages.
You will come across cases where simply putting in a single variable will not be enough. You may need to work with multiple variables and data types in a single executor.
age = 16
#MESSAGE "My age is " + age + "."
This would output "My age is 16." The double quotes tell TR that it is a string and to output the text as is. If there are no double quotes, TR will think that it is a variable. The plus sign is the concatenation operator. This adds the strings and variables together so it will output the full sentence.
age = 16
year = 2018
#MESSAGE "My birthday was in the year of " + (year - age) + "."
This would output "My birthday was in the year of 2002." If you are familiar with the order of operations, you should know that the subtraction within the brackets will be done first. After that, TR will parse the variables, then it will add the strings together.
However, if you are not familiar with the order of operations, the order is just same as the normal mathematics order. Multiplication or Division will be calculated first, and then the Addition(concatenation) and Subtraction. For the same orders, operation will go from left to right. To manually change the order, use the bracket like the above.
Global variables are very similar to local variables. The main difference is that you can use them from anywhere in TR, and they will not disappear when your script is complete.
The main way of creating global variables in by putting them inside your triggers. They are surrounded by curly brackets and are assigned the same as if it was a local variable.
{“Plugin.Enabled”} = true
#MESSAGE {"Plugin.Enabled"}
This would output "true". Notice that we use double quotes inside the curly brackets. This is because global variables use string to reference the value.
After running the script above, if you open up var.yml you will find the following:
Plugin:
Enabled: true
This is how TR stores your global variables. It can be accessed from anywhere in TR. Notice how "Enabled" is a child node of "Plugin". We use the "." operator to organize global variables in a yaml structure.
You can also create variable in game by using /trg variables {name} {value}. If you omit {value}, and just do /trg variables {name}, it will return the current value of the variable.
- /trg variables Plugin.Enabled true - This will create the same variable as above.
- /trg variables Plugin.Enabled - This will return "true".
You can save item and location data into a variable by using the /trg variables command.
- /trg vars Location locations.test - This will save your current location into global variable locations.test
- /trg vars Item items.test - This will save the item you are holding into global variable items.test
Note that when setting Location and Item data, the syntax is actually in reverse! "Location" and "Item" come before the variable name. This is intentional.
This is where things get a bit complex, but it is also where global variables really shine.
playeruuid = player.getUniqueID()
{playeruuid + ".isActive"} = true
Notice how we use the concatenation operator "+" to add variables and strings together within the global variable. If we opened var.yml:
85532811-ddf2-44b4-83d0-e8b5f2e2ac48:
isActive: true
Using this method, we can give each user their own variable based on their uuid.
A temporary global variable is developed to address the problem where developers want to share a variable with multiple triggers, but they do not want it to be saved. This is useful to keep track of temporal information like player specific marks, such as pvp tag. A pvp tag is a tag a player gets when they get hit by another player. When the player who is tagged logs-out during combat to prevent inventory loss, he or she can be punished. Clearly, this information should not be saved permanently. Temorary Global Variables don’t save their content in var.yml, so it does not have any overhead, and you don't have to delete all the temporary variables as it will not be saved in var.yml.
To create a temporary global variable, put a ? sign next to the { bracket.
{?"my.variable"} = 3
Temporary Global Variables work just like the normal Global Variables, but they don’t share the same space with normal Global Variables. For example, if you save 2 in a normal Global variable and 3 in Temporary Global Variable with the same name, they do not overlap.
{"my.variable"} = 2
{?"my.variable"} = 3
#MESSAGE {"my.variable"}
#MESSAGE {?"my.variable"}
Result:
2
3
The value 3 will be deleted as soon as the server shuts down as it's temporary.
null is special kind of value indicating that a variable doesn’t exist. If the specified variable does not have a value, it defaults to null.
#MESSAGE test //null
test = "yo"
#MESSAGE test //yo
The player variable is a special variable in trigger reactor. It is used internally by placeholders and executors. Therefore, by modifying this variable in your script, you can change the target of executors and placeholders.
Sometimes this is necessary in certain triggers, where the player variable is not automatically defined, like custom or repeating triggers.
For example:
player = player("MidnightSugar")
#MESSAGE "I am MidnightSugar!"
player = player("wysohn")
#MESSAGE "I am wysohn!"
This script will send the first message to MidnightSugar, and the second message to Wysohn.
With the use of loops, you can also make executors be sent for all online players.
FOR player = getPlayers()
#MESSAGE "This is being sent to every player!"
ENDFOR