Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

4.4 KB
Raw Blame

estella-plugin-minigame-services

The mini-game host's share sheet, in-game purchase, friends leaderboard and screen recorder, as Estella services. The first three used to ship inside the engine; they live here because most games open none of them, and because a service is exactly the shape a plugin should be.

Ships with the Estella editor — import it; play and every export resolve it to the editor's copy. A copy the project installs into its own node_modules takes precedence.

import { addPlugin } from 'esengine';
import { miniGameServicesPlugin, Share, Payment, Leaderboard, Recorder } from 'estella-plugin-minigame-services';

addPlugin(miniGameServicesPlugin);

Share

const share = app.getResource(Share);

// Hide your share button where the host has no sheet (web and native).
if (share.available) showShareButton();

// The card is asked for at SHARE time, so it can carry live state.
share.setShareCard(() => ({ title: `I scored ${score}` }));
share.share();

Setting the card also answers the host's OWN share menu — the passive surface a player reaches without touching your button.

Payment

const pay = app.getResource(Payment);

// Answers for the DEVICE, not for the API: on WeChat, buying is Android-only.
// A shop reads this to stay closed rather than to open and fail.
if (!pay.available) return;

await pay.request({ quantity: 10 });
// Resolved means "the host says it completed" — go ASK your server what the
// player owns. A purchase the client believes in is one an attacker can claim.

Failures reject with the host's own message and code, untranslated: vendors number them differently, and a mapping invented here would be a guess your game then branches on.

Leaderboard

A friends board has two halves in two JS runtimes, and this package is both.

The game's half asks for the board and wears its pixels:

const board = app.getResource(Leaderboard);

// Hide the button where the host has no open data context (web, native).
if (board.available) showLeaderboardButton();

board.submit(score);            // your own cloud row — the one write allowed here
board.show({ limit: 10 });      // a REQUEST to draw; no rows ever come back
uiVisual.texture = board.texture; // what it drew, as an engine texture handle
board.hide();

The context's half runs in the second runtime, which has no engine, no WebGL and no DOM. The project owns that file; this package supplies what goes in it:

import 'estella-plugin-minigame-services/open-data';

That directory is what the exporter bundles separately and names in game.json. Without it the host has no context, and available says so rather than a board failing on a device.

In the editor's play mode there is no host, so the editor stands in for one: it runs that same file against an offscreen canvas and invented friends. So the board you look at while building the panel is the one that ships, and nothing here needs a rehearsal mode of its own.

Recorder

const recorder = app.getResource(Recorder);

if (recorder.available) await recorder.start();   // at the start of a run
recorder.highlight(3, 2);                          // keep 3s before, 2s after
await recorder.stop();                             // at the end

// From the player's tap — both hosts refuse a share nobody asked for.
if (recorder.canShare) await recorder.share({ title: 'Look at this run' });

With highlights the shared clip is the highlights; without, the whole recording. WeChat keeps the video and shares it itself (at most 60s of highlights), Douyin hands over a temp file and cuts the highlights on its side, and a browser — the editor's play mode — records a WebM preview it cannot share, so canShare is false there.

Built on the public API

Only esengine's public surface: defineResource, the Plugin interface, createCanvasTexture, and the host capability functions (platformShare, platformCanShare, platformOnShareRequest, platformCanPay, platformRequestPayment, platformCanOpenData, platformOpenDataPostMessage, platformOpenDataCanvas, platformSetCloudKeyValues, platformCreateCanvas, platformDevicePixelRatio, platformScreenRecorder). Declare esengine a peer dependency if you write one of these — a copy vendored inside the package would be a second engine whose resources nothing can read.

Apache-2.0.