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
- JavaScript
- TypeScript
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!");
});
script.ts
// ... 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: (): void => void coins.gain(),
},
{
id: "buyUpgrades",
name: "Buy Upgrades",
key: "b",
onDownContinuous: (): void => void 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
initmethod is called to initialize the game. This method should be called after all currencies and upgrades are added. TheloadDatamethod is called to load the game data. For more information, see the DataManager documentation. - The
keyManagerobject is used to add hotkeys to the game. TheaddKeymethod 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
dataManagerobject is used to save and load data. ThesaveDatamethod is used to save the game data, and theloadDatamethod is used to load the game data. For more information, see the DataManager documentation. - The
autoSaveevent 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