I've recently run into an issue where I want to alias a method that (sort of) doesn't exist.
I'll explain by example. Consider
Now, I want to override the function only for actors, but in a way that another plugin wanting to do the same would still function.
The normal approach does not work correctly. For example:
The problem is far from obvious, but this will conflict with a plugin that does this:
If Plugin1 appears first in the plugin list, then you'll find that Plugin2 does not affect actors - it only affects enemies, because Plugin1 calls the original
Has anyone else come up with a general solution to this? I'm using something like this, which I think will work correctly, but I wondered if there was a better way:
Note: Obviously the specific code in these examples won't work (as states() needs to return the state objects, not their IDs). It's just for demonstration anyway.
I'll explain by example. Consider
Game_Actor.prototype.states. If you type it into the console, it exists, but there is no declaration for it in rpg_objects.js. This is because the function is inherited from Game_BattlerBase - you can confirm this in the console by checking Game_Actor.prototype.states == Game_BattlerBase.prototype.states.Now, I want to override the function only for actors, but in a way that another plugin wanting to do the same would still function.
The normal approach does not work correctly. For example:
Code:
// Let's call this Plugin1
var whatever1 = Game_Actor.prototype.states
Game_Actor.prototype.states = function() {
return whatever1.call(this).concat(4,5,6);
};
The problem is far from obvious, but this will conflict with a plugin that does this:
Code:
// Let's call this Plugin2
var whatever2 = Game_BattlerBase.prototype.states
Game_BattlerBase.prototype.states = function() {
return whatever2.call(this).concat(4,5,6);
};
If Plugin1 appears first in the plugin list, then you'll find that Plugin2 does not affect actors - it only affects enemies, because Plugin1 calls the original
Game_BattlerBase.prototype.states rather than the version overridden by Plugin2. It will work if Plugin2 appears first in the plugin list (since whatever1 would now be the function defined in Plugin2), but with more than 2 plugins it could occur that every possible arrangement breaks a different thing, so I don't want to rely on plugin ordering if I can avoid it.Has anyone else come up with a general solution to this? I'm using something like this, which I think will work correctly, but I wondered if there was a better way:
Code:
var whatever = Game_Actor.prototype.states
var is_overridden = Game_Actor.prototype.states != Game_Battler.prototype.states;
Game_Actor.prototype.states = function() {
return (is_overridden ? whatever : Game_Battler.prototype.states).call(this).concat(4,5,6);
};
Note: Obviously the specific code in these examples won't work (as states() needs to return the state objects, not their IDs). It's just for demonstration anyway.