DejiScript reference

DejiScript is the small sandboxed language used by Dejinochi code mods. It looks like GDScript but runs in a host interpreter with no engine, filesystem, network or Steam access.

Not GDScript. Do not use .gd files or Godot APIs. A code mod's entry point is a .sgd file, and only the documented built-ins and the api object are available.

Execution model

  1. Top-level statements run once when the mod loads.
  2. on_init() runs if present.
  3. on_activate() runs after initialization and storage migrations.
  4. Registered callbacks run from events, screen/game ticks and timers.
  5. on_deactivate() runs when the mod is disabled, updated or the game closes.

Top-level var declarations persist for the session and are shared by the mod's functions.

var visits = 0

function on_init()
    api.subscribe("pet.fed", on_fed)
end

function on_fed(payload)
    visits = visits + 1
    if visits >= 3:
        api.notify("That is three meals today!")
    else:
        api.notify("Meal " + str(visits))
    end
end

Syntax at a glance

# Comments start with a hash.
var name = "Kerochi"
var speed = 1.5
var items = ["seed", "water"]
var state = {"seeds": 5, "water": 3}

function greet(who)
    return "Hello, " + who + "!"
end

if state.seeds > 0:
    state.seeds = state.seeds - 1
elif state.water > 0:
    state.water = state.water - 1
else:
    api.log("Nothing to use.")
end

while state.seeds < 5:
    state.seeds = state.seeds + 1
end

for item in items:
    api.log(item)
end

Every block ends with end. Colons after if, elif, else, while, for and function headers are optional.

Values

TypeExamplesNotes
nilnilAbsence of a value.
booleantrue, falseUsed by conditions.
number42, 1.5, -3Integers and floats. Division returns a float.
string"garden", 'seed'Single or double quotes; escapes such as \n.
array[1, 2, 3]Indexed from 0; negative indexes count from the end.
dictionary{"seeds": 5}Member access state.seeds or state["seeds"].
functionfunction tick(dt) ... endCan be passed to registration and event APIs.

Operators

  • Arithmetic: + - * / %
  • Comparison: == != < <= > >=
  • Logic: and, or, not with short-circuiting
  • Assignment: = += -= *= /= %=
  • Access: object.member, container[index], call(args)

+ concatenates strings and arrays and adds numbers. nil, false, 0, empty strings, empty arrays and empty dictionaries are false in conditions.

var plots = [
    {"planted": 0, "boost": 0},
    {"planted": 0, "boost": 0}
]

plots[0].planted = api.now()
var count = len(plots)

for index, plot in plots:
    plot.boost = plot.boost + 10
end

Arrays and dictionaries are reference values. Use api.storage_get when you need a fresh copy of persisted data.

Control flow and functions

function stage_of(plot)
    if plot.planted <= 0:
        return 0
    end
    var elapsed = api.now() - plot.planted + plot.boost
    if elapsed < 20:
        return 1
    elif elapsed < 55:
        return 2
    elif elapsed < 100:
        return 3
    end
    return 4
end
  • break leaves the nearest loop.
  • continue starts the next iteration.
  • pass is an explicit no-op.
  • Recursion is allowed up to the call-depth limit.
  • Functions cannot be overloaded; the latest declaration wins in its scope.

Functions accept positional parameters. Missing arguments become nil; extra arguments are rejected.

Built-in functions

FunctionReturnsNotes
len(value)numberLength of a string, array or dictionary.
str(value)stringPlain-text conversion. Arrays and dictionaries use JSON text.
int(value), float(value), bool(value)converted valueNumeric and boolean conversion.
min(a, b, ...), max(a, b, ...)numberTwo or more numbers.
abs, floor, ceil, round, sqrtnumberMath helpers.
sin, cosnumberRadians. PI is available as a constant.
clamp(value, low, high)numberClamps to an inclusive range.
range(n), range(a, b), range(a, b, step)arrayInteger sequence, capped at 8,192 items.
keys(dict), values(dict)arrayDictionary views.
has(container, key)booleanArray membership, dictionary key or substring test.
type_of(value)stringnil, bool, number, string, array, dict, function or object.
random()numberFloat from 0 to 1.
random_int(a, b), random_float(a, b)numberInclusive random range.
choose(array)valueRandom element from a non-empty array.
substr(text, start, length?)stringSubstring by character position.
lower(text), upper(text)stringCase conversion.
contains(text, part)booleanSubstring test.
print(...)nilWrites to the Godot output with the mod ID.

Strings and arrays intentionally have no methods. Use the built-ins above, indexing and plain assignments.

Limits

LimitValue
Evaluation steps per callback250,000
Wall-clock time per callback40 ms
Call depth48
Loop iterations per loop100,000
Array or dictionary items8,192
String length16,384 characters
Draw commands per frame2,000
Text per draw command512 characters

A parse error, denied capability, invalid API argument or exceeded limit records an error and disables only that mod. The game and other mods keep running.

Restrictions

  • No classes, modules, imports, coroutines, await or exceptions.
  • No engine globals, scene tree, nodes, OS, files, network or Steam.
  • No arbitrary native code, Godot scenes or resource packs.
  • No methods on strings or arrays.
  • No unbounded recursion, loops, collections or output.

Keep code organised with top-level variables, functions, arrays and dictionaries. For the host API, continue to Code mods and the Mod API. When the mod is ready, follow Publish to Workshop.