Getting Started

How to write your first scripts.

Script Files

Scripts are written in files with the extension .script.

In order to be run, the file must first be parsed and loaded by a domain's script manager.

The loading/running process is provided by the domain (application) that is managing your script.

The UTF-8 file encoding is recommended for script files. Parsing success may vary with other non-standard encodings.

Script file names must include no whitespace or control characters (e.g. /, \). There are no other official naming conventions. Domains may impose extra name requirements.

Editing a Script

A script file can be edited in any plain text editor, but a code-focused editor (e.g. Visual Studio Code, Atom) is recommended.

Whitespace

CraftScript is fairly lenient with whitespace.

Most statements permit any amount of whitespace between elements.

number = 5 + 5
number=5+5
number    =5 +5

Some (keyword) statements require at least one space character to separate elements.

if 10>5 { // ok
}

if10>5{ // no no bad
}

New lines are used to separate line statements.

if 5 == 5 { // good
    /print hi
}

if 5 == 5 { /print hi } // bad

Indentation is not required, but may be useful to help you!

    if 5 == 5 { // ok, looks weird
/print hello
        }

if 5 == 5 { // ok, looks nice
    /print hello
}

Code Structure

All code inside a script is made of statements.

A statement is a section of code that can be executed (run) and produces a value.

Each (non-empty) line in a script file is a statement.

var = 10      // statement

15            // statement
/print hello  // statement

if var is true { //statement

} // statement

Most statements contain other (smaller) statements.

There is no definite limit (other than parsing memory) on how many statements can occupy the same line.

Every statement has a result value.

This allows one statement to be used as an input for another.

var = if 5 > 8 {
}

assert var == false

Variables

Variables are containers to store data values in your script.

Variables are local to your script. They are not shared with other scripts.

A variable can be set by name.

number = 5
word = "hello"
my_variable = word

Variables do not have a fixed type, and can be set to a different value anywhere within the script.

number = 5
number = number + 2

assert number == 7

number = "hello"

assert number == "hello"

A variable is retrieved using its name.

name = "Jeremy"
if name == "Jeremy" {
    ...
}
/print {name}

Keywords

Keywords are words at the start of special control statements. They add functionality to the language.

What Can a Script Do?

Scripts can do any of four things:

  • Run a Minecraft command (from the perspective of the person running the script)

  • Logic (maths, if/else, for each, etc.)

  • Write or read a variable or property

  • Run another script

Minecraft Commands

Scripts can run commands provided by Minecraft or by a Bukkit plugin.

Command Access

All commands are run from the context of the sender that ran the script. If the sender cannot run the command neither can the script.

E.g. the console cannot run /tp so a script run from the console cannot run /tp.

If a sender does not have access or permission to run the command neither does the script.

The user receives any messages and feedback from the command as if they had entered it manually.

Command Format

Commands start with a slash / character. The command name and arguments follow the slash.

No special formatting, brackets or quotes are required.

/say hello everybody!
/tp @s ~ ~1 ~

Interpolation

Code statements like variables can be interpolated in the command. Their output will be a string.

Surround the code in curly { and } brackets.

message = "hello"

/print message
// "message"

/print {message}  
// "hello"

/print {5 + 5}
// "10"

Last updated