GameMaker
AI Summary
A short gist of this page, generated for you.
Powered by AI. Summaries can miss nuance — when in doubt, read the page.
GameMaker's HTML5 export can call JavaScript through an extension. You write a few JavaScript functions that wrap the SDK, then call them from GML like any other function.
#Step 1 — The JavaScript file
Create funox.js:
javascript
var funox_ad_state = 0; // 0 idle, 1 showing, 2 finished
// Loads the SDK and initializes it. Call once when the game starts.
function funox_init() {
var s = document.createElement('script');
s.src = 'https://funox.com/sdk/v1/funox-sdk.js';
s.onload = function () { FunOxSDK.init(); };
document.head.appendChild(s);
return 1;
}
function funox_gameplay_start() {
if (window.FunOxSDK) FunOxSDK.gameplayStart();
return 1;
}
function funox_gameplay_stop() {
if (window.FunOxSDK) FunOxSDK.gameplayStop();
return 1;
}
// Starts an ad break. Pause and mute the game before calling it.
function funox_commercial_break() {
funox_ad_state = 1;
if (!window.FunOxSDK) { funox_ad_state = 2; return 0; }
FunOxSDK.commercialBreak()
.catch(function () {})
.then(function () { funox_ad_state = 2; });
return 1;
}
// Returns 1 once, when the ad break has finished.
function funox_ad_finished() {
if (funox_ad_state === 2) { funox_ad_state = 0; return 1; }
return 0;
}#Step 2 — The extension
- In the Asset Browser, create an Extension and add
funox.jsto it. - Set the file to be used for the HTML5 target.
- Add one function entry for each function in the file, with the same name as its External Name, no arguments, and a double return type:
funox_init,funox_gameplay_start,funox_gameplay_stop,funox_commercial_break,funox_ad_finished.
#Step 3 — Call it from GML
When the game starts, for example in the Create event of a controller object in your first room:
gml
funox_init();When a level starts and ends:
gml
funox_gameplay_start();gml
funox_gameplay_stop();For an ad break, pause your game, start the ad and remember that you are waiting:
gml
audio_pause_all();
global.waiting_for_ad = true;
funox_commercial_break();Then, in a Step event that keeps running while the game is paused:
gml
if (global.waiting_for_ad && funox_ad_finished()) {
global.waiting_for_ad = false;
audio_resume_all();
// resume the game and go to the next level
}#Notes
- Give
inita moment before the first call: the SDK loads in the background, and the wrapper functions do nothing until it is ready. Callingfunox_gameplay_start()when the first level starts, after a menu, is normally late enough. - Add more wrappers the same way for
rewardedBreak()orsaveProgress(). - Export with the HTML5 target, upload the exported folder with
index.htmlat its top level, and check the calls in the portal preview.