Skip to content
Docs

The azar service

Reference for the azar service. Random numbers and choices drawn from the match seed, so a match can be replayed exactly.

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

On this page

Random numbers and choices drawn from the match seed, so a match can be replayed exactly.

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

Functions and properties of azar#

azar.numero#

ts
azar.numero(): number   // [0, 1)

A decimal in [0, 1).

azar.entero#

ts
azar.entero(min: number, max: number): number

An integer between min and max, both included.

From docs/EJEMPLOS.md, «azar»:

ts
const cantidad = azar.entero(3, 6);

azar.entre#

ts
azar.entre(min: number, max: number): number

A decimal in [min, max).

From docs/EJEMPLOS.md, «mundo: crear piezas desde el script»:

ts
const moneda = mundo.crear({
  forma: 'cilindro',
  posicion: [azar.entre(-10, 10), 1, azar.entre(-10, 10)],
  rotacion: [90, 0, 0],
  tamano: [0.9, 0.16, 0.9],
  material: { color: 'ambar', acabado: 'brillante' },
  solida: false,
  etiquetas: ['moneda']
});

azar.elegir#

ts
azar.elegir<T>(lista: T[]): T

A random item from the list.

From docs/EJEMPLOS.md, «azar»:

ts
const premio = azar.elegir(['moneda', 'estrella', 'llave']);

azar.mezclar#

ts
azar.mezclar<T>(lista: T[]): T[]

A shuffled copy of the list.

From docs/EJEMPLOS.md, «azar»:

ts
const losas = azar.mezclar(mundo.piezas('losa'));

azar.suerte#

ts
azar.suerte(p: number): boolean

true with probability p (0 to 1).

From docs/EJEMPLOS.md, «azar»:

ts
if (azar.suerte(0.25)) juego.registro('Partida con premio doble:', premio, cantidad);

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.

azar#

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

juego.alIniciar(() => {
  const losas = azar.mezclar(mundo.piezas('losa'));
  for (const trampa of losas.slice(0, 3)) trampa.etiquetar('peligro');     // tres trampas distintas cada partida
  const premio = azar.elegir(['moneda', 'estrella', 'llave']);
  const cantidad = azar.entero(3, 6);
  if (azar.suerte(0.25)) juego.registro('Partida con premio doble:', premio, cantidad);
});

Next#