Script crashing on save/load: proto's forgetting functions?

Hi, I've been trying to port some of my VX Ace scripts over and learning javascript as I go. I think it's going pretty well, though no doubt it could be much, much better. But I've met a problem that has me utterly stumped.

I've created a new camera system that allows the camera to focus on any combination of the player, events, followers, and static points.

It creates an object at "$gameMap._camera" that contains a lot of variables and functions needed for the camera. Since it's contained in $gameMap, it gets included in the save file automatically.

It's all working great, I think, but I've met a problem with saving and loading. After loading the game, functions in _camera seem to become undefined.

For example, here's a console.log of $gameMap before saving.

goodproto.png


And here it is after loading

badproto.png


The proto for Game_Camera is empty, all the functions are gone, and it crashes with "undefined is not a function" immediately after loading. I'm sure I must be missing something obvious but I have no idea what. Help?

The script can be found at https://dl.dropboxusercontent.com/u/65029315/Camera.js

There's stuff I'm not too happy with in there (storing/restoring backups ended up very clunky) and if you have feedback/pointers for the rest of the script they're welcome. But it's the stuff above that's breaking my back right now.

Thanks.

EDIT: So I found a way to hack it. The proto gets removed when the save extracter overwrites $gameMap, so this...

var gk_camera_DataManager_extractSaveContents = DataManager.extractSaveContents  DataManager.extractSaveContents = function(contents) {    var cam = $gameMap._camera.__proto__;    gk_camera_DataManager_extractSaveContents.call(this,contents);    $gameMap._camera.__proto__ = cam;    $gameMap._camera.restoreAfterLoad();  };works. But it does not feel good. It feels bad. So if there's a way I should be going about it (and I don't know why, but things like Game_Map and Game Event properly retain their protos when loaded) then I'd still like to hear it.
move your variable to game system... it's saved...

then you could create a method in game map to access that $gameSystem variable.

$gameMap is not saved if i'm not wrong. it's accessing $dataMap everytime it's running setup.

so if you save it there... when you leave map and reenter it also should be wiped out.
Hm, it looked to me like $gameMap was saved, since it's in DataManager.makeSaveContents and DataManager.extractSaveContents along with everything else. And the camera wasn't being destroyed, just it's proto - all its variables still existed. But if $gameSystem is a more proper place to put it, I'll try putting it there and seeing if that improves matters. Thanks.
OK I got it. Putting this here in case someone else has the same problem later.

The code already has a function (JsonEx._resetPrototype, called by JsonEx._decode)that restores lost protos to objects as the game is loaded from a save. but it'll only do that if it can find a reference to the object's constructor in a global object called window.

That's why things like $gameMap and events on the map were retaining their protos after load, but my camera was not. My camera was not found in window.

So getting it to work properly is as simple as making sure that the references are added to window before the save is loaded.

 function Game_Camera() {      window['Game_Camera'] = Game_Camera.prototype.constructor;      window['Camera_Point'] = Camera_Point.prototype.constructor;      this.initialize.apply(this, arguments);  }Putting it in the camera's constructor is probably not the ideal place, but it's working. Maybe I'll put it somewhere in Scene_Boot or something. As long as it gets run before loading a save can be attempted.

And it works. 🙂
A similar issue was driving me nuts.  This led to my answer and saved my sanity.  Thank you!

Original thread: https://forums.rpgmakerweb.com/threads/script-crashing-on-save-load-protos-forgetting-functions.49864/