-
Notifications
You must be signed in to change notification settings - Fork 0
Tutorial Variables
Important
This file is still a work in progress.
Variables in HySkript work the same way as Skript (for Minecraft).
Incase you haven't used Skript before, this tutorial should outline the basic understanding of how variables are handled.
You can modify/add databases in the config.sk file.
This is what the default databases section will look like in the config.
databases:
# dummy: (This is the name of your database)
# type: json-database
# This is the type of database, currently only 'json-database' is supported
#
# enabled: true
# Whether or not the database is enabled
#
# file-type: json
# The file type of the database, currently only 'json' and 'bson' are supported
# 'json' = Will save as a JSON file in text format (Easy to read/change if you need to).
# 'bson' = Will save as a BSON file in binary format (This is what Hytale uses, much smaller file).
#
# file: variables.json
# The file to save variables to.
#
# pattern: (?!-).*
# The regex pattern to match variables to.
# '.*' = Will save all variables.
# '(?!-).*' = Will only save variables that don't start with '-' (ex: '{-some_var}' will not be saved).
default:
type: json-database
enabled: true
file-type: json
file: variables.json
pattern: .*
The name of your database is used internally, and doesn't really matter.
Choose whatever you'd like.
This represents the type of database you can use.
Addons can create their own databases as well.
Currently json-database is the only database type we offer.
Set whether this database is enabled.
When disabled, variables will not be loaded from this database nor will they be saved to this database.
This is used in the json-database to determine which type of saving will occur.
-
json= Will save in Json format in string form in a file (easy to read and modify). -
bson= Will save in Json format but in a binary file (saves space).- This is the format Hytale uses for saving data.
This is where your variables will save.
In the json-database format, you should use:
-
json= "file-name.json" -
bson= "file-name.bin" You can choose whatever name you want, but should stick with the appropriate file extensions.
This decides which variables can be saved to this file.
The default pattern is (?!-).*, which will save all variables EXCEPT variables that start with a hyphen.
Examples: {-someVariable} will not be saved to file.
We often refer to this as Ram/Memory variables.
You could also use the .* which is from the original version of Skript, which will save ALL variables (except local variables) to file.
HySkript handles a few different types of variables.
Any variable starting with an underscore _ will never save to file.
These are called local variables.
They are local to the event/structure they're created in.
They are never passed out of that event.
They are local to each calling of said event/structure as well, meaning each time your code is run, the variables will be deleted as soon as the execution stops.
By default (editable in the config) variables that start with a hyphen will not be saved to file.
Example: {-myVariable} will never save to file.
These variables are still global, and will work across all your scripts.
They only live while the server is running.
As soon as the server stops, bye bye variable.
All variables that do not start with an underscore will be considered global.
This means you can access them across any script file.
Depending on how you structure your variable, it can be a single variable or a list variable.
Single variables are just as they sound, they hold a single object.
Example:
set {myVariable} to name of player
In this example, this variable is named/indexed "myVariable" and will hold just one player object.
List variables allow you to hold a list of objects.
The list is defined by using a double colon ::.
Variables have three parts, their name, index and value.
Example:
set {variableName::variableIndex} to player
set {variableName::*} to all players
This may look like a single variable, but it's able to hold more than one object.
The ::* signifies that this is a list and can hold multiple objects.
When setting a list this way with multiple objects, they indexes will be created in numerical order starting at 1.
Example:
set {var::*} to all players
Internally this would look like this:
{var::1} -> Bob
{var::2} -> Simon
{var::3} -> Sarah
{var::4} -> Tim
Important
Even though the index looks like a number, they're ALWAYS in string format.
Variables can be set to any object on the server, but only certain types of variables can be saved to file.
If you check out the Types you will see whether the type can be serialized:
Can Be Serialized: true/false
If true, the variable will be saved to file and loaded next time your server starts.
If false, the variable will be lost when the server stops.
Why are some types not serializable you may ask?
This may be either due to complexity when attempting to serialize/deserialize an object or it may be due to issues that may arise.
For example, let's say you were to save an Entity to file.
Next time the server loads, that Entity wont exist yet until that entity is loaded again when their chunk loads.
Because of this, some variables just don't appropriately work for serialization.