How do I check if the player wants to close the game?
I’m trying to implement a feature where, when the player attempts to close the game, a confirmation popup appears. I want to use either the popup extension or create a unique in-game popup. However, I can’t find any answers on how to do this, and I don’t have the coding skills to create an extension myself.
I want the game to display a popup asking if I want to save my progress when I try to close it, either by pressing the close button or using ALT+F4.
With ChatGPT, I was able to prevent the game from closing, but I couldn’t get it to show a popup. I’m aware of autosaving, and that’s most likely the solution I’ll go with.
window.addEventListener('beforeunload', function (event) {
// Custom message for the confirmation dialog
const confirmationMessage = 'Are you sure you want to leave the game? Your progress may not be saved.';
// Some browsers may ignore the custom message and show a generic message
event.returnValue = confirmationMessage; // Standard way of triggering the confirmation dialog
return confirmationMessage; // For some browsers that might require it
});
This is the code ChatGPT provided, but since I don’t know JavaScript, I’m not exactly sure what it does. What I do know is that when you try to close the game, it doesn’t let you, and you have to force close it using Task Manager.