Skip to content

Commit 58f7fdb

Browse files
Moderockygitbook-bot
authored andcommitted
GitBook: [#25] No subject
1 parent bf9d21c commit 58f7fdb

File tree

1 file changed

+92
-15
lines changed

1 file changed

+92
-15
lines changed

structures/expressions/variables.md

Lines changed: 92 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -170,19 +170,19 @@ This would allow an atomic variable to be returned from a function secretly, but
170170
This will be an `AtomicVariable` object and may be difficult to manipulate.
171171
{% endhint %}
172172

173-
### Thread Local Variables
173+
### Thread-Local Variables
174174

175-
Atomic variables use the `{_variable}` name pattern. They are **reference** variables.
175+
Thread-local variables use the `{_variable}` name pattern. They are **reference** variables.
176176

177-
Thread local variables are accessible **anywhere** on the current process (thread). This includes other functions and lambdas. Thread local variables are **not** accessible from other threads.
177+
Thread-local variables are accessible **anywhere** on the current process (thread). This includes other functions and lambdas. Thread-local variables are **not** accessible from other threads.
178178

179179
{% hint style="info" %}
180180
A thread/process is like a queue of instructions, executed in order. Using the `wait` or `sleep`effect will pause the thread.
181181

182182
The `run ... in the background` effect can be used to create a different, branching process that will run at the same time as the current one.
183183
{% endhint %}
184184

185-
In normal code, thread local variables function exactly the same as regular value variables.
185+
In normal code, thread-local variables function exactly the same as regular value variables.
186186

187187
```clike
188188
set {_var} to 100
@@ -195,17 +195,17 @@ if {_var} is 6:
195195
Remember: a variable named `{_var}` is **different** from a variable named `{var}`.
196196

197197
```clike
198-
set {_var} to 1 // thread local
198+
set {_var} to 1 // thread-local
199199
set {var} to 2 // normal
200200
assert {var} is not {_var}
201201
```
202202

203-
Thread local variables make it easy to pass data between triggers that are guaranteed to be executed in the same process.
203+
Thread-local variables make it easy to pass data between triggers that are guaranteed to be executed in the same process.
204204

205205
```clike
206206
function first:
207207
trigger:
208-
set {_var} to 10 // thread local
208+
set {_var} to 10 // thread-local
209209
set {var} to 5 // normal (local to this trigger)
210210
run second()
211211
@@ -215,12 +215,12 @@ function second:
215215
assert {_var} is 10 // value is kept from before call
216216
```
217217

218-
Thread local variables are **atomic**, and any use alters the same copy of the variable.
218+
Thread-local variables are **atomic**, and any use alters the same copy of the variable.
219219

220220
```clike
221221
function first:
222222
trigger:
223-
set {_var} to 10 // thread local
223+
set {_var} to 10 // thread-local
224224
assert {_var} is 10
225225
run second() // value is changed in this function
226226
assert {_var} is 5
@@ -232,12 +232,12 @@ function second:
232232
assert {_var} is 5
233233
```
234234

235-
Thread local variables can be accessed and changed from lambdas, unlike regular variables.
235+
Thread-local variables can be accessed and changed from lambdas, unlike regular variables.
236236

237237
```clike
238238
function my_function:
239239
trigger:
240-
set {_var} to 10 // thread local
240+
set {_var} to 10 // thread-local
241241
set {thing} to a new runnable:
242242
assert {_var} is 10
243243
set {_var} to 5
@@ -254,13 +254,14 @@ This is because background processes are run on a **different** thread, so their
254254
```clike
255255
function my_function:
256256
trigger:
257-
set {_var} to 10 // thread local
257+
set {_var} to 10 // thread-local
258258
set {thing} to a new runnable:
259259
assert {_var} is null
260260
set {_var} to 5 // does NOT update the other _var
261261
assert {_var} is 5
262262
assert {_var} is 10
263263
run {thing} in the background // run on a DIFFERENT thread
264+
wait 10 milliseconds // wait for other thread to finish
264265
assert {_var} is 10 // _var is unchanged
265266
```
266267

@@ -270,13 +271,13 @@ Events and other entry-points are triggered on a new thread, so there is no cros
270271
on load:
271272
trigger:
272273
assert {_var} is null // not set yet
273-
set {_var} to 10 // thread local
274+
set {_var} to 10 // thread-local
274275
assert {_var} is 10
275276
276277
on load: // different event trigger, so run on different thread
277278
trigger:
278279
assert {_var} is null // not set on THIS thread
279-
set {_var} to 10 // thread local
280+
set {_var} to 10 // thread-local
280281
assert {_var} is 10
281282
```
282283

@@ -288,7 +289,7 @@ Transferring these will **copy** the variables, so the original copy will not be
288289
function my_function:
289290
trigger:
290291
set {thread} to the current process
291-
set {_var} to 10 // thread local
292+
set {_var} to 10 // thread-local
292293
set {thing} to a new runnable:
293294
run copy_threadlocals_from({thread})
294295
assert {_var} is 10 // copied from other _var
@@ -298,3 +299,79 @@ function my_function:
298299
run {thing} in the background // run on a DIFFERENT thread
299300
assert {_var} is 10 // _var is unchanged, the COPY was changed
300301
```
302+
303+
### Global Variables
304+
305+
Global variables use the `{!variable}` name pattern. They are **reference** variables. These are most similar to the normal variables from [original Skript](https://github.com/SkriptLang/Skipt).
306+
307+
Any copy of a global `{!variable}` anywhere on any process will access the same value.
308+
309+
{% hint style="info" %}
310+
Unlike in original Skript, global variables are not persistent across restarts (by default.)
311+
{% endhint %}
312+
313+
In normal code, globals variables function exactly the same as regular value variables.
314+
315+
```clike
316+
set {!var} to 100
317+
set {!var} to {!var} - 1
318+
print {!var}
319+
if {!var} is 6:
320+
set {!var} to 20
321+
```
322+
323+
Remember: a variable named `{!var}` is **different** from a variable named `{var}`.
324+
325+
```clike
326+
set {!var} to 1 // global
327+
set {var} to 2 // normal
328+
assert {var} is not {!var}
329+
```
330+
331+
Global variables make it easy to pass data between triggers and entire scripts.
332+
333+
```clike
334+
function first:
335+
trigger:
336+
set {!var} to 10 // thread-local
337+
set {var} to 5 // normal (local to this trigger)
338+
run second()
339+
340+
function second:
341+
trigger:
342+
assert {var} is null
343+
assert {!var} is 10 // value is kept from before call
344+
```
345+
346+
Global variables are **atomic** and can be accessed and changed from other triggers, lambdas and different threads, unlike regular variables.
347+
348+
```clike
349+
function my_function:
350+
trigger:
351+
set {!var} to 10 // thread-local
352+
set {thing} to a new runnable:
353+
assert {!var} is 10
354+
set {!var} to 5
355+
assert {!var} is 5
356+
assert {!var} is 10
357+
run {thing} in the background // value is changed by the runnable
358+
wait 10 milliseconds // wait for other thread to finish
359+
assert {!var} is 5
360+
```
361+
362+
Global variables are accessible from **any** process.
363+
364+
```clike
365+
on load:
366+
trigger:
367+
assert {!var} is null // not set yet
368+
set {!var} to 10 // global
369+
assert {!var} is 10
370+
371+
on load: // different event trigger, so run on different thread
372+
trigger:
373+
wait 10 milliseconds // wait for previous trigger to finish
374+
assert {!var} is 10 // set everywhere
375+
set {!var} to 5 al
376+
assert {!var} is 5
377+
```

0 commit comments

Comments
 (0)