MZ Failed To Initialize Graphics (only with a Plugin)

I require the help of the community to understand what's going on and maybe how to fix this...
1.) Backed up my RMMZ projects (Documents folder)
2.) Built new PC
3.) Installed Steam
4.) Installed RPG Maker MZ
5.) Put RMMZ projects back in Documents
6.) Loaded up a project -> Working on plugin

When I playtest I get "Failed to initialize graphics". When I disable the plugin, it works just fine. I am a bit confused by this as the console doesn't lead to anywhere near my plugin, nor mentions it, and nothing my plugin does touches the manager functions ... (Well DataManager.loadDatabase - but I commented that section out when I continued to test the error).

Any ideas? I've tried copying over the "img" folder from "newproject" in the actual MZ folder to my project folder and still nothing. I have latest drivers on everything (chipset, gpu, etc). Definitely not a PC issue.
it may be because your browser has unsupported WebGL or is disabled.

if its disabled, turn it on, if its unsupported, this may be your issue.
depending the browser you use.

so far I know, MZ heavily rely on PIXI v5 which can't render if something happens.
so I would check your browser or security if its blocks something in order to render.

but thats all I can see from the rmmz manager.
With no plugins, that error means that Graphics.initialize did not return truthy, i.e. the PIXI app, Graphics._app, was not created for some reason.

A plugin could reliably cause your error trace by altering a relevant method of Graphics, e.g.
JavaScript:
Graphics._createPixiApp = function() { /** do nothing */ };
UPDATE 02:
Okay well I found the culprit. When I removed this code from my plugin, it works fine again... but am curious as to how this broke the initGraphics 😳
JavaScript:
Object.prototype.matches = function(obj) {
    return Object.keys(this).every(key => {
        return (typeof this[key] === "object" && typeof obj[key] === "object")
            ? this[key].matches(obj[key])
            : this[key] === obj[key];
    });
};

-- Adding this just for context that I have a check for arrays as well that mix-match with the Object version above:
JavaScript:
Array.prototype.matches = function(array) {
    if (!Array.isArray(array) || this.length !== array.length) return false;
    for (let i = 0; i < this.length; i++) {
        const typeA = Object.prototype.toString.call(this[i]);
        const typeB = Object.prototype.toString.call(array[i]);
        if (typeA !== typeB) return false;
        if (this[i] && this[i].matches) { // function must exist
            if (!this[i].matches(array[i])) return false;
        } else if (this[i] !== array[i]) {
            return false;
        }
    }
    return true;
};
It could be that Object.prototype.matches is already in use. The question is, why do you add such a function anyway? Such a "magic" function that is made to solve everything usually only shifts problems, but doesn't solve them, from my experience.
It could be that Object.prototype.matches is already in use. The question is, why do you add such a function anyway? Such a "magic" function that is made to solve everything usually only shifts problems, but doesn't solve them, from my experience.
Well I originally made it to compare the "list" array for events, since there are many types of values in there (numerical, arrays, objects, strings, etc) and I wanted a "catch-all" function to ensure the entire array matches another. RMMZ's default Array.prototype.equals isn't strong enough to do that sadly.

Also I tried changing the function name to "Object.prototypie.matchObjects" and it still errors out. So I don't think it's the method name *thinks*. Google search also found there is an "Element.prototype.matches" but no "Object.prototype.matches" by default, so I doubt RMMZ would have one.
-- ISSUE SOLVED --

Welp.. I have no idea what exactly I did, but I changed the function to this, and it works now - so I guess we can resolve this issue 😛

JavaScript:
function matchObjects(source, obj) {
    return Object.keys(source).every(key => {
        if (obj.hasOwnProperty(key)) {
            return source[key] instanceof Array && obj[key] instanceof Array
                ? source[key].matches(obj[key])
                : obj[key] === source[key];
        }
        return false;
    });
};
Object.keys returns all enumerable keys, and you have added the matches function as a new enumerable key. Functions are of type object, hence I believe this part of your original code would recurse until it errors out due to maximum call depth:
JavaScript:
        return (typeof this[key] === "object" && typeof obj[key] === "object")
            ? this[key].matches(obj[key])
            : this[key] === obj[key];
[Edit: as Solar_Flare points out, I misremembered: typeof actually returns function for functions. So the actual problem may well be something else.]

Object.defineProperty defaults to enumerable: false, so you could probably have used that instead. Like Aerosys said, though, I think I'd personally use a different approach.
Object.keys returns all enumerable keys, and you have added the matches function as a new enumerable key. Functions are of type object, hence I believe this part of your original code would recurse until it errors out due to maximum call depth:

Object.defineProperty defaults to enumerable: false, so you could probably have used that instead. Like Aerosys said, though, I think I'd personally use a different approach.
Well I have the Array.prototype.matches set to false, I don't know if that would have affected the Object.prototype.matches? (would it?) 😳

JavaScript:
Object.defineProperty(Array.prototype, "matches", {
    enumerable: false
});
While it is possible to add functions to Array, you shouldn't do that IMO. It's just a matter of time until it clashes with another plugin or library.

Do this instead

Code:
function areListsEqual(list1, list2) {
  ...
}
Well I have the Array.prototype.matches set to false, I don't know if that would have affected the Object.prototype.matches?
😅 No. I'm not sure why you would think that. You've defined the property on both prototypes, so they're mutually independent rather than one inheriting from the other. Even so, Array inherits from Object, not the other way around.

I strongly recommend rearranging your code like Aerosys suggested!

Regardless, I think it would be a good idea for you to (re)learn about inheritance in object-oriented programming, since it's a very important concept. E.g.
Well I originally made it to compare the "list" array for events, since there are many types of values in there (numerical, arrays, objects, strings, etc) and I wanted a "catch-all" function to ensure the entire array matches another. RMMZ's default Array.prototype.equals isn't strong enough to do that sadly.
The way I handle comparing lists of event or move route commands is:

JavaScript:
if(JSON.stringify(left.list) == JSON.stringify(right.list))

Though, I wouldn't recommend using this method if you're doing it every frame.


Functions are of type object
Huh? What? No, typeof(someFunction) returns 'function'. Though someFunction instanceof Object would return true, but that's not what he was doing.


-----

Generally speaking, I don't agree with Aerosys and caethyril that you shouldn't ever add functions to the Array prototype (or even the Object prototype), but you should definitely never add enumerable properties to them (always use defineProperty or defineProperties with enumerable: false). Still, since it was somehow causing problems in this case and you couldn't figure out how, just making it a standalone function is probably the best option.
Huh? What? No, typeof(someFunction) returns 'function'. Though someFunction instanceof Object would return true, but that's not what he was doing.
👋 Oh wow, I was so focused on the inheritance thing that I forgot typeof distinguishes functions as their own type. Not sure what the problem was, then. 😴

(Could perhaps refine the error message by temporarily overriding _createPixiApp to remove the try/catch.)
The way I handle comparing lists of event or move route commands is:

JavaScript:
if(JSON.stringify(left.list) == JSON.stringify(right.list))

Though, I wouldn't recommend using this method if you're doing it every frame.



Huh? What? No, typeof(someFunction) returns 'function'. Though someFunction instanceof Object would return true, but that's not what he was doing.


-----

Generally speaking, I don't agree with Aerosys and caethyril that you shouldn't ever add functions to the Array prototype (or even the Object prototype), but you should definitely never add enumerable properties to them (always use defineProperty or defineProperties with enumerable: false). Still, since it was somehow causing problems in this case and you couldn't figure out how, just making it a standalone function is probably the best option.
I'm still not 100% understanding the enumerable thing (other than having it set to false on my Array.prototype.matches method (because I copied the format in rmmz_core.js and how they had those array functions.

Instead of Object.keys(source).every(), would doing a for loop be better? Example:

JavaScript:
function objectsMatch(source, target) {
    for (const key in target) {
        if (!source[key]) return false;
        const typeA = Object.prototype.toString.call(source[key]);
        const typeB = Object.prototype.toString.call(target[key]);
        if (typeA !== typeB) return false;
        if (source[key] instanceof Array) {
            // still using Array.prototype.matches in this case
            if (!source[key].matches(target[key])) return false;
        } else if (typeA === "[object Object]") {
            if (!objectsMatch(source[key], target[key])) return false;
        } else if (source[key] !== target[key]) {
            return false;
        }
    }
    return true;
}
Or -something- like this?
Don't try to reinvent the wheel. It's not a shame to copy-paste from StackOverflow (well, not always).

I think this one is a good candidate, it's not the top-voted answer, but it's much easier to read: https://stackoverflow.com/a/45683145

Again, especially for beginners, it's probably easier to compare by fixed attributes instead of "everything". Usually, you know what your objects and lists contain, e.g. it's a list of actors, then you can compare by IDs or names. This gives you more control than a magix function that still hasn't covered edge-cases (e.g. NaN == NaN actually returns false).
Don't try to reinvent the wheel. It's not a shame to copy-paste from StackOverflow (well, not always).

I think this one is a good candidate, it's not the top-voted answer, but it's much easier to read: https://stackoverflow.com/a/45683145

Again, especially for beginners, it's probably easier to compare by fixed attributes instead of "everything". Usually, you know what your objects and lists contain, e.g. it's a list of actors, then you can compare by IDs or names. This gives you more control than a magix function that still hasn't covered edge-cases (e.g. NaN == NaN actually returns false).
I'm not exactly a beginner (have had 30+ years of coding experience from the C-based NWscript, ruby, python (modding Bethesda games), and with JS it's been about 10 years now. I just don't know -everything- about ever scripting language I work with. The "enumerable" property is new to me (I didn't understand its definition - I do now, googled it lol).

And I don't think I'm reinventing the wheel - I couldn't find anything that I needed, so I made it myself 😛 The logic worked when I tried testing it in the https://developer.mozilla.org/ site's "Try It Yourself" blocks, but I never launched it in RPG Maker so didn't notice the problem until recently.
---
The problem is though you don't know what the list contains in this case (since it derives from the event's list which has different parameters based on what you are trying to do in the event editor. That's why I wanted this function mainly to compare event lists - to account for each property/value types.
(( normally I'd agree with you, I'd tailor a function to match what I need. I don't usually do "general" functions like this, but it's why it's in my LvMZ_Core plugin because it could be a very useful function for me or anyone who wants to compare event lists - like a custom events plugin :troll: - or something.

Original thread: https://forums.rpgmakerweb.com/threads/failed-to-initialize-graphics-only-with-a-plugin.182732/