8000 Variables · TriggerReactor/TriggerReactor Wiki · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
MidnightSugar edited this page Oct 4, 2021 · 24 revisions

Types of 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

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"

Number

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

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.

Local Variables

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).

Creating local variable

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.

Internal Variable

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.

WARNING!

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.

Working with Variables

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.

Hopefully this image helps a little

Global Variables

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.

Creating Global Variables

Global Variables Using Triggers

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.

Global Variables Using Commands

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".

Location and Item Variables

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.

Concatenation within Variables

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.

Temporally Global Variable

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.

Usage

To create a temporary global variable, put a ? sign next to the { bracket.

{?"my.variable"} = 3

Characteristics

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

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

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

Plugin Description / 목차

1. Getting Started () (рус)

S.L. In-game Editor () (рус)

2. Triggers () (рус)

List and usage of Triggers / 트리거 목록과 사용 방법:

  • List of Executors / 실행자(Executor) 목록

4. Placeholders () (рус)

  • Using PlaceholderAPI / PlaceholderAPI 사용법
  • List of Placeholders / 플레이스 홀더(Placeholder) 목록

5. Conditions () (рус)

  • Creating Conditions / 조건식 만들기
    • Boolean Expressions / 부울 (Boolean) 표현 방법
  • Logical Operators / 연산자 사용법
  • IF statement / IF 조건문
  • Null Checking / Null 검사법
  • Switch Case / Switch Case 조건

6. Variables () (рус)

  • Local Variables / 지역 변수
  • Global Variables / 전역 변수

Advanced

Timings () (рус)

7. Methods () (рус)

  • Using Methods / 메소드 사용법
  • Special Data Types / 특수한 데이터 형식
  • Reading Javadocs / Javadoc 읽기
  • Handling Enum / Enum 데이터 처리
  • Lambda Expresion / Lambda(람다) 식 사용법

8. Array () (рус)

  • Creating an empty array / 빈 배열 만들기
  • Storing data into array / 배열에 데이터값 저장하기
  • Read data from array / 배열에서 데이터 읽기(불러오기)

9. Loops () (рус)

  • WHILE loop / WHILE 반복문
  • FOR loop / FOR 반복문
    • Iterating Collection / Collection 형식의 변수 순회법
    • #BREAK executor / #BREAK 실행자
    • #CONTINUE executor / #CONTINUE 실행자

10. Sync Mode () (рус)

  • #CANCELEVENT executor / #CANCELEVENT 실행자
  • Setting Sync/Async Mode / 동기, 비동기 모드 전환
    • Custom Trigger
    • Area Trigger

11. Custom Executors () (рус)

12. Plugin Access () (рус)

  • Check And Use / 플러그인 존재여부 확인
    • Get Third Party Plugin / 제 3자 플러그인 불러오기
    • Check Eligibility / 호환성 확인하기
    • Use the Plugin / 플러그인 사용하기

13. IMPORT Statement () (рус)

  • Creating new instance / 새 인스턴스 생성하기
  • Accessing static method / 종속 메소드 불러오기
  • Accessing static field / 종속 Enum 불러오기

14. IS Statement () (рус)

  • Understanding / 이해하기
    • Understanding Instance / 인스턴스 이해하기
    • Understanding Superclass / 부모클래스 이해하기
    • Understanding Subclass / 자식클래스 이해하기
  • Using IS Statement / IS조건연산자 사용하기

15. TRY-CATCH Statement () (рус)

  • Understanding TRY-CATCH Exception Handling / TRY-CATCH 예외처리 이해하기

Misc

16. Interface Casting () (рус)

module x.x does not "opens x.x" problem

  • List of Custom Events

Examples

Trigger

Trigger Example () (рус)

More Examples: Bukkit, Sponge

Case Specific

Clone this wiki locally
0