When it comes to me writing my own classes and functions I follow JS conventions. When it comes to messing with RMMV core functions and classes I follow there standards. I try and subclass there stuff as to not break anything.
Share the way you Alias functions
Learning JavascriptWhen it comes to me writing my own classes and functions I follow JS conventions. When it comes to messing with RMMV core functions and classes I follow there standards. I try and subclass there stuff as to not break anything.
I would like to suggest another method, that is both descriptive and normalized. It requires the function extend() to be available, either through your own code or some "core" plugin. It uses apply() instead of call() so it also works with Game_Interpreter.pluginCommand(), which expects arguments.
The added value of this approach is readability and normalization. The "previous" method is always called before your plugins' one, because your plugin extends upon the current functionality and the order matters.
If you want to make the "previous" implementation publicly available (which may be necessary for compatibility fixes between plugins?), adjust the extend() function accordingly.
Thoughts, anyone?
The added value of this approach is readability and normalization. The "previous" method is always called before your plugins' one, because your plugin extends upon the current functionality and the order matters.
If you want to make the "previous" implementation publicly available (which may be necessary for compatibility fixes between plugins?), adjust the extend() function accordingly.
Thoughts, anyone?
Code:
(function () { var extend = function (module, action, fn) { var previous = window[module].prototype[action]; window[module].prototype[action] = function () { var args = Array.prototype.slice.call(arguments); previous.apply(this, args); fn.apply(this, args); }; }; extend('Game_Player', 'moveByInput', function () { // ... }); extend('Game_Interpreter', 'pluginCommand', function (command, args) { if (command === 'test') { console.log('okay!', args); } });})();
I personally would not use this as I write in ES6 but in terms of es5 this looks hackish and harder to use them properly aliasing methods and so on.
Just my thoughts
Are you sure you know what's going on here? Why do you feel this is "hackish" and harder to use? If one thing, it's meant to be easier to use and read. The declaration of extend() is slightly more complex, but only required once.
What if you want to run your code before the aliased method, instead? Also, this uses 2 calls to apply(), which is a performance loss, doesn't work for namespaced classes or functions and slices the arguments object, which is stated to be a bad thing on the MDN reference: https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Functions/arguments
Apply()ing two functions instead of one is hardly a performance hit. Don't worry about micro optimizations until they are necessary. And we are a long from them being necessary.
Please supply an example of a situation where you would want your plugins' code to run before the original implementation. Don't you agree that your plugin expands upon existing functionality and that, thus, your code should run after the original?
You're right about what MDN states about V8 engines not being able to optimize arguments slicing. That can easily be changed, no problem.
Please supply an example of a situation where you would want your plugins' code to run before the original implementation. Don't you agree that your plugin expands upon existing functionality and that, thus, your code should run after the original?
You're right about what MDN states about V8 engines not being able to optimize arguments slicing. That can easily be changed, no problem.
In my achievement plugin i have this:
(function($) { /** * Return the path to our own save file, if our savefile id is supplied. */ var alias_localFilePath = $.localFilePath; $.localFilePath = function(savefileId) { if(savefileId === _savefileId) { return this.localFileDirectoryPath() + 'achievements.rpgsave'; } return alias_localFilePath.call(this, savefileId); }; /** * Return the key to our local storage entry, if our savefile id is supplied. */ var alias_webStorageKey = $.webStorageKey; $.webStorageKey = function(savefileId) { if(savefileId === _savefileId) { return 'RPG Achievements'; } return alias_webStorageKey.call(this, savefileId); }; })(StorageManager);Here i want to redirect the StorageManager to a specific file or web storage key, if my savefile id was supplied. This won't work, if i run my code after the original function.Also, you ignored this part:
which doesn't work.
(function($) { /** * Return the path to our own save file, if our savefile id is supplied. */ var alias_localFilePath = $.localFilePath; $.localFilePath = function(savefileId) { if(savefileId === _savefileId) { return this.localFileDirectoryPath() + 'achievements.rpgsave'; } return alias_localFilePath.call(this, savefileId); }; /** * Return the key to our local storage entry, if our savefile id is supplied. */ var alias_webStorageKey = $.webStorageKey; $.webStorageKey = function(savefileId) { if(savefileId === _savefileId) { return 'RPG Achievements'; } return alias_webStorageKey.call(this, savefileId); }; })(StorageManager);Here i want to redirect the StorageManager to a specific file or web storage key, if my savefile id was supplied. This won't work, if i run my code after the original function.Also, you ignored this part:
If my class is named "IAVRA.MyClass", you code would try
Code:
window["IAVRA.MyClass"]
Okay, both are valid points indeed. Ofcourse the function I suggested can be adjusted to support these cases. Would you care to make these adjustments?
No, because i wouldn't use it. In the end, you are saving maybe 1 line of code, but have to add a whole new function, that needs to be complex enough to allow for different aliasing variations and is either contained in every single plugin, or in MVCommons (or similar) and adds an additional dependency.
I must say that you showed me some cases where this approach is not desirable at all, such as the one where you return early under special circumstances. That, along with the question whether to execute before or after current logic, is reason enough for me to drop the idea 🙂
The benefit is simply too little.
The benefit is simply too little.
I want to use your styles, Sulsay and Iavra, but I have not yet able to understand how to use them. Probably, but could you explain line per line what the codes mean? So I would understand it better? Reading complex explanations from sites doesn't help. Most of the terminologies are confusing.
It's actually pretty easy. I'll explain in comments:
(function($) { // Take the class name as function parameter. var alias_fn = $.prototype.fn; // Store a reference to the original function. $.prototype.fn = function() { // Define a new function with the same name. alias_fn.call(this); // Call the stored function. // Do your stuff. }; })(SomeClass); // The class name given to the anonymous function.I wrap the aliasing in an anonymous function, because it allows me to reference the classname as "$" (or anything else) and not by its real (maybe long) name.You need to consider, that everything in JavaScript, that's not a primitive value (String, Number, Boolean) is actually a reference to the real object in memory. So, when i define "alias_fn", i create a new reference to the existing function, which would otherwise be destroyed, when i overwrite the existing reference to it one line later. This way, the original function still exist and i can call it from the new one.
Using "call(this)" passes the current class instance to the function as "this" value, so if the function says "this.x = ...", the "this" will point to the correct object and not to the anonymous function i created the reference in.
(function($) { // Take the class name as function parameter. var alias_fn = $.prototype.fn; // Store a reference to the original function. $.prototype.fn = function() { // Define a new function with the same name. alias_fn.call(this); // Call the stored function. // Do your stuff. }; })(SomeClass); // The class name given to the anonymous function.I wrap the aliasing in an anonymous function, because it allows me to reference the classname as "$" (or anything else) and not by its real (maybe long) name.You need to consider, that everything in JavaScript, that's not a primitive value (String, Number, Boolean) is actually a reference to the real object in memory. So, when i define "alias_fn", i create a new reference to the existing function, which would otherwise be destroyed, when i overwrite the existing reference to it one line later. This way, the original function still exist and i can call it from the new one.
Using "call(this)" passes the current class instance to the function as "this" value, so if the function says "this.x = ...", the "this" will point to the correct object and not to the anonymous function i created the reference in.
This looks amazing. I would have never thought to do this
Trying to see if I'm understanding things correctly. Is this the least intrusive way to add my own functionality to Scene_Base.update?
(function(){ (function($) { var aliased_update = $.prototype.update $.prototype.update = function() { console.log ('additions to Scene_Base update') aliased_update.call(this) } })(Scene_Base);})()I've seen lots of plugins that have "Imported" related things at the beginning of their plugin (written like below?). I don't know what's the purpose of it, though. If other people were to make their own additions to Scene_Map.update, wouldn't it already always include the code I've added?
(function(){ (function($) { var aliased_update = $.prototype.update $.prototype.update = function() { console.log ('additions to Scene_Base update') aliased_update.call(this) } })(Scene_Base);})()I've seen lots of plugins that have "Imported" related things at the beginning of their plugin (written like below?). I don't know what's the purpose of it, though. If other people were to make their own additions to Scene_Map.update, wouldn't it already always include the code I've added?
Code:
var Imported = Imported || {};var EC = EC || {};Imported.ExamplePlugin = true;EC.ExamplePlugin = EC.ExamplePlugin || {};(function($){ (function($) { var aliased_update = $.prototype.update $.prototype.update = function() { console.log ('additions to Scene_Base update') aliased_update.call(this) } })(Scene_Base);})(EC.ExamplePlugin)
Using a variable named "Imported" is just the way, plugins were "registered" in VX Ace (earlier RPG Maker version), so other plugins could check whether their dependencies exist. As long as your plugin has a public API ("EX.ExamplePlugin" in your case) this isn't really necessary, since you can simply check for the existence of that variable, instead.
It is, however, useful if you store the plugin version inside the Imported variable, instead of a simple "true", so dependent plugins can check for the correct version.
It is, however, useful if you store the plugin version inside the Imported variable, instead of a simple "true", so dependent plugins can check for the correct version.
I'm not sure how I feel about the whole let's wrap anonymous functions inside each other.
Seems like it could make the code harder to read and harder to understand
Seems like it could make the code harder to read and harder to understand
Oh, so that isn't the standard practice. I'm still not exactly sure why I see plugins wrapped in anonymous functions at all.
Well, the outer anonymous function should be standard, since it prevents your variables from leaking into the public scope and ensures, that noone can access your private stuff (OO principle of encapsulation).
Everything beyond that isn't needed and more personal preference.
Everything beyond that isn't needed and more personal preference.
When I write es6 everything's compiled down and placed in side an Anonymous function, thank god for that, but looking back at the trans piled source there is no concept of nesting Anonymous functions.
Nesting them just seems like a horrible and unreadable way of things IMO.
Nesting them just seems like a horrible and unreadable way of things IMO.
I'm stopping the whole anonymous-function thing in my plugins. It's:
1. Generally annoying.
2. Not great for patching scripts indeed.
3. Your variables should not 'leak' to other scripts anyway and if you happen to make a global variable, at least make it very unique.
4. Does not really seem to serve much purpose when not using global variables.
5. Encapsulation in JavaScript... Yeah... Maybe give us private/internal/etc access modifiers instead of this 'anonymous garbage'. And maybe add some normal ways to create classes instead of the current mess. But hey, a billion dollars would be nice too. Basically I really don't care about encapsulation in a language that doesn't really support it anyway.
How I alias:
var alias_silv<name_of_class (if any)>_<name_of_method>
Example:
1. Generally annoying.
2. Not great for patching scripts indeed.
3. Your variables should not 'leak' to other scripts anyway and if you happen to make a global variable, at least make it very unique.
4. Does not really seem to serve much purpose when not using global variables.
5. Encapsulation in JavaScript... Yeah... Maybe give us private/internal/etc access modifiers instead of this 'anonymous garbage'. And maybe add some normal ways to create classes instead of the current mess. But hey, a billion dollars would be nice too. Basically I really don't care about encapsulation in a language that doesn't really support it anyway.
How I alias:
var alias_silv<name_of_class (if any)>_<name_of_method>
Example:
Code:
var alias_silv_skillGem_Scene_Window_Base_drawFace = Window_Base.prototype.drawFace;Window_Base.prototype.drawFace = function(faceName, faceIndex, x, y, width, height){ //alias_silv_skillGem_Scene_Window_Base_drawFace.call(this, faceName, faceIndex, x, y, width, height); alias_silv_skillGem_Scene_Window_Base_drawFace.apply(this, arguments); // .call(this, arguments) doesn't work properly but it works with .apply() bitmap.addLoadListener(this.refresh);};
Original thread: https://forums.rpgmakerweb.com/threads/share-the-way-you-alias-functions.52058/