You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: structures/expressions/variables.md
+92-15Lines changed: 92 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -170,19 +170,19 @@ This would allow an atomic variable to be returned from a function secretly, but
170
170
This will be an `AtomicVariable` object and may be difficult to manipulate.
171
171
{% endhint %}
172
172
173
-
### ThreadLocal Variables
173
+
### Thread-Local Variables
174
174
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.
176
176
177
-
Threadlocal variables are accessible **anywhere** on the current process (thread). This includes other functions and lambdas. Threadlocal 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.
178
178
179
179
{% hint style="info" %}
180
180
A thread/process is like a queue of instructions, executed in order. Using the `wait` or `sleep`effect will pause the thread.
181
181
182
182
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.
183
183
{% endhint %}
184
184
185
-
In normal code, threadlocal variables function exactly the same as regular value variables.
185
+
In normal code, thread-local variables function exactly the same as regular value variables.
186
186
187
187
```clike
188
188
set {_var} to 100
@@ -195,17 +195,17 @@ if {_var} is 6:
195
195
Remember: a variable named `{_var}` is **different** from a variable named `{var}`.
196
196
197
197
```clike
198
-
set {_var} to 1 // threadlocal
198
+
set {_var} to 1 // thread-local
199
199
set {var} to 2 // normal
200
200
assert {var} is not {_var}
201
201
```
202
202
203
-
Threadlocal 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.
204
204
205
205
```clike
206
206
function first:
207
207
trigger:
208
-
set {_var} to 10 // threadlocal
208
+
set {_var} to 10 // thread-local
209
209
set {var} to 5 // normal (local to this trigger)
210
210
run second()
211
211
@@ -215,12 +215,12 @@ function second:
215
215
assert {_var} is 10 // value is kept from before call
216
216
```
217
217
218
-
Threadlocal 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.
219
219
220
220
```clike
221
221
function first:
222
222
trigger:
223
-
set {_var} to 10 // threadlocal
223
+
set {_var} to 10 // thread-local
224
224
assert {_var} is 10
225
225
run second() // value is changed in this function
226
226
assert {_var} is 5
@@ -232,12 +232,12 @@ function second:
232
232
assert {_var} is 5
233
233
```
234
234
235
-
Threadlocal 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.
236
236
237
237
```clike
238
238
function my_function:
239
239
trigger:
240
-
set {_var} to 10 // threadlocal
240
+
set {_var} to 10 // thread-local
241
241
set {thing} to a new runnable:
242
242
assert {_var} is 10
243
243
set {_var} to 5
@@ -254,13 +254,14 @@ This is because background processes are run on a **different** thread, so their
254
254
```clike
255
255
function my_function:
256
256
trigger:
257
-
set {_var} to 10 // threadlocal
257
+
set {_var} to 10 // thread-local
258
258
set {thing} to a new runnable:
259
259
assert {_var} is null
260
260
set {_var} to 5 // does NOT update the other _var
261
261
assert {_var} is 5
262
262
assert {_var} is 10
263
263
run {thing} in the background // run on a DIFFERENT thread
264
+
wait 10 milliseconds // wait for other thread to finish
264
265
assert {_var} is 10 // _var is unchanged
265
266
```
266
267
@@ -270,13 +271,13 @@ Events and other entry-points are triggered on a new thread, so there is no cros
270
271
on load:
271
272
trigger:
272
273
assert {_var} is null // not set yet
273
-
set {_var} to 10 // threadlocal
274
+
set {_var} to 10 // thread-local
274
275
assert {_var} is 10
275
276
276
277
on load: // different event trigger, so run on different thread
277
278
trigger:
278
279
assert {_var} is null // not set on THIS thread
279
-
set {_var} to 10 // threadlocal
280
+
set {_var} to 10 // thread-local
280
281
assert {_var} is 10
281
282
```
282
283
@@ -288,7 +289,7 @@ Transferring these will **copy** the variables, so the original copy will not be
288
289
function my_function:
289
290
trigger:
290
291
set {thread} to the current process
291
-
set {_var} to 10 // threadlocal
292
+
set {_var} to 10 // thread-local
292
293
set {thing} to a new runnable:
293
294
run copy_threadlocals_from({thread})
294
295
assert {_var} is 10 // copied from other _var
@@ -298,3 +299,79 @@ function my_function:
298
299
run {thing} in the background // run on a DIFFERENT thread
299
300
assert {_var} is 10 // _var is unchanged, the COPY was changed
300
301
```
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
0 commit comments