Aller au contenu

Using TypeScript

Ce contenu n’est pas encore disponible dans votre langue.

GDMP runs JavaScript, but you can write your scripts in TypeScript, or keep JavaScript and let your editor check it. Either way you install the GDMP types: every name of the gdmp global, with its arguments, what it returns and the shape of every event. Your editor then completes gdmp.players. for you, shows the docs of each call as you type, and underlines a mistake (a typo, a missing argument, a call the server doesn’t have) before you ever start the game.

The types are open source: GeometryDashMultiplayer/types. Every page of the API reference ends with its part of them.

A server script, a Play client script and a Build script each get a different gdmp (see where a script runs): gdmp.player exists only on a client, gdmp.nametags only on the server. So you pick the types for the folder you’re in:

Script Types Only there
scripts/server/main.js gdmp-types/server players.respawn / teleport / freeze…, nametags, economy (full), data, state.set
scripts/client/main.js gdmp-types/client player, input, ui, web, sprites, voice, console
scripts/build/main.js gdmp-types/build world / level editing, build.save()
code that runs on several gdmp-types/shared only what every host has

Call a name the host doesn’t have and the editor says so:

gdmp.player.respawn(); // in a server script
~~~~~~
Property 'player' does not exist on type 'ServerApi'. Did you mean 'players'?

No build step: your package stays exactly as it is.

  1. Install the types in your package folder (you need Node.js):

    Terminal window
    npm install --save-dev github:GeometryDashMultiplayer/types
  2. Put these two lines at the top of each script, with that script’s host:

    scripts/client/main.js
    // @ts-check
    /// <reference types="gdmp-types/client" />
  3. Open the folder in VS Code (or any editor with TypeScript support). Hover a call to read its docs; mistakes are underlined in red.

Write .ts files in a src/ folder and build them into the scripts/ files GDMP runs. With TypeScript you can also split your code into several files with import: the build joins them into the one file each host loads.

  1. Install TypeScript, esbuild (the bundler) and the types:

    Terminal window
    npm install --save-dev typescript esbuild github:GeometryDashMultiplayer/types
  2. Lay out src/ like scripts/, with one tsconfig.json per host:

    my-experience/
    manifest.json
    package.json
    src/
    tsconfig.base.json
    server/ main.ts tsconfig.json
    client/ main.ts tsconfig.json
    shared/ rules.ts <- imported by both
    scripts/ <- built, what GDMP runs
    server/main.js
    client/main.js
    {
    "compilerOptions": {
    "strict": true, "target": "ES2022", "lib": ["ES2022"], "types": [], "noEmit": true,
    "module": "ESNext", "moduleResolution": "Bundler", "isolatedModules": true
    }
    }

    "lib": ["ES2022"] with no DOM: GDMP scripts have no window, fetch, console or require, so the editor flags them too. (Log with gdmp.log.)

  3. Add the scripts to package.json:

    package.json
    {
    "scripts": {
    "check": "tsc -p src/server && tsc -p src/client",
    "build": "npm run check && esbuild src/server/main.ts --bundle --format=iife --target=es2022 --outfile=scripts/server/main.js && esbuild src/client/main.ts --bundle --format=iife --target=es2022 --outfile=scripts/client/main.js"
    }
    }
  4. Write the code:

    export const ROUND_MS = 60_000;
    export interface Score { name: string; points: number }
    export const label = (s: Score): string => `${s.name}: ${s.points}`;
  5. Build, then test in GDMP as usual (the launcher’s Test, or the Build menu):

    Terminal window
    npm run build

    The build stops at the first type error, so a broken script never reaches the game.

Your .ts files in src/ are published with the package, like the .js files GDMP runs (players’ games download both). If you’d rather keep them to yourself, put the TypeScript project one folder up and point the build’s --outfile into the package.

  • What you receive is any. A message’s data and a state value are JSON your own scripts sent, so the types can’t know their shape. Say it once: const msg = e.data as BuyRequest;. What you send must be JSON (no functions, no Map), and the types check that.
  • Stores can be typed. gdmp.data.getStore<Save>('players') makes get, set and update use your Save type.
  • Values are read-only. A player, a zone event or a level object is a frozen copy: the types mark every field readonly, so changing one is an error in the editor instead of a TypeError in the game.
  • Capabilities. The types show every namespace the host can have. If your manifest doesn’t declare a capability, that namespace is undefined when the script runs (capabilities).
  • API levels. The types follow the newest API level. A call marked Level 92 in its docs needs that level: if (Number(gdmp.version) >= 92) ... if your experience must also run on older games.

The types repo has a checker that type-checks every script of a package (or a folder of packages) against its host, without changing anything:

Terminal window
npm install --save-dev typescript
node node_modules/gdmp-types/tools/check-experiences.mjs .