Skip to content

Commit 6069bda

Browse files
sovdeethgitbook-bot
authored andcommitted
GITBOOK-61: No subject
1 parent cc7caa5 commit 6069bda

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

core-concepts/variables/global-and-local.md

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# Global and Local
22

3-
Variables, as mentioned previously, have two main "scopes" they can have. They can be global, where any script, any file, any event or function or command can access them. Or, they can be local, where only the event/function/command that created them can access them. 
3+
Variables, as mentioned previously, have two main "scopes" they can have. They can be global, where any script, any file, any event or function or command can access them. Or, they can be local, where only the event/function/command that created them can access them.
44

55
```applescript
66
on load:
7+
# no _ to start = global
78
set {a-global-variable} to "hello, world"
9+
# _ at start = local
810
set {_a-local-variable} to "Hey!"
911
1012
# this broadcasts "hello, world" and "Hey!"
@@ -19,9 +21,11 @@ command /test:
1921
2022
```
2123

22-
As seen above, the global variable is the same no matter where it's accessed. The local variable, though, only is set to `"Hey!"` in the event it was defined in, the `on load`. In the command, it's not set to anything yet. This might seem annoying, but it's actually one of the strengths of local variables. 
24+
As seen above, the global variable is the same no matter where it's accessed. The local variable, though, only is set to `"Hey!"` in the event it was defined in, the `on load`. In the command, it's not set to anything yet. This might seem annoying, but it's actually one of the strengths of local variables.
2325

24-
Say you have an event that includes some sort of wait. Let's say, a command that draws particles for 5 seconds. If you were to use a global variable to store the location, you'd need to somehow make the name of the variable unique for every single time the command is called, otherwise the particles move somewhere else if the command is called again within the 5 seconds. 
26+
### Local Variables
27+
28+
Say you have an event that includes some sort of wait. Let's say, a command that draws particles for 5 seconds. If you were to use a global variable to store the location, you'd need to somehow make the name of the variable unique for every single time the command is called, otherwise the particles move somewhere else if the command is called again within the 5 seconds.
2529

2630
```applescript
2731
# bad code:
@@ -45,7 +49,13 @@ command /particles:
4549
wait 1 tick
4650
```
4751

48-
Global variables can also be extremely useful, even if they need to be unique. As shown earlier, global variables can be used as flags to pass infomation over time, or from one command/event to another:
52+
{% hint style="danger" %}
53+
To reiterate: Local variables are **limited to the place they were set**. You cannot get the value of the local variable outside of when you set it, it's only accessible in the same trigger that you defined it in. If you need to pass information between triggers, or over time, use global variables.
54+
{% endhint %}
55+
56+
### Global Variables
57+
58+
Global variables can also be extremely useful, even if they need to be unique. As shown earlier, global variables can be used as flags to pass information over time, or from one command/event to another:
4959

5060
```applescript
5161
command /stop-loop:
@@ -59,7 +69,7 @@ on load:
5969
wait 1 second
6070
```
6171

62-
They're also great for holding onto information over longer periods of time, like data attatched to a player:
72+
They're also great for holding onto information over longer periods of time, like data attached to a player:
6373

6474
```applescript
6575
on join:
@@ -69,11 +79,18 @@ on quit:
6979
add difference between {last-join::%player's uuid} and now to {playtime::%player%}
7080
```
7181

72-
Note that we had to make sure the variables were uniquely named by using the player's uuid.
82+
{% hint style="danger" %}
83+
Remember, global variables are **unique**. This means that if you want a variable to be different for each player, you'll have to make its name different for each player!
84+
85+
The easy way to do this is put the player's uuid in the variable, like so:
86+
87+
```applescript
88+
set {variable::%player's uuid%} to "hello!"
89+
```
90+
{% endhint %}
7391

7492
### When to Use Global vs Local
7593

7694
Let's summarize. Local variables are great at storing data that we only need for a short time, in a specific place. We don't have to worry about using unique names, or resetting it to some starting value, or anything like that. **Local variables should be used whenever possible.**
7795

7896
Global variables are great for storing data in the long term, like over server restarts. They're also great for allowing us to access data from wherever. We can set the variable in one command and access it in a completely different event, even in a different file. **Global variables should be used when needed, either for long-term storage or for communication across various parts of a script or throughout time.**
79-

0 commit comments

Comments
 (0)