RMMZ Common names conflict. Adding functions to the (built-in JS default) prototypes

TheoAllen

Self-proclaimed jack of all trades
Veteran
Joined
Mar 16, 2012
Messages
6,983
Reaction score
9,646
First Language
Indonesian
Primarily Uses
N/A
Context:
FD2BjCHVQAArul9


I'm mainly posting this to raise awareness. Some of you might want to add helper functions to the built-in JS prototypes. Heck, or even the RM prototypes (Game_Battler.prototype, Game_Player.prototype, etc). But while it is nice to have a function so that you can do something like [1,2,3,4,5,6].shuffle(), there is a chance that other plugin devs have the same thought.

In this particular case, we're talking about the shuffle function for Array. While the goal remains the same (shuffle the array content), different dev probably thought of a different thing. For example:
  • Should the function return undefined? (i.e, it modified the contents of the array but the function doesn't return anything)
  • Should the function return the modified array itself? (i.e, you can do something like arr.shuffle().shift())
  • Should the function return a modified copy of the array?
To be fair, while writing the code I totally expect this conflict could actually happen. Although I was optimistic that it probably wouldn't break. It totally would.

Fortunately, to avoid such a thing, it is not that hard. Although it is probably not as pretty as it could be. For example, if I were going to make a shuffle function to an array. I could do it this way.
JavaScript:
shuffleArr = function() { ... }
a = [1,2,3,4,5,6]
shuffleArr.call(a)
Instead of
JavaScript:
Array.prototype.shuffle = function() { ... }
a = [1,2,3,4,5,6]
a.shuffle()
This is probably why core engines have so many compatibility problems as they add (or modify) too many common function/variable names to the prototypes (this is the important part because you can avoid it if you are making an IIFE scoped function than attaching it to the prototype).

Thought?
 

MushroomCake28

KAMO Studio
Global Mod
Joined
Nov 18, 2015
Messages
4,099
Reaction score
4,972
First Language
EN, FR
Primarily Uses
RMMZ
Actually a neat idea. It's true that most people in this community add/modify functions directly in the prototype. Personally, to avoid those kind of troubles, I just add something at the beginning of the function so it's less likely to have any conflict. Like
Code:
Game_Battler.prototype.mush_paramAbility

Sometimes I also just add random numbers at the end, like:
Code:
Game_Battler.prototype.berserkMode_213
I just type random numbers at the end. Since when you type, most IDE will fill in the rest of the function before you finish typing, it doesn't cause any trouble when coding.

Of course, I only do that for plugins I share. Most of my plugins are for personal usage so I just don't bother doing any of that.
 

caethyril

^_^
Global Mod
Joined
Feb 21, 2018
Messages
4,104
Reaction score
3,163
First Language
EN
Primarily Uses
RMMZ
Some options:
  1. A separate function on the plugin namespace, e.g.

    $.shuffle(array)
    I tend to use this for internal functions.

  2. A new property with a user-configurable identifier, e.g.

    array[PluginManager.parameters['function name: shuffle']]
    I tend to use this mostly for user-facing methods/properties (script call stuff).

  3. A new property with a symbolic identifier (ES6+), e.g.

    array[$.SYM.shuffle]()
    Good for instanced properties! More info:
 

TheoAllen

Self-proclaimed jack of all trades
Veteran
Joined
Mar 16, 2012
Messages
6,983
Reaction score
9,646
First Language
Indonesian
Primarily Uses
N/A
The symbol identifier seems interesting. I might want to explore more on it.
 

subengari

Veteran
Veteran
Joined
Dec 12, 2020
Messages
36
Reaction score
14
First Language
king's english
Primarily Uses
RMMV
Another suggestion, but I'm not sure if it's halal- I've been adding an if clause before appending default prototypes with my own functions.

ecils.png
 

TheoAllen

Self-proclaimed jack of all trades
Veteran
Joined
Mar 16, 2012
Messages
6,983
Reaction score
9,646
First Language
Indonesian
Primarily Uses
N/A
@subengari it is not a good idea, because if the other plugins already defined the function and it didn't behave the way you wanted, your plugin is gonna break.
 

Latest Threads

Latest Profile Posts

Can Elon crash Twitter faster? I want something as big to replace it so I can promote my game there. I also need to actually do promotion this time, heh.
Thinking of getting a mechanical keyboard, eventually! Would anyone be interested in a chill keycap-painting stream?
Walked through the rain to take pictures of an 50+ year old printing press for my students. Hope they will honor that effort xD
Can I promote my %75 OFF Creator Day Sale here, or it's against forum rules as well? :kaocry:
Tests for an alert system that spawns enemy reinforcements, as a stealth game should do :kaoswt2:

Forum statistics

Threads
126,910
Messages
1,182,257
Members
166,715
Latest member
IcarusCursed
Top