How to keep a switch on when loading back to a save file? (having trouble with BGS/BGM after loading in)

I have a chain command puzzle in my game, basically where you have to press a number of inputs before the time runs out. If the time runs out, you get a game over. I have it so it autosaves right before you enter the map to the puzzle. This means when you get a game over, it takes you back to this spot.

My issue is that the map you are returning to should have two sounds playing, one BGM and BGS (music and a rain sound effect). When pressing continue after a game-over and returning to this map, none of the music is playing. I can't choose to autoplay BGS and BGM in the map properties because the music is only triggered during an earlier autorun event on that map.

At first, I tried to turn on a switch during the game-over scene that would trigger the BGM and BGS when returning, but I quickly realized that because I'm loading into a previous save, the switch won't be kept on.

Is there any way to turn on a switch that is retained when loading into the latest save?


Not sure if this matters, but this is the script I use to return to the latest save file

if (DataManager.loadGame(DataManager.latestSavefileId(1))) {

$gameSystem.onAfterLoad();
SceneManager._scene.fadeOutAll();
if ($gameSystem.versionId() !== $dataSystem.versionId) {
var p = $gamePlayer;
p.reserveTransfer($gameMap.mapId(), p.x, p.y, p.direction(), 0);
p.requestMapReload();
}
SceneManager.goto(Scene_Map);
}
Sounds to me like the easiest solution would probably be to have the first autorun set a switch. Then have a second autorun dependent on the switch which starts the music and then erases itself. This way the music change will also persist when the player just leaves the map and returns. That scenario would not be covered by your proposed solution of carrying over the switch.

If you are dead set on carrying over the switch, then something like this might work as an addition to your load script:
JavaScript:
var rememberSwitch = $gameSwitches.value(x); //this is new
if (DataManager.loadGame(DataManager.latestSavefileId(1))) {

    $gameSystem.onAfterLoad();
    SceneManager._scene.fadeOutAll();
    if ($gameSystem.versionId() !== $dataSystem.versionId) {
        var p = $gamePlayer;
        p.reserveTransfer($gameMap.mapId(), p.x, p.y, p.direction(), 0);
        p.requestMapReload();
    }
    $gameSwitches.setValue(x, rememberSwitch); //this is new
    SceneManager.goto(Scene_Map);
}
Replace x with the ID of the switch you want to remember.
yes and no

by default all switches are unique to the save - it has to be like this for some base functionality.

however there are plugins for persistent switches that change that into specific switches being identical over all saves, usually to enable new game plus content or the like.

Problem is I'm not sure if that would help you, and it would make the switches identical over all savefiles, not just the one that gives you problems.

can't you turn on the trigger switch just before the autosave and use erase event for the music settings?
Sounds to me like the easiest solution would probably be to have the first autorun set a switch. Then have a second autorun dependent on the switch which starts the music and then erases itself. This way the music change will also persist when the player just leaves the map and returns. That scenario would not be covered by your proposed solution of carrying over the switch.

If you are dead set on carrying over the switch, then something like this might work as an addition to your load script:
JavaScript:
var rememberSwitch = $gameSwitches.value(x); //this is new
if (DataManager.loadGame(DataManager.latestSavefileId(1))) {

    $gameSystem.onAfterLoad();
    SceneManager._scene.fadeOutAll();
    if ($gameSystem.versionId() !== $dataSystem.versionId) {
        var p = $gamePlayer;
        p.reserveTransfer($gameMap.mapId(), p.x, p.y, p.direction(), 0);
        p.requestMapReload();
    }
    $gameSwitches.setValue(x, rememberSwitch); //this is new
    SceneManager.goto(Scene_Map);
}
Replace x with the ID of the switch you want to remember.
1. I set a switch during the first autorun that created this autorun event. Shortly after, the game autosaves and you enter the chain command puzzle. Unfortunately, when a game over is triggered and you press continue (which returns you to the save) there still isn't any music playing. However, when I exit the game instead of continuing and then return to that same save, the music does work.
Screenshot (514).png


2. I couldn't get this script to work. I used it for switch #909 which would trigger an autorun event upon returning to the map but the switch did not stay on.
Screenshot (516).png
1) probably because in the first case, the map is not reloaded

2) because you didn't follow the instructions. you were told to add it to your load script, not to use it as an independent script command.
1) probably because in the first case, the map is not reloaded

2) because you didn't follow the instructions. you were told to add it to your load script, not to use it as an independent script command.
Thanks for the quick response.

1) Okay. I need to have the music playing when I load the last save through my game over event, not just through the title screen. Is there a way to reload the map to achieve this or is erasing the event just not the solution for me?

2) I tried including it in the load script but there was not enough room to fit both in.
Ok so I investigated for a bit and it turns out the switch gets carried over just fine with the method I proposed. (You can verify that by pressing F9 and looking at the switch value directly)
The problem is that the map actually does not get reloaded with your script and the autorun event stays erased. So no matter how we try to trigger it, it will not run.
The quick and dirty way to circumvent that was to manually "unerase" the event with the following script which I also added to your load script.
JavaScript:
var rememberSwitch = $gameSwitches.value(x); //1. modification
if (DataManager.loadGame(DataManager.latestSavefileId(1))) {

    $gameSystem.onAfterLoad();
    SceneManager._scene.fadeOutAll();
    if ($gameSystem.versionId() !== $dataSystem.versionId) {
        var p = $gamePlayer;
        p.reserveTransfer($gameMap.mapId(), p.x, p.y, p.direction(), 0);
        p.requestMapReload();
    }
    $gameSwitches.setValue(x, rememberSwitch); //1. modification
    $gameMap.event(y)._erased = false; //2. modification
    $gameMap.event(y).refresh(); //2. modification
    SceneManager.goto(Scene_Map);
}
Note that this got long enough to run into the RPGMMV limitation of only being able to use 12 lines. However a single line can be as long as you wish. So you could change the script something like this (which is ugly, but oh well...):
JavaScript:
var rememberSwitch = $gameSwitches.value(x); //1. modification
if (DataManager.loadGame(DataManager.latestSavefileId(1))) {
    $gameSystem.onAfterLoad(); SceneManager._scene.fadeOutAll();
    if ($gameSystem.versionId() !== $dataSystem.versionId) {
        $gamePlayer.reserveTransfer($gameMap.mapId(), $gamePlayer.x, $gamePlayer.y, $gamePlayer.direction(), 0);
        $gamePlayer.requestMapReload();
    }
    $gameSwitches.setValue(x, rememberSwitch); //1. modification
    $gameMap.event(y)._erased = false; $gameMap.event(y).refresh(); //2. modification
    SceneManager.goto(Scene_Map);
}

2) because you didn't follow the instructions. you were told to add it to your load script, not to use it as an independent script command.
Sorry for being unclear. I actually posted the full modified load script and marked the insertions with comments. I thought that would be easier then describing both places where a line needs to be inserted.
Ok so I investigated for a bit and it turns out the switch gets carried over just fine with the method I proposed. (You can verify that by pressing F9 and looking at the switch value directly)
The problem is that the map actually does not get reloaded with your script and the autorun event stays erased. So no matter how we try to trigger it, it will not run.
The quick and dirty way to circumvent that was to manually "unerase" the event with the following script which I also added to your load script.
JavaScript:
var rememberSwitch = $gameSwitches.value(x); //1. modification
if (DataManager.loadGame(DataManager.latestSavefileId(1))) {

    $gameSystem.onAfterLoad();
    SceneManager._scene.fadeOutAll();
    if ($gameSystem.versionId() !== $dataSystem.versionId) {
        var p = $gamePlayer;
        p.reserveTransfer($gameMap.mapId(), p.x, p.y, p.direction(), 0);
        p.requestMapReload();
    }
    $gameSwitches.setValue(x, rememberSwitch); //1. modification
    $gameMap.event(y)._erased = false; //2. modification
    $gameMap.event(y).refresh(); //2. modification
    SceneManager.goto(Scene_Map);
}
Note that this got long enough to run into the RPGMMV limitation of only being able to use 12 lines. However a single line can be as long as you wish. So you could change the script something like this (which is ugly, but oh well...):
JavaScript:
var rememberSwitch = $gameSwitches.value(x); //1. modification
if (DataManager.loadGame(DataManager.latestSavefileId(1))) {
    $gameSystem.onAfterLoad(); SceneManager._scene.fadeOutAll();
    if ($gameSystem.versionId() !== $dataSystem.versionId) {
        $gamePlayer.reserveTransfer($gameMap.mapId(), $gamePlayer.x, $gamePlayer.y, $gamePlayer.direction(), 0);
        $gamePlayer.requestMapReload();
    }
    $gameSwitches.setValue(x, rememberSwitch); //1. modification
    $gameMap.event(y)._erased = false; $gameMap.event(y).refresh(); //2. modification
    SceneManager.goto(Scene_Map);
}


Sorry for being unclear. I actually posted the full modified load script and marked the insertions with comments. I thought that would be easier then describing both places where a line needs to be inserted.
Thanks for putting so much work into this. I just want to clarify some things.

1) Is this code a solution for carrying over a switch or a solution to your first proposal outlined below? Your first proposal of creating an autorun event that erases itself didn't need any script. I think I'm just confused because I had outlined problems with the two proposed solutions (one being carrying over a switch and the other being through an autorun event) and you replied back with only one solution.

I have an autorun event that I was trying to trigger when testing your solution to carry over a switch, and I also have the autorun event that is triggered from the first autorun event that erases itself. Because I'm not sure which solution I'm looking at I don't know what switchID to put in.
Sounds to me like the easiest solution would probably be to have the first autorun set a switch. Then have a second autorun dependent on the switch which starts the music and then erases itself. This way the music change will also persist when the player just leaves the map and returns. That scenario would not be covered by your proposed solution of carrying over the switch.

2) Is this new script supposed to replace my existing load script entirely? All I have to do is put this into a script and change the x value to the switchID I want?

var rememberSwitch = $gameSwitches.value(x); //1. modification
if (DataManager.loadGame(DataManager.latestSavefileId(1))) {
$gameSystem.onAfterLoad(); SceneManager._scene.fadeOutAll();
if ($gameSystem.versionId() !== $dataSystem.versionId) {
$gamePlayer.reserveTransfer($gameMap.mapId(), $gamePlayer.x, $gamePlayer.y, $gamePlayer.direction(), 0);
$gamePlayer.requestMapReload();
}
$gameSwitches.setValue(x, rememberSwitch); //1. modification
$gameMap.event(y)._erased = false; $gameMap.event(y).refresh(); //2. modification
SceneManager.goto(Scene_Map);
}
Sorry for not being clear again. Feeling a bit under the weather at the moment.
My second proposal does both - it carries over a switch and it "unerases" an event. If you don't need to carry over the switch, simply delete the lines that end with // 1. modification. (This seems to be the case. If you do want to carry over a switch anyways, leave them in and replace x with the id of the switch.)
The line marked with // 2. modification is the one that "unerases" an event. Here you will need to replace y with the id of the event you want to "unerase". So in your case that is the autorun that switches the music. Telling from your screenshot above, it should be 41.
Sorry for not being clear again. Feeling a bit under the weather at the moment.
My second proposal does both - it carries over a switch and it "unerases" an event. If you don't need to carry over the switch, simply delete the lines that end with // 1. modification. (This seems to be the case. If you do want to carry over a switch anyways, leave them in and replace x with the id of the switch.)
The line marked with // 2. modification is the one that "unerases" an event. Here you will need to replace y with the id of the event you want to "unerase". So in your case that is the autorun that switches the music. Telling from your screenshot above, it should be 41.
Thanks! This seems to have solved the music issue. However, am I now getting an error when continuing from the game over screen.

Initially, I followed your instructions and everything seemed fine but now I'm getting this error over and over again (when I click "continue" from the game over screen). Any ideas?

Screenshot (519).png
I took a quick look, but I can't see how my modification to your load script would cause that particular error. Are you sure these two are related? If you are, then sorry, but I am at a loss then.
I took a quick look, but I can't see how my modification to your load script would cause that particular error. Are you sure these two are related? If you are, then sorry, but I am at a loss then.
I thought maybe YEP_SaveEventLocations was causing the issue but upon removing it, the issue persists.

Screenshot (521).png


However, I did some experimenting and it appears that the issue is with the forced autosave.

I have a player touch event that contains this script:
$gameSystem.onBeforeSave(1);
DataManager.saveGame(1);
I trigger a switch after this so that you can't walk over it again.

The player touch event is right before I enter the chain command puzzle, so after the game-over, you can come right back to this spot and try again. Unfortunately, when you autosave, do the puzzle and trigger a game-over, upon loading back in you get that error. When I remove the autosave and trigger a game-over, there is no error and everything works fine.

I didn't realize this because when I was testing your solution I'd start the game by loading a file rather than starting a new game entirely. The file I was loading into had already triggered the autosave so it didn't need to again, thus the lack of errors.

So something is obviously off with the auto-saving, but I haven't figured it out.
I recently wrote a little plugin that also has an autosave feature. With that I didn't encounter that kind of error. It does two things differently that could actually make a difference. First it calls the following function after a successful save:
JavaScript:
StorageManager.cleanBackup(1);
And secondly I noticed that $gameSystem.onBeforeSave(1); should not be called with a parameter. It should be the following instead:
JavaScript:
$gameSystem.onBeforeSave();
Try if this fixes the error.

Because in general you have a very weird bug on your hands. You have an existing Game_Event object (otherwise either the function would not be called or you would get something like "can't call function refresh of undefined"). And the Game_Map object must know about it, otherwise the no function call for this event would be executed when Game_Map does its refresh.
However for some reason when either YEP_SaveEventLocation or just the core RPGM scripts try to actually access the event's information with this.event() that suddenly returns undefined.
I just can't think of a circumstance where all these conditions would be true, resulting in the bug you are experiencing. So I recommend trying my suggested modifications to your save script and hoping that somehow solves this issue.
I recently wrote a little plugin that also has an autosave feature. With that I didn't encounter that kind of error. It does two things differently that could actually make a difference. First it calls the following function after a successful save:
JavaScript:
StorageManager.cleanBackup(1);
And secondly I noticed that $gameSystem.onBeforeSave(1); should not be called with a parameter. It should be the following instead:
JavaScript:
$gameSystem.onBeforeSave();
Try if this fixes the error.

Because in general you have a very weird bug on your hands. You have an existing Game_Event object (otherwise either the function would not be called or you would get something like "can't call function refresh of undefined"). And the Game_Map object must know about it, otherwise the no function call for this event would be executed when Game_Map does its refresh.
However for some reason when either YEP_SaveEventLocation or just the core RPGM scripts try to actually access the event's information with this.event() that suddenly returns undefined.
I just can't think of a circumstance where all these conditions would be true, resulting in the bug you are experiencing. So I recommend trying my suggested modifications to your save script and hoping that somehow solves this issue.
Screenshot (522).png

So my autosave script should look like this?
Yes. At least that's the way I do it and I didn't run into any errors. Is it gone now or do you still get an error with this new script?
Yes. At least that's the way I do it and I didn't run into any errors. Is it gone now or do you still get an error with this new script?
Still getting an error.

Screenshot (523).png


Oddly enough, when there is no self-switch triggered after the autosave there is no error when loading back - only a pitch black screen that fixes itself when I open up the menu.
Ok, I don't understand what is going on in your game at all and I am afraid thus can no longer be of assistence. Sorry.
I hope you find a solution to your problem though.
Ok, I don't understand what is going on in your game at all and I am afraid thus can no longer be of assistence. Sorry.
I hope you find a solution to your problem though.
All good. Thanks for helping out so much. I'll have to make a separate post and see if anyone can figure it out.

Original thread: https://forums.rpgmakerweb.com/threads/how-to-keep-a-switch-on-when-loading-back-to-a-save-file-having-trouble-with-bgs-bgm-after-loading-in.150961/