By default, importingDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/g-js-api/G.js/llms.txt
Use this file to discover all available pages before exploring further.
@g-js-api/g.js injects roughly 50 names — group, trigger_function, counter, on, touch, and many more — directly into the global scope of your JavaScript environment. This is convenient for quick scripts and one-file levels, but it can cause conflicts in larger projects, confuse TypeScript’s type checker, or break tools that lint for undeclared globals. G.js ships an alternative entry point, @g-js-api/g.js/safe, that gives you the exact same API as named exports so you import only what you need.
The Problem with the Default Import
When you write:globalThis object. This means that anywhere in your program, names like unknown_g, particle_system, sepia, and keyframe_system are available without any import statement — but also without any explicit declaration. In a multi-file project or a TypeScript codebase this creates ambiguity about where values come from and makes tree-shaking impossible.
The Solution: Safe Mode
/safe entry point exports everything as named exports. Nothing is written to globalThis. Your bundler, linter, and TypeScript compiler can all see exactly which parts of G.js your file depends on.
What Safe Mode Exports
Every function and constant available in the global mode is also exported by/safe. This includes:
- Core
- Events & Control
- Counters & Items
- Visual & Effects
- Reader & Utils
| Export | Description |
|---|---|
$ | The main G.js object ($.exportConfig, $.add, etc.) |
exportConfig | Configure and trigger level export |
group | Reference a specific group by ID |
color | Reference a specific color channel by ID |
object | Create a GD object |
trigger_function | Create a trigger function group |
unknown_g | Allocate the next free group ID |
unknown_c | Allocate the next free color ID |
unknown_b | Allocate the next free block ID |
level | The current level helper (level.objects, level.get_objects) |
obj_props | Object property name dictionary |
levelstring | Parse a level string into objects |
levelstring_to_obj | Low-level level string → Dictionary[] converter |
extract | SPWN-style extract — puts dict values into scope |
wait | Insert a spawn delay |
range | Create a numeric range |
speed | Level speed constant |
exportConfig is available both as a named export and as $.exportConfig. In safe mode you can use either form — the standalone named export is convenient when you only need it once at the top of your script.TypeScript Support
In global mode, G.js ships asrc/runtime.d.ts ambient declaration file that tells TypeScript about all the names injected into global scope. When you use safe mode, that file is irrelevant — every type comes directly from the /safe module imports, which are fully typed.
This means safe mode is the recommended choice for TypeScript projects: your IDE will provide accurate autocomplete and type checking without needing the ambient declaration file installed.
Complete Safe Mode Script
The following script shows a fully self-contained safe-mode level that moves a group and increments a counter on touch:Using extract() with Safe Mode
extract(dict) is a SPWN-inspired helper that takes a dictionary and injects its values into global scope. This is useful with constant objects — for example, you can extract the easing constants so you can write BOUNCE_OUT instead of constants.BOUNCE_OUT:
When to Use Safe Mode vs Global Mode
Use Safe Mode When…
- Building a TypeScript project where you want accurate type inference
- Working with multiple modules that each import G.js separately
- Following strict ESLint rules that disallow implicit globals
- You want explicit control over which G.js features are used in each file
Use Global Mode When…
- Writing a quick one-file script for a small level
- You’re a beginner and don’t want to think about import statements
- The level logic is entirely in a single file with no module boundaries
- You’re porting a SPWN script and want the most familiar syntax
Installation
Set up G.js in your project with npm.
Quick Start
Build your first level with the global API.
Level Reader
Read and modify existing GD levels from scripts.
Export Modes
Configure savefile, levelstring, live_editor, and gmd exports.