# Add these two helper functions inside your class,
# preferably right above the __ClickExitButton function.
def __OnAcceptExit(self):
import app
app.Exit()
def __OnCloseExitDialog(self):
if self.exitDialog:
self.exitDialog.Close()
self.exitDialog = None
return True
# Now, replace your old __ClickExitButton function with this one:
def __ClickExitButton(self):
import uiCommon # Imports the common dialogs module
# Creates the question dialog window
self.exitDialog = uiCommon.QuestionDialog()
self.exitDialog.SetText("Do you really want to exit the game?") # You can change this text
# Defines what happens when "Yes" is clicked (calls the __OnAcceptExit function)
self.exitDialog.SetAcceptEvent(self.__OnAcceptExit)
# Defines what happens when "No" is clicked (calls the __OnCloseExitDialog function)
self.exitDialog.SetCancelEvent(self.__OnCloseExitDialog)
# Opens the dialog window
self.exitDialog.Open()
---
### **Explanation of the Change**
Why was this change made?
The original `__ClickExitButton` function would instantly close the game client (`app.Exit()`). As you pointed out, this "instant close" can be problematic, as players might click it by accident. This new implementation is based on a better approach, similar to what is often
seen in demonstration videos/GIFs, which uses a confirmation dialog to prevent accidental exits and improve the overall user experience.
The original `__ClickExitButton` function would instantly close the game client (`app.Exit()`). This can be problematic as players might click it by accident, losing their progress or having to log in again. The new code replaces this instant action with a confirmation dialog, improving the user experience by preventing accidental exits.
**How does it work?**
The new implementation is broken down into three functions for clarity:
1. `__ClickExitButton(self)`:
* This is the main function that gets called when the user clicks the exit button.
* Instead of closing the game, it now creates an instance of `uiCommon.QuestionDialog()`, which is a standard pop-up window with "Yes" and "No" buttons.
* It sets the question text for the dialog (e.g., "Do you really want to exit the game?").
* Crucially, it assigns other functions to the "Yes" (`SetAcceptEvent`) and "No" (`SetCancelEvent`) buttons.
2. `__OnAcceptExit(self)`:
* This small, dedicated function contains the single command to close the game: `app.Exit()`.
* It is only executed if the user clicks the "Yes" button on the confirmation dialog.
3. `__OnCloseExitDialog(self)`:
* This function's job is to simply close the confirmation dialog window if the user clicks "No".
* It also cleans up the reference to the dialog by setting `self.exitDialog = None`.