How do I check if the player wants to close the game?

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.

Never seen any means to stop/detect game being closed

MAYBE there exist some with JS yet again i never heard of any

And i seen all ppl making auto saves with some time intervals like 1 or 2 or 3 or more mins as a workaround

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.

Really?
There is a way?
How to do it?

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.

1 Like

Thx
I needed it just for ppl that would ask same question as you
Personally i wonder does it affect mobile in any shape or form

I need to say nice find