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
- Top-level statements run once when the mod loads.
on_init()runs if present.on_activate()runs after initialization and storage migrations.- Registered callbacks run from events, screen/game ticks and timers.
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
| Type | Examples | Notes |
|---|---|---|
| nil | nil | Absence of a value. |
| boolean | true, false | Used by conditions. |
| number | 42, 1.5, -3 | Integers 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"]. |
| function | function tick(dt) ... end | Can be passed to registration and event APIs. |
Operators
- Arithmetic:
+ - * / % - Comparison:
== != < <= > >= - Logic:
and,or,notwith 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
breakleaves the nearest loop.continuestarts the next iteration.passis 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
| Function | Returns | Notes |
|---|---|---|
len(value) | number | Length of a string, array or dictionary. |
str(value) | string | Plain-text conversion. Arrays and dictionaries use JSON text. |
int(value), float(value), bool(value) | converted value | Numeric and boolean conversion. |
min(a, b, ...), max(a, b, ...) | number | Two or more numbers. |
abs, floor, ceil, round, sqrt | number | Math helpers. |
sin, cos | number | Radians. PI is available as a constant. |
clamp(value, low, high) | number | Clamps to an inclusive range. |
range(n), range(a, b), range(a, b, step) | array | Integer sequence, capped at 8,192 items. |
keys(dict), values(dict) | array | Dictionary views. |
has(container, key) | boolean | Array membership, dictionary key or substring test. |
type_of(value) | string | nil, bool, number, string, array, dict, function or object. |
random() | number | Float from 0 to 1. |
random_int(a, b), random_float(a, b) | number | Inclusive random range. |
choose(array) | value | Random element from a non-empty array. |
substr(text, start, length?) | string | Substring by character position. |
lower(text), upper(text) | string | Case conversion. |
contains(text, part) | boolean | Substring test. |
print(...) | nil | Writes 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
| Limit | Value |
|---|---|
| Evaluation steps per callback | 250,000 |
| Wall-clock time per callback | 40 ms |
| Call depth | 48 |
| Loop iterations per loop | 100,000 |
| Array or dictionary items | 8,192 |
| String length | 16,384 characters |
| Draw commands per frame | 2,000 |
| Text per draw command | 512 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,
awaitor 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.
