HyperScript
Docs

← All of the Docs The basics

Functions

functionfunction <name>(<locals>):since 1.5.6

Any script

function reward({_who}, {_amount}):
    add {_amount} to {%{_who}%.coins}
    return {%{_who}%.coins}
Try it
functionlocal function <name>(<locals>):since 1.5.6

Only the file it is written in

local function prefix():
    return "&7[&bShop&7] "
Try it
Guide and examples

A function is a block with a name. Write it once, at the left margin like a command, and call it from anywhere:

function reward({_who}, {_amount}):
    add {_amount} to {%{_who}%.coins}
    send "&a+%{_amount}% coins" to {_who}
    return {%{_who}%.coins}

command /daily:
    cooldown: 1 day
    trigger:
        set {_total} to reward(player.name, 50)
        send "&7You now have %{_total}% coins."
FormCan be called from
function <name>(<locals>):Any script
local function <name>(<locals>):Only the file it is written in
  • The values in the brackets arrive in the locals named there. A function with nothing to take is written function refresh(): and called refresh().
  • A parameter may have a default: function reward({_who}, {_amount} = 10): can be called as reward(player.name) or reward(player.name, 50). Parameters with a default come last.
  • Call a function on a line of its own (reward(player.name, 50)) or anywhere a value is expected (set {_total} to reward(player.name, 50), if is_rich(player.name):).
  • return <value> leaves the function and hands the value back; return on its own just leaves. A function that never returns a value gives unset.
  • Inside a function, player and the event values are those of whoever called it. Its locals are its own: it cannot see the caller's locals, and the caller cannot see its.
  • A plain function is shared by every script, in whatever order the files load, so two scripts may not both define a shared function of the same name; the second one is refused with a message naming the first. Use local function for helpers that only one file needs. When both exist, a file's own local function is the one it gets.
  • Function names are letters, digits and _, and may not be a word that starts a statement, such as send or give.
  • A function may call itself. Calls nested more than 64 deep are stopped with an error, so one that never ends cannot freeze the server.
  • A wait inside a function pauses the function only: the caller carries on at once, and gets whatever had been returned by then (normally nothing).