Scripts#
What is a script?#
Scripts are the foundation of ZScript. You cannot do anything in ZScript without
scripts. Every script has a void run() function, the entry point for the script.
Each script type runs its scripts based on different things occurring in the engine -
for example, an npc script runs when an npc (an enemy) is alive.
Instance Variables#
Added in version 3.0.
Declaring variables inside a script creates ‘instance variables’. These variables will
hold a different value for each instance of the script that is running. The this variable
of scripts is now an instance variable.
A variable declared with the static keyword will be a global variable instead, holding one
value for all instances of the script. Constants are automatically static.
The compiler option DEFAULT_STATIC_SCRIPT_MEMBERS strongly affects instance variables.
It may mark all script variables and functions as static depending on the setting; this is used to preserve old scripts.
While static global variables in a script can be accessed using ScriptName.var_name,
instance variables cannot be accessed except by the script they belong to.
Exported Variables#
Added in version 3.0.
Script-scope variables can be annotated with some export-related annotations.
Exported variables can have their initial value modified in the Editor when setting
up the script, similarly to InitD[].
If an exported variable has an initializer, the initializer value will be used as a default value, used automatically if no value is specified in the editor. A button in the editor also allows “resetting to default” an exported variable’s value.
A value in [square brackets] indicates an ‘optional’ value.
Many of these annotations conflict with one another, and cannot be used together. The notable exception
is @Export(), which is REQUIRED by the other annotations here.
Note
The values an annotation supplies must match the exported variable’s type. Use int
values (ex. 1) and @ExportBitflags() for an int variable, and long values
(ex. 1L) and @ExportLongBitflags() for a long variable. An @ExportEnum() enum has
int values unless it is declared @Bitflags("long"), so only such an enum may be used
with a long variable. A mismatch is a compile error, as the two kinds of value are
stored differently.
Annotation Name |
Values |
Purpose |
|---|---|---|
|
Name: String (256)
Help Text: [String (65535)]
Type: [“D”, “H”, “LD”, “LH”, “B”, “-1”]
|
Exports the specified variable to be modifiable Sets the label, helptext, and input field type of the textbox for the exported variable. Name must be specified, help text defaults to blank, and type defaults based on variable type, as: int, float, untyped -> “D”long -> “LD”bool -> “B”rgb -> “LH”else -> “-1”
|
|
Min: int
Max: int
|
Sets a range for the exported variable. The field in the editor will not allow setting its value outside of the specified range. If used on a non-exported variable, errors.
If used on a boolean variable, errors.
|
|
String: “Tile”, “Tile CSet”, “Combo”, “Combo CSet”, “Color”, “Item”, “Enemy”, “Counter”, “Sprite Data”, “SFX”, “MIDI”, “Music”, “Save Menu”, “Message String”, “Weapon Type”, “LWeapon Type”, “EWeapon Type”, “Dropset”, “Font”, “Bottle Type”, “Combo Type”, “Combo Flag”, “Level Item” |
Sets the exported value to use a special GUI. For example, “Tile”, “Combo”, and “Color” will use a Tile / Combo / Color selector instead of a number entry field. Most special values use Drop Down Lists, limiting the selection to specific values based on the type. Some of these special modes require multiple variables in a row; for example, ‘Tile CSet’ is only usable on a variable directly after a variable exported with ‘Tile’ mode (causing the two variables to share a gui widget) If used on a non-
int variable, errors. |
|
Takes many parameters. At least 1 required. The ‘value’ starts at ‘0’. Specifying an ‘int’ sets the ‘value’ to the specified number. Specifying a ‘String’ gives a name to the current ‘value’, and then increments the ‘value’ by 1. |
Sets the exported value to use a dropdown list, displaying the specified options for the user to choose from. Ex. |
|
Takes many parameters. Specifying no parameters will automatically name every possible flag by its number. Similar to |
Sets the exported value to use flag checkboxes, displaying a checkbox for each specified option. Ex. Ex. Ex. Ex. |
|
Takes many parameters. Specifying no parameters will automatically name every possible flag by its number. The exact same as |
Same as Ex. Ex. |
|
Enum: Identifier
Display Type:
EnumExportDisplayFlags
|
Exports the specified enum. It will either produce an
If the enum has multiple constants with the same value, only the first constant listed will be shown. Bitflag enums will not show multi-bit values. The ‘Display Type’ determines how the constant names will be displayed. If not specified, EXPDISP_DEFAULT will be used by default. See [EnumExportDisplayFlags] for details. |
Static Functions#
Added in version 3.0.
Functions declared inside scripts work very similarly to variables; an ‘instance function’
can access all instance variables of the script, but any functions declared as static
will be unable to access instance variables from the script. Static variables can still
be accessed as normal.
The compiler option DEFAULT_STATIC_SCRIPT_MEMBERS affects script functions
in almost the exact same way it affects variables; the main difference is that the
void run() function will not be made static by it.
Marking a void run() function as static is an error.
Script Annotations#
Scripts are capable of being targetted with a number of annotations.
For String values, the number in parentheses is the maximum length, in characters.
Added in version 3.0: @ScriptInfo, @ScriptSetup, @ExportInitD0 - @ExportInitD7
These annotations take a single value:
Annotation Name |
Value |
Purpose |
|---|---|---|
|
String (256) |
Indicate who authored the script. |
|
String (256) |
Sets labels in the Combo / Item editors. |
|
String (65535) |
Sets help text in the Combo / Item editors. |
|
String (256) |
Sets labels in the Combo / Item editors. |
|
String (65535) |
Sets help text in the Combo / Item editors. |
|
String (256) |
Sets labels in the Combo editor. |
|
String (65535) |
Sets help text in the Combo editor. |
|
String (256) |
Sets labels in the Combo editor. |
|
String (65535) |
Sets help text in the Combo editor. |
|
String (256) |
Sets labels for the |
|
String (65535) |
Sets help text for the |
|
“D”, “H”, “LD”, “LH”, “B”, “-1” |
Sets the input field type of the textbox for
inputting the int, float, untyped -> “D”long -> “LD”bool -> “B”rgb -> “LH”else -> “-1”
|
|
|
Only valid on This means that this script will run once, for 1 frame, when the player begins a ‘New Game’. The ‘int’ parameter determines the order that these will run in, starting with the lower numbers and counting up. A global script named |
|
String (65535) |
Sets an info / summary / description of the script, shown in the editor UI. |
|
String (65535) |
Sets a ‘setup instructions’ string, shown in the editor UI. |
These annotations take a varying amount of values. A value in [square brackets] indicates an ‘optional’ value.
Annotation Name |
Values |
Purpose |
|---|---|---|
|
Name: String (256)
Help Text: [String (65535)]
Type: [“D”, “H”, “LD”, “LH”, “B”, “-1”]
|
Sets the labels, helptext, and input field
type of the textbox for the specified
Name defaults to the variable name, help text defaults to blank, and type defaults based on variable type, as: int, float, untyped -> “D”long -> “LD”bool -> “B”rgb -> “LH”else -> “-1”
|