MZ Game closing unexpectedly, custom scene involved.

Honestly I'm not 100% sure if this is the right forum for this topic, because I'm not 100% sure where the problem actually lies.

I have an event that at a point, enters a loop, set to break once a switch is toggled. In that loop is a call to my custom scene. In the scene is a means to close the scene using SceneManager.pop(), which as far as I can tell is the proper way to do it. The scene's close function called through pop merely closes all the windows, nothing else. The odd part happens after the scene is closed. I have the option to break the loop after the scene closes, returning to the normal game map. The first time this is done, everything's fine. But if, after closing the scene, I choose to open it again while still inside the loop, and then close the scene, then break the loop, the game just closes. No crash, no error message, it just closes as if I'd shut it down myself.

I put this topic in Learning Javascript because my custom scene is involved, though I'm not sure how much of an impact it's really having, since the crash happens during execution of a set of event commands, no custom script at all in that part. Trying to debug a set of event commands at the code level is pretty tricky(at least as far as setting a breakpoint goes), so I figured I'd see if there was an obvious answer before I went bug hunting.

Thanks in advance for any and all assistance!
SceneManager.pop() will always close the current scene. If there is no scene open it will just close the game window itself.
Hi there!

I think you can use the console log to help you with that. I suspect that the SceneManager.pop() can be closing your game.
So, you can check when it is called:

JavaScript:
if(SceneManager._scene instanceof YourSceneClass){
    console.log("this is the scene x")
else{
    console.log("this is the scene y")
}

Just for you to be sure if the pop function is called on the scene map and closing the game.

Otherwise, you can just put an if condition and call the pop function only when you are in your custom scene.
Thanks for the replies! I should probably clarify, the pop function is only called INSIDE the scene, the "crash" happens AFTER the scene is gone. The closest thing I can think of offhand that might cause the problem is that the pop function is called as a script event command in a common event. But that event is intended to be handled ONLY inside the scene, by its own game interpreter. If it's the culprit, then somehow its event list is being transferred to the map scene's interpreter, and the event command is being called AGAIN after breaking the loop, for some reason, as if the event with the pop call was reserved and not immediately handled in-scene. But that still doesn't quite explain why it doesn't crash when I break the loop after opening and closing my scene only once.

For further clarity, when the loop is broken, there IS nothing else in the event to run, the break loop is the last thing in the path. I could probably add something, just to try to get a better idea of exactly WHEN it's crashing on me.

I still intend to do a proper bug hunt, this one is actually wrinkling my brain(more of a headscratcher right now than a headbanger, like most of the issues I feel compelled to post a topic on). I likely won't have time this weekend, but any further information will be helpful in the meantime!
I think we need to see your actual code as well as screenshots of the common event.
without that info all answers would be random guesses.
...I am an utter moron. I figured it out. Played around with other common events ran in the scene... and they ran after breaking the loop too, in sequence, right before the shutdown.

When I was trying to get common events set up to run when clicking buttons in the scene, I had put in the code to reserve a common event with $gameTemp as one of various solution attempts before I had something working, and forgot to see about removing it once I had what I needed... so even though I was running my events properly, it was still also being added to the other interpreter's queue, running when the looped event finally ended. So pop WAS being called twice.

Kinda tempted to write some tutorial documentation on this stuff, since there doesn't seem to be much in the way of figuring out HOW to do some of this stuff the way you want it done, especially when figuring out what you DON'T need.

A lot of tutorials-ish stuff I DO find either doesn't explain what's going on and what does what, or is a specific response to a specific question, and generally not suited for a general answer.

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/game-closing-unexpectedly-custom-scene-involved.138068/