Skip to content
Flint
Dokumentation
Start

Language basics

Values, text, conditions, loops, functions and errors.

Values

name = "Steve"                  # text
level = 5                       # numbers are decimals: 5 / 2 is 2.5
ready = true                    # true, false, none
items = [1, 2, 3]               # list
ages = {steve: 20, "alex": 25}  # map (keys are text)
delay = 5 seconds               # duration: ms, ticks, seconds, minutes, hours, days

none means “no value”. Reading a map key that does not exist gives none, and assigning none to a key removes it. That makes coins[id] or 0 the usual way to read with a default.

Text

print("Hello {name}, next level is {level + 1}")
print('single quotes work too')
print("A literal brace: \{")
print("Line one\nLine two")

Anything inside {...} is evaluated. Text sent to players is parsed as MiniMessage (<green>, <bold>, <gradient:red:blue>…). Always wrap what players typed in escape(...) from the text module before putting it in a message, so they cannot inject tags.

Text has methods such as upper(), replace(a, b), split(","), starts_with(x) and contains(x). See the text type.

Conditions and loops

if level > 3 and not ready:
    print("almost")
elif name in ["Steve", "Alex"]:
    print("a classic")
else:
    pass

label = "high" if level > 5 else "low"

for item in items:
    print(item)

for key, value in ages:          # for lists: index, item
    print("{key} is {value}")

while level < 10:
    level += 1
    if level == 7:
        break

Operators: + - * / %, comparisons == != < <= > >=, and, or, not, in, not in, and the assignments = += -= *= /=.

Functions

fn double(x, times = 2):
    return x * times

print(double(4))        # 8
print(double(4, 3))     # 12

Functions can call themselves, wait, and be passed as values (for example to scheduler.later or to a menu button).

Variables and scope

Variables assigned at the top level of a file are shared by every function, handler and command in that file. A variable first assigned inside a block is local to that block’s function.

count = 0                   # script-wide

on player.join:
    count += 1              # changes the script-wide variable
    greeting = "Hi"         # local to this handler

To keep a value across restarts, declare it with persist. See Saving data.

Errors

try:
    risky()
catch problem:
    print("failed: {problem}")

Every error a script can cause (a missing player, a failed query, a wrong argument) can be caught this way.

Modules

Functionality beyond the basics lives in modules, which a script imports:

import time, random as rnd
from text import escape

print(time.format("HH:mm"))
print(rnd.int(1, 6))

A module is only loaded on the server when a script imports it. Using one without importing it is a load error that tells you which line to add. These functions are always available: print, len, str, num, int, range, type, min, max, sum, abs, round, floor, ceil and format. See the reference for every module.