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#
| Property | Type | Description |
|---|---|---|
juego.id | string | Id of the game, the one in the manifest. |
juego.dificultad | string | Id of the chosen difficulty, or '' if the game has no difficulties. |
juego.nivelDificultad | number | Index 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.semilla | number | Seed of the match. With the same seed, azar gives the same sequence. |
juego.escena | string | Id of the loaded scene. |
juego.terminado | boolean | true once juego.terminar has been called. |
juego.alIniciar#
juego.alIniciar(fn: () => void): voidCalled 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»:
juego.alIniciar(() => {
juego.registro('Partida en', juego.dificultad, 'con semilla', juego.semilla);
});juego.cadaCuadro#
juego.cadaCuadro(fn: (dt: number) => void): voidCalled on every simulation frame. dt is always 1/60.
From docs/EJEMPLOS.md, «mundo: mover piezas»:
// 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#
juego.terminar(fin: { resultado: 'victoria' | 'derrota' | 'fin'; puntaje?: number; tabla?: string; de?: Jugador; detalle?: Texto }): voidEnds the match. After this the room stops: no more frames or timers run.
From docs/EJEMPLOS.md, «juego: cambiar de escena»:
mundo.alEntrar('tesoro', (_zona, j) => juego.terminar({ resultado: 'victoria', puntaje: 1, de: j }));juego.cambiarEscena#
juego.cambiarEscena(id: string): voidUnloads 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»:
// Escena "principal" con una zona "puerta"; escena "sotano" con una zona "tesoro".
mundo.alEntrar('puerta', () => juego.cambiarEscena('sotano'));juego.alCambiarEscena#
juego.alCambiarEscena(fn: (id: string) => void): voidCalled after every juego.cambiarEscena, with the id of the new scene.
From docs/EJEMPLOS.md, «juego: cambiar de escena»:
juego.alCambiarEscena((id) => juego.registro('Ahora en', id));juego.registro#
juego.registro(...datos: unknown[]): void // `greengames probar` shows itWrites to the player console (for debugging only; the person playing does not see it).
From docs/EJEMPLOS.md, «juego: iniciar, cada cuadro, terminar»:
juego.registro('Partida en', juego.dificultad, 'con semilla', juego.semilla);juego.senalar#
juego.senalar(nombre: string, valor?: unknown): voidLeaves 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»:
juego.senalar('puerta-abierta'); // cuando se abreBehavior 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:
| Kept | Lost |
|---|---|
Every registered event (alTocar, alEntrar, cadaCuadro, alAccion...): selector events are applied again to the matching pieces and zones of the new scene | The pieces, zones, lights, actors and labels of the previous scene, including those created by script |
Timers (reloj.despues, reloj.cada) and HUD timers | Events registered with pieza.alTocar(fn) (they go with the piece) |
| HUD, scoreboard, open menus and sounds | Groups (they belong to the pieces) |
Players: datos, puntos, rapidez, salto, progress | Each player's aparicion (it becomes the first one of the new scene) |
Signals from juego.senalar and camera settings | The 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#
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#
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#
- The mundo service
- API reference: the eleven services and the common types.
- Scripts: the rules of a script and how it runs inside the room.