MV activate the switch when the game is closed

I did a quick search with my low skill with plugins and came across this code

Spoiler: Code
Code:
if (Utils.isNwjs()) {
    var onClose = function() {
        if (confirm("Deseja mesmo sair?")) {
            $gameSwitches.setValue(1809, true); // Ativa o switch 2
            nw.App.quit();
        }
    };
    var onSmall = function() {
        $gameSwitches.setValue(1810, true); // Ativa o switch 1
    };
    var w = nw.Window.get();
    w.on("close", onClose);
    w.on("minimize", onSmall);
}

and it was working fine, but I don't know if it was in conflict with other plugins, it only activates the switch when the game is minimized, when I close the game and start it again, I go back to the save, the switch was not activated as the command was intended, the switch should only be activated when the game is closed and opened again, does anyone have a fix or a ready plugin that does this?
switches are stored as part of a savefile.
if the switch is only turned on as part of closing the game, that happens after the game is saved - which means inside the savegame the switch is still off and as such loaded when the game is loaded again.
you would have to save the game as part of the closing script to save the switch - but that has other disadvantages because it will delay the closing.
That seemed strange to me because I had tested it the first few times and it had worked, I closed the game and when I opened it the stitch was activated, but this saving thing can't be done only with the Switch, right?

and if I saved when the game is closed like you said, do you know what this code would look like, as I said I'm VERY new to this?

:awat:
You could try replacing this line:
JavaScript:
            nw.App.quit();
...with a script call to save the game and then quit, e.g.
JavaScript:
            $gameSystem.onBeforeSave();
            var slotId = 1;  // save to slot 1
            if (DataManager.saveGame(slotId)) {
                StorageManager.cleanBackup(slotId);
                nw.App.quit();
            } else {
                alert("Save failed, oh no!");
            }
Deciding the save slot ID might be a problem. I would have suggested $gameSystem.savefileId(), i.e. the most recent save slot this playthrough, but that's only for MZ. 😴

(For MZ users: MZ saves asynchronously, so you'd need to replace if/else with e.g. a Promise chain.)

[Edit: I believe this post of mine was the basis for the code in the original post. I'm linking to that post for context and because it has some extra info.]
I found this plugin that makes certain switches stay activated forever and this is very useful

Plug-in

I just need to do now that one switch is activated when the game is closed that I think the other plugin does the rest the problem is that I can't activate the switch when the game closes

EDIT: I bring updates I updated nw.js and now the code is working. fine
Well, I'm not sure why you needed to update NWJS, but good to hear you got it working! 😤

The persistent switches plugin also avoids any problems related to saving outside of a playthrough, e.g. from the title menu or its sub-scenes (options/load). 😮‍💨
if you want global switches, or temp switches on local,
you can try Olivia_MetaControl as it function nice and more control
which savefiles.

local save file is if you use 1 game through 1,2,3 save files,
global affects all.

while new game starts a new local 🙂
Well, I'm not sure why you needed to update NWJS, but good to hear you got it working! 😤

The persistent switches plugin also avoids any problems related to saving outside of a playthrough, e.g. from the title menu or its sub-scenes (options/load). 😮‍💨
I was getting very frustrated, because I had already tried everything, but even so, when I closed the game the switches were not activated. And then I decided to ask the GPT chat why this is happening? And he asked me to update the sw.js. I had already tried everything, and after a lot of work to update this Sw.js, I tested it and it worked. I think my project was out of date, I created it in the old versions of rpg maker mv.
Sw.js doesnt exist as you probably NWJS?

MetaControls probably need 1.6.x to work based on the coded used.
NWJS 0.29 is old and most probably go for 0.48.4 (that I use) as MZ start with it
if not changed, and others go to the latest version.

but if it works how you want it go, than it's good 🙂

otherwise, we help you further.

Original thread: https://forums.rpgmakerweb.com/threads/activate-the-switch-when-the-game-is-closed.175734/