Include Javascript libraries in your GameMaker WebAssembly games!
In my ongoing quest to try and use the GX.Games/Web Assembly target in GameMaker in place of the HTML5 target, I wanted Javascript extensions. And since we don't have that access yet (and likely won't until GMRT), I DIY'd it.
- Requires Node.JS v20 on system PATH (other versions may work)
This does not work when exporting directly to GX.Games.
However, you can export a zip on your local PC by uncommenting line 62 in the server.js file in the wasm-bridge scripts folder - this line copies any game builds to the curent directory. When WASM zip exports are opened up officially in GameMaker, I expect those to work right out of the box.
This extension overrides document.title (formerly window.prompt and then console.log) in the HTML page that the GX.Games target generates with a custom getter and setter, and bundles specific JavaScript files with the game.
Create Javascript files that register functions on the window.wasmgml object and place them in the libraries folder in the projects main directory (this will be created for you on first run). If you're using a build system such as webpack or a transpiler like TypeScript, just ensure the functions are created on the window.wasmgml object and drop the generated JS file in the correct location.
For example, a function designed to expose the Web Share API
/// extensions/wasm_bridge/scripts/libraries/native-share-dialog.js
window.wasmgml.native_share_dialog = async (params) => {
try {
await navigator.share(params);
} catch(e) {
// share API not available
};
}
/// In GameMaker - to call the above function
run_js_function("native_share_dialog",{text: "Shared from a GameMaker WASM game!"});