Basic Syntax
GScript uses JavaScript syntax, so if you know JavaScript you already know GScript. Quick reference:
// 1. Variables
// Use 'let' for variables that change, and 'const' for fixed values.
let count = 0;
const maxRetries = 5;
let name = "Player";
let isActive = true;
// 2. Data Structures
// Arrays (lists of items)
let collection = [1, 2, 4, 5];
collection.push(7); // [1, 2, 4, 5, 7]
let thirdItem = collection[2]; // get (returns 4)
collection[3] = 10; // set (changes 5 to 10)
// Objects (key-value pairs)
let player = {
name: "Hero",
level: 5
};
let pName = player.name; // get
player.level = 6; // set
// 3. Branching
if (count < maxRetries) {
// Do something
} else if (count === maxRetries) {
// Do something else
} else {
// Give up
}
// Ternary operator for quick assignments
let status = (count < maxRetries) ? "Running" : "Stopped";
// 4. Loops
// - for loop
for (let i = 0; i < collection.length; i++) {
let current = collection[i];
console.log(current);
}
// - while loop
let j = 0;
while (j < 10) {
console.log(j);
j++;
}
// - for...of loop
let array = [1, 2, 3, 4, 5];
for (let item of array) {
// do something to every item
}
// 5. Functions
function add(a, b) {
return a + b;
}
add(1, 1); // 2
// Arrow functions (shorter syntax)
const multiply = (a, b) => a * b;
// Functions are first-class, you can pass them as arguments
let items = [1, 2, 3];
let doubled = items.map(item => item * 2);
// 6. Classes
class Calculator {
// Constructor runs when creating a new instance
constructor(a, b) {
this.a = a;
this.b = b;
}
// Methods
add() {
return this.a + this.b;
}
// Static methods (called directly on the class)
static subtract(a, b) {
return a - b;
}
}
let calc = new Calculator(2, 3);
calc.add(); // 5
Calculator.subtract(5, 2); // 3Important Tips
- Variables you create in the macro editor (
getVar,setVar) are almost always strings. Remember to convert them to numbers before doing math. - End your statements with semicolons (
;). It prevents unexpected bugs. - Use
===instead of==for strict comparisons (e.g., checking if something is exactlyfalse).
What is not available
GScript runs on your phone, not in a browser or Node, so some familiar functions are missing. Each has a GScript equivalent:
| Not available | Use instead |
|---|---|
setTimeout, setInterval | wait(ms) to pause, or a loop that ends in wait(ms) to repeat. There are no timers. |
fetch, XMLHttpRequest | Http.get(url), Http.post(url, body), Http.request(url, opts) |
window, document, the DOM | Screen.* to read the screen, Ui.Overlay.* to draw on it |
localStorage, sessionStorage | Cache.set/get for this run; storeVar or Env.set to persist |
alert, prompt, confirm | toast(message), or a settings dialog to ask up front |
require, import | Nothing to import; every GScript API is already global |
If you use one of these by mistake, the error message tells you which replacement to reach for.