Variables
Containers to hold, manipulate and store data in a program.
Variables are named containers for storing objects in code. They typically function as expressions, and use the simple set/get/delete
behaviour.
Variables can be recognised by the {...}
curly brackets around their name. Special types of variable are recognisable by a non-alphanumeric character @$#!?
at the start of their name.
Some types of variable have special behaviour in certain situations, detailed below.
Value Variables
Most variables are value variables: these store a raw value. Value variables have the standard {variable}
name pattern.
Normal (value) variables are local to the current trigger
section.
Variables can be set and retrieved like a normal expression.
Variables can be set to each other.
Setting one will not affect the other - these are different containers for the same object.
Using a variable as a return or an argument will pass its value.
The function that uses it will not change the original variable.
The same is true for lambdas, which will also freeze the variable value.
Atomic Variables
Atomic variables use the {@variable}
name pattern. They are reference variables, and store a reference to the object rather than the object's value itself.
In normal code, atomic variables function exactly the same as regular value variables.
Remember: a variable named {@var}
is different from a variable named {var}
.
Atomics are designed for being passed to lambdas or background functions. When passed as an argument to something, the passed argument is linked to the original.
Changing the passed copy will change the original.
This also works in function arguments, if and only if the function uses a special atomic parameter.
If a function does not take an atomic parameter, the value will be extracted. This means that the original copy will not be changed.
The reverse is also true: passing a non-atomic argument to an atomic parameter will wrap the argument as an atomic inside that function, but will not alter the original copy.
Atomics cannot be passed as return values: their value will always be extracted.
For advanced users, the raw atomic handle can be extracted using the get_atomic_literal(object)
function from the skript namespace.
This would allow an atomic variable to be returned from a function secretly, but re-wrapping it for use would be complex.
This will be an AtomicVariable
object and may be difficult to manipulate.
Thread-Local Variables
Thread-local variables use the {_variable}
name pattern. They are reference variables.
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.
A thread/process is like a queue of instructions, executed in order. Using the wait
or sleep
effect will pause the thread.
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.
In normal code, thread-local variables function exactly the same as regular value variables.
Remember: a variable named {_var}
is different from a variable named {var}
.
Thread-local variables make it easy to pass data between triggers that are guaranteed to be executed in the same process.
Thread-local variables are atomic, and any use alters the same copy of the variable.
Thread-local variables can be accessed and changed from lambdas, unlike regular variables.
However, lambdas or functions run in the background will not be able to access the value.
This is because background processes are run on a different thread, so their thread-local {_variables}
are different.
Events and other entry-points are triggered on a new thread, so there is no cross-contamination between processes.
The skript namespace has a special function to transfer thread-local variables to a different thread.
Transferring these will copy the variables, so the original copy will not be updated by changes.
Global Variables
Global variables use the {!variable}
name pattern. They are reference variables. These are most similar to the normal variables from original Skript.
Any copy of a global {!variable}
anywhere on any process will access the same value.
Unlike in original Skript, global variables are not persistent across restarts (by default.)
In normal code, globals variables function exactly the same as regular value variables.
Remember: a variable named {!var}
is different from a variable named {var}
.
Global variables make it easy to pass data between triggers and entire scripts.
Global variables are atomic and can be accessed and changed from other triggers, lambdas and different threads, unlike regular variables.
Global variables are accessible from any process.
Last updated