Web API
There are two ways you can import this script:
import tinyGames from "/tiny-games/scripts/games/tiny-games.web.mjs";
or, if you're using NPM:
import tinyGames from "tiny-games-web";
gameReady()
Returns whether or not the game has loaded all of the information it needs to (e.g. the currently connected devices & the save data). Your game shouldn't try to interact with Tiny Games while this is false.
if (tinyGames.gameReady()) {
// Start the game
}
Parameters
None
Return Value boolean
Whether the game is ready yet or not.
onGameReady()
Calls the listener when the game has loaded all of the information it needs to. Your game shouldn't try to interact with Tiny Games while this is false.
tinyGames.onGameReady(() => {
// Start the game
});
Parameters
listener () => void - The callback to be executed when the event is fired.
Return Value
None
offGameReady()
Removes a listener from the gameReady event.
const callback = () => {
// Start the game
tinyGames.offGameReady(callback);
};
tinyGames.onGameReady(callback);
Parameters
listener () => void - The listener to be removed.
Return Value
None
WebDevice
class
The WebDevice class is used to list all of the currently connected devices through the web API. It is identical to the WebDevice class in the web API except it also includes the remove() method.
Instance Properties
username string - The username the user entered when connecting.
id string - The unique id of the device.
connected boolean - Whether or not the user is connected to the app. (The connection between their device and the app may sometimes disconnect and reconnect).
latency number - The latency in milliseconds of the connection between the device and the app.
lastPong number - The last time the device responded to a ping from the app.
Instance Methods
None
getDevices()
Returns an array of the currently connected WebDevices (see above).
Parameters
None
Return Value WebDevice[]
All of the currently connected WebDevices.
emitToApp()
Emits a message to the app.
if (tinyGames.gameReady()) {
tinyGames.emitToApp("loaded");
}
Parameters
event string - The event to emit.
...data any[] - Any extra information to send to the device.
Return Value
None