Subscribe to gameVariable updates?

Hi,

Is there a way to subscribe / listen to gameVariable updates so my callback will be called upon a change in the variable?
I would not like to exploit Scene_Map.update() for performing frequent comparisons to detect the variable change.

To give a specific context of what I am looking for:
Certain time systems store the current hour in a game variable. I would like to detect that the hour has changed without having to constantly compare the previous value with the current value.
You can alias Game_Variables.prototype.setValue(id, value) to make further actions when a variable has been changed.

Code:
var alias = Game_Variables.prototype.setValue;

Game_Variables.prototype.setValue = function(variableId, value) {
    alias.call(this, variableId, value);
    // your code here
}
That's actually brilliant, but what if the external code is directly manipulating $gameVariables._data[variableId] instead of using setValue?
I just looked this up and discovered that there apparently is a way to assign custom getters for arrays (and other objects) in JavaScript, using a "Proxy":
So you might be able to use that? Note that Proxies were introduced in ES6, so older versions of RMMV (pre-1.6?) may not be compatible. 😴

That said, I think I'd personally just go with V_Aero's suggestion: the setValue stuff is there explicitly for this purpose, after all. 😅
I would go with setValue or use Pixijs Event Listeners
Thank you all for your answers so far! Took me some time to try the different approaches.

I believe the setValue approach is not applicable in my case because the code that sets the variable is not mine (it is external to my plugin) and it is directly using the private $gameVariables._data[variableId] = value; instead of using the setValue function. Too bad, I loved it 😉

So I tried the Proxy approach, but could not get my handler to be called.
Perhaps this is because a more fundamental concept that I don't yet fully understand in plugin development : Where in my plugin code it is most appropriate to add the handler definition and the " = new Proxy(...)" call? That place would need access to $gameVariables.

PixiJs Event listeners - I just looked at it briefly now. Can I really listen with it to changes in an array ($gameVariables._data in my case) or only to HTML events of sprites?
For the Proxy thing...I just quickly tested this code in a li'l plugin and it seems to work for me~
Spoiler: Game_Variables Proxy example (ES6+)
JavaScript:
(function() {
'use strict';

    let sayHi = function(id, val) { alert('Var %1 now has a value of %2!'.format(id, val)); };

    (function(alias) {
        Game_Variables.prototype.initialize = function() {
            alias.apply(this, arguments);
            this._data = new Proxy(this.__data = [], {
                set: function(obj, prop, value) {
                    sayHi(prop, value);
                    obj[prop] = value;
                }
            });
        };
    })(Game_Variables.prototype.initialize);

})();
I'm curious which plugins you're using that are bypassing setValue, though...I can't think of a good reason for doing so, since it'd ignore the associated onChange method necessary for updating appropriate things on the map, etc. 😅
Wow, that worked!!! I just did not have enough experience to combine the proxy in my code correctly so thanks a lot for the code snippet.

The plugin I am using is MOG Time system (the link is a huge .zip file of an entire demo project, but the plugins folder includes the actual MOG_TimeSystem.js.). See line 1153: Game_System.prototype.refresh_time. It's a recursive function that uses $gameVariables._data[parameter] += value;
Wow, that worked!!! I just did not have enough experience to combine the proxy in my code correctly so thanks a lot for the code snippet.

The plugin I am using is MOG Time system (the link is a huge .zip file of an entire demo project, but the plugins folder includes the actual MOG_TimeSystem.js.). See line 1153: Game_System.prototype.refresh_time. It's a recursive function that uses $gameVariables._data[parameter] += value;
You're welcome! 🙂

Ah, I see. It's still better to use the value and setValue methods to interact with variable values, I guess Mog was just being lazy when he wrote that, haha. 🙏

Anyway, happy RPG Making! 😂
There could possibly be reason to access the value directly if you're doing many updates in one tick frame, but then you should fire onChange manually...

Original thread: https://forums.rpgmakerweb.com/threads/subscribe-to-gamevariable-updates.123262/