MZ Trouble with saving and loading class objects

I'm trying to save a class and load it again, the data is saved but the function ($myFood.add) no longer works. I've tried both adding the object to $gameSystem and hooking DataManager, neither work.

This is what I've got so far
JavaScript:
(() => {
    $myFood = null;

    Food.prototype = Object.create(Food.prototype);
    Food.prototype.constructor = Food;

    function Food(){
        this.initialize.apply(this, arguments);
    };
       
    Food.prototype.initialize = function() {
        this._amount = 0;
    };

    Food.prototype.add = function()
    {
        this._amount+=1;
    };

    (function(alias) {
        DataManager.createGameObjects = function() {
          alias.apply(this, arguments);
          $myFood = new Food();
        };
      })(DataManager.createGameObjects);
     
      (function(alias) {
        DataManager.makeSaveContents = function() {
          var contents = alias.apply(this, arguments);
          contents.mySaveProperty = $myFood;
          return contents;
        };
      })(DataManager.makeSaveContents);
     
      (function(alias) {
        DataManager.extractSaveContents = function(contents) {
          alias.apply(this, arguments);
          var data = contents.mySaveProperty;
          if (data) $myFood = data;
        };
      })(DataManager.extractSaveContents);

})();
Your Food function is defined locally on the IIFE. To work as you want it to, it needs to be defined on the global scope, e.g.
JavaScript:
window.Food = function() {
  // etc
}
Relevant quote:
MV/MZ include specific encode/decode methods for preserving custom prototypes (Game_Actor, Game_Item, etc) through saves.

Make sure Game_Rune is declared as a global; if not, JsonEx._decode (rmmz_core.js) will not correctly reassign the prototype on parse.
@caethyril Do you keep, like, bookmarks or a spreadsheet of posts where you've talked about various topics to refer back to? Or are you just very handy at finding random old posts on here? 😛
Your Food function is defined locally on the IIFE. To work as you want it to, it needs to be defined on the global scope, e.g.
JavaScript:
window.Food = function() {
  // etc
}
Relevant quote:
Thanks. This works
JavaScript:
Food = function(){
    ...
}


How would I do this with ES6 class syntax?
JavaScript:
class Food
{
    constructor()
    {}
    
    func1()
    {}
    ...
}
@Rlyeh - like function abc, the syntax class abc defines on the enclosing block scope. So, like with the full prototype syntax, you would either need to explicitly define it on the global scope or define it outside of the IIFE.

Note that in JavaScript, classes are "sugar" syntax: it's prettier and enables "true" private instance members without workarounds, but I think that's about it. It doesn't change how things work behind the scenes. Converting should be pretty simple...in this case:
  • The constructor is Food(),
  • The func1 etc methods are initialize and add.
Apparently it's also possible to apply patches using class syntax, e.g.

@ATT_Turan - I'm good at seeing things and thinking "this looks familiar", but yeah, from there I just use a search engine to (try to) find the relevant content.
@Rlyeh - like function abc, the syntax class abc defines on the enclosing block scope. So, like with the full prototype syntax, you would either need to explicitly define it on the global scope or define it outside of the IIFE.
How would I do this?
I come from C++ and find class easier to read.

This is what I have
JavaScript:
class Food
{
  _amount = 0;
 
  constructor()
  {}

  add()
  {
    this._amount += 1;
  }
}

$myFood = null;

(() => {
    (function(alias) {
        DataManager.createGameObjects = function() {
          alias.apply(this, arguments);
          $myFood = new Food();
        };
      })(DataManager.createGameObjects);
      
      (function(alias) {
        DataManager.makeSaveContents = function() {
          var contents = alias.apply(this, arguments);
          contents.mySaveProperty = $myFood;
          return contents;
        };
      })(DataManager.makeSaveContents);
      
      (function(alias) {
        DataManager.extractSaveContents = function(contents) {
          alias.apply(this, arguments);
          var data = contents.mySaveProperty;
          if (data) $myFood = data;
        };
      })(DataManager.extractSaveContents);

})();
That looks OK, the class is implicitly declared as a global now. If it's not working correctly, you can try explicitly defining it as a member of the global object, like I mentioned for the ES5 syntax, e.g.
JavaScript:
window.Food = class Food {
  // etc
}
If you're asking about how to patch methods using class syntax, I think it's largely the same idea as the prototype stuff: store a pointer to the original method, then replace that method with a new one that invokes the stored method at some point. I haven't tried it myself.
It works if I use if (data) $myFood = Object.assign(new Food(), data); instead of if (data) $myFood = data;
That looks OK, the class is implicitly declared as a global now. If it's not working correctly, you can try explicitly defining it as a member of the global object, like I mentioned for the ES5 syntax, e.g.
JavaScript:
window.Food = class Food {
  // etc
}
That gives an error at startup "Class constructor Food cannot be invoked without 'new'"
Edit: it was the missing ;

If you're asking about how to patch methods using class syntax, I think it's largely the same idea as the prototype stuff: store a pointer to the original method, then replace that method with a new one that invokes the stored method at some point. I haven't tried it myself.

No, just trying to get class methods to work with loading saved data.
This is confusing.
Food = class { ... }; works but class Food { ... } doesn't unless I use Object.assign(new Food(), data).
Oh, huh. I just tested in the console (F8 during playtest) and apparently class X doesn't actually declare a global:

• function myFunc() {} • class myClass {} • "myFunc" in window > true • "myClass" in window > false
The "proper" way has always been to define globals explicitly, as members of the global object. Since it happens with class but not with function, I'd guess it's related to JS modules.

(In modern JS you'd usually use globalThis to get the global object. However, I've used window here because that's what RPG Maker's JsonEx._decode method, cf rmmz_core.js, references for rebuilding object instances from JSON save data.)
I come from C++ and find class easier to read.
I also come from C++, and various other languages where classes are defined similarly, so I get it. However, and this could just be a me thing, I actually kinda like using the prototype syntax better for RPG Maker stuff.

The reason is because, in the languages that the class syntax is meant to resemble, it's customary to put each class in its own source file. However, in JavaScript, you might have 30 classes stuffed into the same file.

With prototype syntax, if you open rmmz_objects.js, and then quickly scroll to a random point in the file, you'll be able to immediately tell exactly what you're looking at, with a glance. If it had instead been written in class syntax, you wouldn't know what class the methods you're looking at belong to, without further investigation. Similarly, prototype syntax allows you to Ctrl + F and search for a specific method. Whereas with class syntax, you might have 15 methods all simply named "update" (in the same file), and you wouldn't be able to specify which one you're searching for.

Original thread: https://forums.rpgmakerweb.com/threads/trouble-with-saving-and-loading-class-objects.158317/