Counters are one of G.js’s most powerful abstractions. They wrap Geometry Dash’s item system — a set of integer variables that the game engine tracks at runtime — and give you a JavaScript-friendly API for reading, writing, and reacting to those values. Every arithmetic operation, comparison, and loop you perform with a counter compiles down to GD’s item triggers, so the logic runs entirely inside the game with no external JavaScript needed at play-time.Documentation 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.
Creating a Counter
Use thecounter() function to allocate a new in-game item variable:
Initial value of the counter. Booleans are converted to
1 or 0.When
true, treats num as an existing item ID to reuse rather than allocating a fresh one.When
true, the counter retains its value across attempts (deaths and restarts).When
true, the counter behaves as a GD timer rather than a plain item.Specifies the bit-width of the counter. Useful for memory-efficient item storage.
Arithmetic Methods
EveryCounter exposes chainable methods that emit the appropriate item-edit triggers when called inside a trigger function context.
| Method | Description |
|---|---|
add(amount) | Adds amount to the counter. |
subtract(amount) | Subtracts amount from the counter. |
multiply(amount) | Multiplies the counter by amount. |
divide(amount) | Divides the counter by amount. |
set(amount) | Sets the counter to an exact value. |
amount can be a plain number or another Counter, letting you compose item-edit chains.
Additional Arithmetic
| Method | Description |
|---|---|
abs() | Replaces the counter value with its absolute value. |
neg() | Negates the counter value. |
mod(b) | Computes counter mod b (number or Counter). |
reset() | Resets the counter to zero. |
Copying and Cloning
| Method | Description |
|---|---|
add_to(item) | Adds this counter’s value into item. |
copy_to(item) | Copies (overwrites) this counter’s value into item. |
clone() | Returns a new counter that is a copy of this one. |
subtract_from(b) | Subtracts this counter from counter b. |
Display and Object Conversion
Places a GD item-display object at the given coordinates. The display updates in real-time as the counter changes.
Returns the underlying
GJsObject so you can attach additional properties with .with() and then .add() it.Conditional Branching with if_is
if_is fires a trigger function whenever the counter satisfies a comparison against a constant:
One of the three comparison constants:
EQUAL_TO (0), LARGER_THAN (1), or SMALLER_THAN (2).The constant to compare against.
The trigger function to call when the condition is true.
Comparison Constants
| Constant | Value | Meaning |
|---|---|---|
EQUAL_TO | 0 | Counter equals other |
LARGER_THAN | 1 | Counter is greater than other |
SMALLER_THAN | 2 | Counter is less than other |
SPWN-style Constant Ranges with to_const
to_const maps a range of possible counter values to JavaScript-time code, letting you generate different trigger logic for each value:
Floating-Point Counters
For fractional values, usefloat_counter:
FloatCounter extends Counter with three additional rounding methods:
| Method | Description |
|---|---|
round() | Rounds the value to the nearest integer. |
floor() | Floors the value to the largest integer ≤ value. |
ceil() | Ceils the value to the smallest integer ≥ value. |
float_counter accepts the same val, use_id, and persistent parameters as counter() but does not expose the is_timer or bits options.Timers
Thetimer() function creates a special counter that counts time in seconds rather than arbitrary units:
The value (in seconds) at which the timer starts.
The value at which the timer stops (if
stop is true).Group to call when the timer reaches
end_seconds.When
true, the timer counts down instead of up.When
true, only whole seconds are counted.When
true, the timer halts at end_seconds.Multiplier applied to the timer’s tick rate. Cannot be used together with
backwards.When
true, the timer is immune to timewarp triggers.When
true, another timer trigger cannot override this timer.Counter also exposes start(), stop(), set_start(x), and set_end(x) for controlling the timer at runtime.
While Loops
G.js supports runtime while-loops using condition helpers and thewhile_loop function from the control-flow module.
Condition Helpers
These functions build a condition descriptor thatwhile_loop understands:
{ count, comparison, other }.
while_loop(r, triggerFunction, del?)
Keeps spawning triggerFunction as long as the condition r holds:
A condition object returned by
less_than, equal_to, or greater_than.The trigger function to repeat while the condition is true.
Optional delay (seconds) between loop iterations.
Item Compare with compare
For comparing two counters against each other (rather than a counter against a constant), use compare:
First counter to compare.
Comparison operator. Use constants from
constants.d.ts: EQ (0), GREATER (1), GREATER_OR_EQ (2), LESS (3), LESS_OR_EQ (4), NOT_EQ (5).Second counter to compare.
Group to call if the comparison is true.
Group to call if the comparison is false.
Full Examples
Basic Counter Arithmetic
While Loop Countdown
if_is Branching
to_const Range Dispatch
Control Flow
Learn
while_loop, for_loop, and frame_loop for repeating trigger sequences.Events
Combine counters with event triggers for reactive level logic.
Items API
Low-level
item_edit and item_comp trigger wrappers.Constants
Full list of comparison and operator constants.