Skip to content
Docs

The juego service

Reference for the juego service. The match: its difficulty and scene, when it starts, what happens every frame and how it ends.

DescribesFormato 1, API 1 For those who write code This page is generated from the SDK sources.

On this page

The match: its difficulty and scene, when it starts, what happens every frame and how it ends.

The types used in the signatures (Vec3, Texto, the selector) are in the API reference.

Functions and properties of juego#

PropertyTypeDescription
juego.idstringId of the game, the one in the manifest.
juego.dificultadstringId of the chosen difficulty, or '' if the game has no difficulties.
juego.nivelDificultadnumberIndex of the chosen difficulty within dificultades (0 is the first). -1 if there are none.
juego.modo'solo' | 'sala-local' | 'sala''solo': one player. 'sala-local': one player plus bots on this device. 'sala' is reserved and not used today.
juego.semillanumberSeed of the match. With the same seed, azar gives the same sequence.
juego.escenastringId of the loaded scene.
juego.terminadobooleantrue once juego.terminar has been called.

juego.alIniciar#

ts
juego.alIniciar(fn: () => void): void

Called once, with the scene loaded and the initial players already in. Register the rest of your events here.

From docs/EJEMPLOS.md, «juego: iniciar, cada cuadro, terminar»:

ts
juego.alIniciar(() => {
  juego.registro('Partida en', juego.dificultad, 'con semilla', juego.semilla);
});

juego.cadaCuadro#

ts
juego.cadaCuadro(fn: (dt: number) => void): void

Called on every simulation frame. dt is always 1/60.

From docs/EJEMPLOS.md, «mundo: mover piezas»:

ts
// A mano, cuadro a cuadro: una pieza en órbita. Se asigna un vector nuevo.
juego.cadaCuadro(() => {
  const a = reloj.ahora * 2;
  mundo.pieza('satelite').posicion = [Math.cos(a) * 5, 3, Math.sin(a) * 5];
});

juego.terminar#

ts
juego.terminar(fin: { resultado: 'victoria' | 'derrota' | 'fin'; puntaje?: number; tabla?: string; de?: Jugador; detalle?: Texto }): void

Ends the match. After this the room stops: no more frames or timers run.

From docs/EJEMPLOS.md, «juego: cambiar de escena»:

ts
mundo.alEntrar('tesoro', (_zona, j) => juego.terminar({ resultado: 'victoria', puntaje: 1, de: j }));

juego.cambiarEscena#

ts
juego.cambiarEscena(id: string): void

Unloads the current scene and loads another. Players respawn at the first spawn point. Calls the alCambiarEscena handlers again.

From docs/EJEMPLOS.md, «juego: cambiar de escena»:

ts
// Escena "principal" con una zona "puerta"; escena "sotano" con una zona "tesoro".
mundo.alEntrar('puerta', () => juego.cambiarEscena('sotano'));

juego.alCambiarEscena#

ts
juego.alCambiarEscena(fn: (id: string) => void): void

Called after every juego.cambiarEscena, with the id of the new scene.

From docs/EJEMPLOS.md, «juego: cambiar de escena»:

ts
juego.alCambiarEscena((id) => juego.registro('Ahora en', id));

juego.registro#

ts
juego.registro(...datos: unknown[]): void   // `greengames probar` shows it

Writes to the player console (for debugging only; the person playing does not see it).

From docs/EJEMPLOS.md, «juego: iniciar, cada cuadro, terminar»:

ts
juego.registro('Partida en', juego.dificultad, 'con semilla', juego.semilla);

juego.senalar#

ts
juego.senalar(nombre: string, valor?: unknown): void

Leaves a named signal for the bot script (conditions { "senal": "name" }). It does nothing else: the player does not see it. It lets the test know where the game is ('ronda', 'puerta-abierta'). valor must be JSON; it defaults to true.

From docs/EJEMPLOS.md, «Guion de bot con señales, ciclos y ruta»:

ts
juego.senalar('puerta-abierta');      // cuando se abre

Behavior notes#

terminar with tabla records the puntaje in that table for the player de (by default, the first human). After terminar the room stops. cambiarEscena unloads the scene, loads the other one on the next frame, takes the players to its first spawn point and calls alCambiarEscena.

What survives cambiarEscena:

KeptLost
Every registered event (alTocar, alEntrar, cadaCuadro, alAccion...): selector events are applied again to the matching pieces and zones of the new sceneThe pieces, zones, lights, actors and labels of the previous scene, including those created by script
Timers (reloj.despues, reloj.cada) and HUD timersEvents registered with pieza.alTocar(fn) (they go with the piece)
HUD, scoreboard, open menus and soundsGroups (they belong to the pieces)
Players: datos, puntos, rapidez, salto, progressEach player's aparicion (it becomes the first one of the new scene)
Signals from juego.senalar and camera settingsThe environment (the new scene's is used)

A defeat does not enter the table: terminar({ resultado: 'derrota', tabla }) neither records the score nor announces a record. To record it anyway (a survival game that always ends in defeat), use resultado: 'fin' or call puntajes.registrar first.

Complete examples#

Complete scripts from docs/EJEMPLOS.md. Each one assumes a scene containing what it names, and the text keys (interfaz.t('...')) must exist in textos of juego.json.

juego: start, every frame, end#

scripts/principal.ts
import { juego, jugadores } from '@greengames/sdk';

juego.alIniciar(() => {
  juego.registro('Partida en', juego.dificultad, 'con semilla', juego.semilla);
});

// Gana quien llegue a z = 50. dt siempre vale 1/60.
juego.cadaCuadro(() => {
  for (const j of jugadores.todos()) {
    if (j.posicion[2] >= 50) juego.terminar({ resultado: 'victoria', puntaje: 100, de: j });
  }
});

juego: changing scenes#

scripts/principal.ts
import { juego, mundo } from '@greengames/sdk';

juego.alIniciar(() => {
  // Escena "principal" con una zona "puerta"; escena "sotano" con una zona "tesoro".
  mundo.alEntrar('puerta', () => juego.cambiarEscena('sotano'));
  mundo.alEntrar('tesoro', (_zona, j) => juego.terminar({ resultado: 'victoria', puntaje: 1, de: j }));
});
juego.alCambiarEscena((id) => juego.registro('Ahora en', id));

Next#