Customize HTML5 Engine Games With Code?
There is limited interaction a user can have with the HTML5 engine. This is and advanced feature, so our support team
may not be able to help you quickly. Before using these features or reaching out to our support team, please make sure
you have an intermediate to advanced understanding of javascript and how it works in the browser.
The most comprehensive example of how to uses these types of features lives in the sample-legendsoflearning.html file
included with your HTML5 package.
Change Engine State
The following functions are available:
gse.pauseGame(); // Pauses the Game
gse.playGame(); // Resumes the Game
gse.resetGame(); // Resets the Game
gse.getGameVolume(); // Gets the engine volume
You can also write functionality to control the volume of the game inside the gse.ready callback. Inside that callback
you can call the following two functions:
gse.setGameVolume(engine, volume[0-1]); // Sets the volume for the whole game. Must be called where the engine context
is available. This can be hooked up in the gse.ready callbackback function. This must happen inside the gse.ready
callback, because you can only set the volume after the engine has initialized.
Handle Engine Events via Delegates
On the sample-index.html file provide, inside the gse.ready callback function, you will see a playerDelegate object
which has 3 delegate functions defined. These two actually trigger on engine events:
Life Cycle Delegates
onLoadingBegin: function() // define what you want to happen while the engine is loading a scene
onLoadingEnd: function() // define what you want to happen when the engine is done loading a scene
onCurrentSceneChanged: function(sceneKey, scenename) // define what extra actions to take when the scene changes. good
for analytics or ads
onSceneAboutToChange: function (sceneKey, sceneName, adType) // lets you know before a scene changes. good for analytics
or ads
onEndGame: function() // Called when the end game behavior is triggered.
External Behavior Delegates
onShowBannerShow: function(position[‘top’|’bottom’]) // Triggered by the Show Banner Ad Action. Use this to trigger code
to show ads.
onTweetSheet: function(message, image) // Triggered by Tweet Sheet Action. One of the most flexible delegates, can be
used to trigger almost anything based on the values of the message and image fields.
onLogDebuggingStatement: function (text, entity) // Called when log Debug Statement action is triggered.
IAP Delegates
onIAPBuyItem: function (itemInfo) // itemInfo: {itemID, consumable, name, price, state}. Return a promise that rejects
if the purchase fails and resolves with the following json object: {buyerCancelled: (true || false), purchaseComplete:
(true || false)}.
onIAPRestoreItems: function () // Returns a promise. Resolves with purchases object. Purchases is an array of objects
containing the key itemID for all items purchased.
onIAPConsumeItem: function (itemInfo) // itemInfo: {itemID, consumable, name, price, state}. Return a promise that
rejects if the purchase fails and resolves if the purchase is a success.
onIAPRequestPurchaseData() // Promise that resolves successful with itemInfo (see onIAPBuyItem) or rejects
unsuccessfully.
Game Service Delegates
onGameCenterLogin: function () // Called when Game Service Login is triggered. Return a promise. If resolve is called,
Platform Connected will be set to true.
onGameCenterPostScore: function (score, leaderboard) // No promise, just do what you will with the score and leaderboard
name.
onGameCenterShowAchievements: function () // Called when the Show Achievements action is triggered.
onGameCenterResetAchievements: function () // Called when the Reset Achievements action is triggered.
onGameCenterUpdateAchievement: function (identifier, percentageComplete) // Called when Update Achievement is called.
onGameCenterShowLeaderboard: function (identifier) // Called when Show Leaderboard action is triggered.
Data Loading Delegates
onSaveTable: function(key, table, defaultFunction) { // Intercept save table events. If your delegate returns anything
but undefined, the default table saving behavior will be skipped. You can use this to do things in addition to saving
table data to local storage, or you can override storing the table to store the data elsewhere like a server.
onSaveAttribute: function(key, value, defaultFunction) { // Intercept save attribute events. If your delegate returns
anything but undefined, the default attribute saving behavior will be skipped. You can use this to do things in addition
to saving attributes to local storage, or you can override storing the attribute to store the data elsewhere like a
server.
onLoadAttribute: function(key, defaultFunction) { // Intercept load attribute events. If your delegate returns anything
but undefined, the default attribute saving behavior will be skipped. You can use this to do things in addition to
saving attributes to local storage, or you can override storing the attribute to store the data elsewhere like a server.
Posting Events to Engine
You can also post events to the engine from inside the delegates. This will let you change attributes which can trigger
events in your game logic. You will need to define this inside the gse.ready function in your index page, so you have
access to the engine object. These require a bit more advanced knowledge about the internals of how some things work in
the engine, so please consult a knowledgable member of our forums or our support team before using them.
To post an event you will call postEvent on the engine object.
engine.postEvent(event, null, param1, param)
Events Include:
localeDetected – param1 is the location code, param2 is the language code. Use this if you are using something other
than browser information to detect a users locale information (like a call to an external API or an event from another
javascript library).
giveAdReward – param1 reward name, param2 reward value. Use this to update ad rewards from the results of your ad
display in onCurrentSceneChanged or onShowBannerShow
externalWriteGameAttribute – param1: attribute path, param2 value. Use this to update an attribute. You will need to
provide the full attribute path so something like ‘game.attributes.name’ or the actual internal attribute Id for custom
attributes like ‘game.attributes.id12345’. For the value you can provide a simple value like a string or number. You can
also update a table (assuming your attribute is a table) by providing the complete table JSON structure (the same
structure sent and received via the network behaviors).
loadExternalImage – param1 imageId, param2 url. You can use this to replace an existing image in your game project with
an image loaded from a URL. The imageId is the name of your image in your project. The URL is the url for the new image.
If the image is already on display, call this will replace the image once it has finished loading from the network. If
the URL is unable to load an image, the original image from the game project will continue to display.