Skip to content

Examples

Dimitri edited this page Aug 28, 2026 · 4 revisions

Examples

Complete, worked PotScript programs. See the Standard Library page for the builtins used here.

Ore-sorting timer with a heartbeat over wifi

sethost("timer")

fn hms(ticks) {
    let secs = floor(ticks / 20)
    return str(floor(secs / 60)) + "m" + str(secs % 60) + "s"
}

let pulses = num(load("pulses") or "0")
while true {
    rs_set("up", 15)
    sleep(4)
    rs_set("up", 0)
    pulses = pulses + 1
    store("pulses", pulses)
    broadcast(["pulse", pulses, hms(uptime())])
    sleep(196)      # 10s total cycle
}

Monitor that collects heartbeats

sethost("monitor")
let counts = []
let names = []

while true {
    let msg = recv(600)             # 30s timeout
    if msg == nil {
        print("[quiet] " + str(len(names)) + " known hosts")
        continue
    }
    let from = msg[0]
    let at = find(names, from)
    if at < 0 {
        push(names, from)
        push(counts, 1)
        print("new host: " + from)
    } else {
        counts[at] = counts[at] + 1
    }
    print(from + " -> " + str(msg[1]))
}

Interactive console app

print("pot shell. commands: light, pos, who, set <key> <value>, get <key>, quit")
while true {
    print("? ")
    let line = trim(read())
    let parts = split(line, " ")
    let cmd = lower(parts[0])

    if cmd == "quit" {
        print("bye")
        break
    } else if cmd == "light" {
        print("light: " + str(light()) + "  weather: " + weather())
    } else if cmd == "pos" {
        print(join(pos(), ", "))
    } else if cmd == "who" {
        let here = players(32)
        if len(here) == 0 { print("nobody nearby") } else { print(join(here, ", ")) }
    } else if cmd == "set" and len(parts) > 2 {
        remove(parts, 0)                 # drop "set"
        let key = remove(parts, 0)       # take the key, the rest is the value
        store(key, join(parts, " "))
        print("stored " + key)
    } else if cmd == "get" and len(parts) > 1 {
        print(load(parts[1]) or "(unset)")
    } else {
        print("unknown command")
    }
}

Redstone-triggered alarm

sethost("alarm")
let armed = true
while true {
    if armed and rs_get("down") > 0 {
        say("intruder detected!", 32)
        let i = 0
        while i < 6 {
            beep(24)
            rs_set("up", 15)
            sleep(5)
            rs_set("up", 0)
            beep(0)
            sleep(5)
            i = i + 1
        }
        broadcast(["alarm", pos()])
        sleep(100)
    }
    sleep(2)
}

See also: Errors & Gotchas · Wiki Home

Clone this wiki locally