Loading last save through script

Hello.

I'm trying to adapt this little script I made for VX Ace.

I'm trying to modify the Scene_Gameover from rpg_scenes.js, I also looked at rpg_managers.js for something useful.

Here's what I have:

(function() {Scene_Gameover.prototype.gotoTitle = function() {    DataManager.loadGame(DataManager.latestSavefileId()); # also tried DataManager.lastAccessedSavefileId but I think the problem isn't here    Scene_Load.onLoadSuccess(); # without this, clicking does nothing};})();No matter what I do, I get "TypeError. undefined is not a function".

Trying other things like removing the "onLoadSuccess" line or adding other methods just make it worse, no response when clicking.

I don't know where the problem is.
What does the console log say? What line does it break at?

Can you describe what the code is supposed to do?

I have made script that goes back to the map when it is game over and you press ok.

Still working on making it load last save file though.
function Scene_Gameover() { this.initialize.apply(this, arguments);}Scene_Gameover.prototype = Object.create(Scene_Base.prototype);Scene_Gameover.prototype.constructor = Scene_Gameover;Scene_Gameover.prototype.initialize = function() { Scene_Base.prototype.initialize.call(this);};Scene_Gameover.prototype.create = function() { Scene_Base.prototype.create.call(this); this.playGameoverMusic(); this.createBackground();};Scene_Gameover.prototype.start = function() { Scene_Base.prototype.start.call(this); this.startFadeIn(this.slowFadeSpeed(), false);};Scene_Gameover.prototype.update = function() { if (this.isActive() && !this.isBusy() && this.isTriggered()) { this.gotoTitle(); } Scene_Base.prototype.update.call(this);};Scene_Gameover.prototype.stop = function() { Scene_Base.prototype.stop.call(this); this.fadeOutAll();};Scene_Gameover.prototype.terminate = function() { Scene_Base.prototype.terminate.call(this); AudioManager.stopAll();};Scene_Gameover.prototype.playGameoverMusic = function() { AudioManager.stopBgm(); AudioManager.stopBgs(); AudioManager.playMe($dataSystem.gameoverMe);};Scene_Gameover.prototype.createBackground = function() { this._backSprite = new Sprite(); this._backSprite.bitmap = ImageManager.loadSystem('GameOver'); this.addChild(this._backSprite);};Scene_Gameover.prototype.isTriggered = function() { return Input.isTriggered('ok') || TouchInput.isTriggered(); };Scene_Gameover.prototype.gotoTitle = function() { this.fadeOutAll(); DataManager.loadGameWithoutRescue(DataManager.latestSavefileId()); SceneManager.goto(Scene_Map); $gameSystem.onAfterLoad();};If I load this script and go to game over it reloads my last save file, my inventory is reset and the items on my screen are reset as well. There is probably some bugs but this is all I can come up with.

Disclaimer: I cannot code well just reverse engineer and copy.
Scene_Gameover.prototype.gotoTitle = function() { this.fadeOutAll(); DataManager.loadGameWithoutRescue(DataManager.latestSavefileId()); SceneManager.goto(Scene_Map); $gameSystem.onAfterLoad();};If I load this script and go to game over it reloads my last save file, my inventory is reset and the items on my screen are reset as well. There is probably some bugs but this is all I can come up with.

Disclaimer: I cannot code well just reverse engineer and copy.
The part of the code I left in the quote is the only code you need in a .js file for it to work actually, it just replaces the "gotoTitle" and uses the rest from the original rpg_scenes.js. I know a bit about this, but I mostly do what you do, copying.

Tho, you helped me figure it out, I forgot I had to call the Scene_Map after loading, that was the problem.

I don't have time now but tomorrow I'll see about the inventory problem and BGM not playing (That happened in my VX Ace version and I fixed it, pretty sure it's just adding 1 line)

By the way, why did you use "loadGameWithoutRescue" instead of just "loadGame"? It works for me. Not sure what WithoutRescue is...
You should call "loadGame". The WithoutRescue functions (both save and load game) do the actual work but may throw an error if something bad happens, which is caught in "loadGame"/"saveGame". The naming comes from older RM versions, which were written in Ruby. The try-catch-mechanism is called "rescue" there.

Also, you should work with the return value of "loadGame". It returns true if the load was succesful and false if there was an error. Maybe just call the original gotoTitle in that case.
Also, you should work with the return value of "loadGame". It returns true if the load was succesful and false if there was an error. Maybe just call the original gotoTitle in that case.
I saw that but had to ignore it because it was beyond my knowledge, I understand what it does but don't want to mess with it.

The code:

Scene_Gameover.prototype.gotoTitle = function() { this.fadeOutAll(); DataManager.loadGame(DataManager.latestSavefileId()); SceneManager.goto(Scene_Map); $gameSystem.onAfterLoad();};Is working fine already, the only problem so far (for me) is that the line $gameSystem.onAfterLoad(); doesn't seem to be working and believe me I tried everything, when the save is loaded, the BGM doesn't play. I tried adding practically every line that contained "load", "audio" and "reload" in it, my ignorance with this language is killing me.

If you mentioned the value of loadGame of true or false to detect if there's a saved game, I'm planning to do that later, if there's a save, load it, if not go to title screen.
I am glad I could help some. 

Original thread: https://forums.rpgmakerweb.com/threads/loading-last-save-through-script.47872/