Skip to main content

Advanced Features


After you have set up display, you can optionally add advanced features to your game.

This tutorial will cover the following advanced features:

  • Hotkeys
  • Save and Load
script.js
// ... Previous code

// Data loading

// Initialize / Load game
// Only do this after all currencies and upgrades are added
coinGame.init();
coinGame.dataManager.loadData();

// Hotkeys
coinGame.keyManager.addKey([
{
id: "gainCoins",
name: "Gain Coins",
key: "g",
onDownContinuous: () => coins.gain(),
},
{
id: "buyUpgrades",
name: "Buy Upgrades",
key: "b",
onDownContinuous: () => coins.buyUpgrade("upg1Coins"),
},
]);

// Saving and Loading
window.addEventListener("beforeunload", () => {
coinGame.dataManager.saveData();
});
coinGame.eventManager.setEvent("autoSave", "interval", 30000, () => {
coinGame.dataManager.saveData();
console.log("Auto Saved!");
});

Explanation:

  • The init method is called to initialize the game. This method should be called after all currencies and upgrades are added. The loadData method is called to load the game data. For more information, see the DataManager documentation.
  • The keyManager object is used to add hotkeys to the game. The addKey method is used to add a key with an ID, name, key, and function to execute when the key is pressed. For more information, see the KeyManager documentation.
  • The dataManager object is used to save and load data. The saveData method is used to save the game data, and the loadData method is used to load the game data. For more information, see the DataManager documentation.
  • The autoSave event is set to save the game data every 30 seconds. This is useful for automatically saving the game data at regular intervals. For more information, see the EventManager documentation.

You are now done with the coinGame tutorial! Final code: coinGame Repo