Template for MV plugins - a possible recommendation

Oh, I forgot to mention, but I also approve of this template 🙂
I don't really follow all of that. After I see how Yami implements it (let me know when you've done one, Yami), I'll see about doing the same for mine.
Just noticed this part

of course, I suggest your classes extensions like "SpriteSomething" go inside your module:
What if someone else wants to extend my custom classes?


Would they have to also write the code in the same way that I write it? Would they have to pass in the same variable name that you passed into $ ?
Just noticed this part

What if someone else wants to extend my custom classes?

Would they have to also write the code in the same way that I write it? Would they have to pass in the same variable name that you passed into $ ?
Nope. When you pass your class to the anonymous function, everything that is done to the '$' variable inside it will be applied to your class. Then anybody can change anything on your class on any of those two ways:

1) Creating a new anonymous function and passing your class to it;

2) Changing your class directly.

Edit: Wait, I hadn't checked that part on the original post. If you declare a class inside an anonymous function, other people will only be able to change it if you export it somehow. On Ramiro's example, he did so in this line:

$.ChildSprite = ChildSprite;
This means that he added the ChildSprite class to whatever was passed to the anonymous function, creating a namespace. For example:

Code:
var MyNamespace = {}; (function($){  function ChildSprite() {    // blah blah blah  };  $.ChildSprite = ChildSprite;}(MyNamespace); var something = MyNamespace.ChildSprite();
It sounds like we effectively can achieve encapsulation and force other scripters to access our data only through the interface we provide (rather than accessing it directly).


In order to add new methods to that class, I'd have to go into his namespace.


Which isn't that big of a deal: just copy-paste the outline and then you're ready to add new stuff.
That is the module pattern for JavaScript. There are other ways to do the modules: CommonJS (Browserify, require in Node.js...) and AMD (RequireJS,...)

I like Browserify too since it allows people to *require* the module easily, but people will find it confusing when look @ final build (most of users don't seem to interest in source code before build)

AMD is really a good way to do the modular pattern, and is used widely for front-end development (look @ AngularJS).
Actually using scopeing this way is probably the easiest, if you want to allow every function of your interface to be modified, just put them with underscores if the method is meant to be private:

function ($) { function _myPrivateUtility() { }; function AClass() { $._myPrivateUtility(); // notice how we still use $._privateMethod; }; AClass.prototype._privateMethod = function () { }; $.AClass = AClass; $._myPrivateUtility = _myPrivateUtility; // is public, but "special and considered private by meaning"})(MyModule);This is used on many, many places.

We are not using jquery or angular or anything here, so i think than "normal scoping" would be good.

Also making an API for people using your scripts is actually a good idea, because you can control access.

This also happens on engines like PIXI.js where "private variables and methods" starts with _, like _textureMap or _bounds.

Of course this is just a way to do it, and not always you must scope your code.

If you are modifiying existing classes from the RPG Maker you don't need an scope.

Sprite.prototype.addedMethod = function () {   // this can go without scoping after all...}Of course if the maker allows me to actually use scoping like node (wich I don't see as a possibility...) it would be great and this actually would be nonsensical.

But using scope will also transfer some meaning to your code, imagine yanfly made all inside YEP.

an extension to a YEP class can be:

YEP.SpriteEx.prototype.myAddedMethod = function () {   // again no scope needed, };If you actually extend many classes on YEP, and if you are lazy than myself, you can of course:

// Not needed, but this may transfer the idea of "I am extending YEP here" (function ($) {   $.ext1 = //...;   $.ext2 = //...;   //...})(YEP);But as I said, it is just an idea and not needed, only it is quite clear your intention doing it this way.

I'm not meant to "make your classes private to others" just:

Make sure your classes stand inside your space, that way people know than you are using someone's space to do that or this, wich will in turn help you on plugin dependencies because if they see something like:

YEP.SpriteActor.prototype.myExtension = function () {};This means I'm extending YEP's SpriteActor, this means I need probably the module YEP and the module YEP SpriteActors.

Another thing this will provide is than if you somehow create the same class as other like:

var Game_AI = function () { ... };Now imagine I made an AI for Scene_Battle, and put that name, and now someone does the same for events...

Yes, this is where script incompatibility appears, doing

MyModule.Game_AI = function () {};WIll allow both scripts to work together because you know than YEP is used by yanfly as a namespace so I will use another, people will know I'm using D$E so they won't use it as their module, and so on...

I don't recommend using less than 3 letters as namespace for that purpose, for example, your scripts can go all inside TMV (Tsukihime MV), yami can go YEM (Yami Engine Magic) or how people actually would like to call 'em. Of course TE (Tsukihime engine will work 😛  but you know, yanfly is using 3 letters, yami probably too, etc...) You can also call your namespace AReallyLongLongNamespaceForTheLoveOsGod, but it's not too productive...

For another example, I'm using D$E (Dragon Engine as I said I would call it this time) as my namespace, I  am doing some mundane utilities than parsing notetags on the core so I don't need MV to make a plugin like that for now...)

You know, calling your engine $ and _ would be great, but those names are know as jQuery and Underscore, so you are a bit late from using them 😛 .

This idea is to use your modules for more than just constants (as we used to do in ruby) to avoid even more conflicts.

at least if you are not using the idea of scoping, do a favor and put the:

var Imported = Imported || {};Imported["MyScript"] = true;At minimun, this is the only "required" part, but the other makes some sense.

At least if you put all your classes on the "window" (just look at this D:, and that is without counting all the classes like Object, Array, String, Number, all of the DOM objects, the standard functions, aliases to some functions, etc) we can check your script is there and try to avoid incompatibilities.

If someone actually wants the idea of evading scoping to make incompatibilities (like some (in)famous people on ACE...) I will make the script they made with scope and calling the same name plus "Plus" to piss 'em off to trying to piss other off xD

Anyway, I have and give my reasons to do this because I think in the long term it will improve code quality and will make us easy to expand scripts, but just for that.


TL; DR:

Scoping is nicer than not using it, and we are not using node.js nor angular or jquery, so why not using  it if it helps people ? (at least form me)
I love scope too (or self-invoking anonymous function). At least I can extend existed methods (alias things in Ruby) without using such a long complicated name.
Why not use the multiline comment for the top part?
Hmm, for some reason RGSS never did. They could easily have done:

=beginCommentCommentComment=endBut instead went with

Code:
#===================# comment# comment# comment#===================
Which, thinking about it, wastes a lot of characters. Not that that's a problem in old RPG Makers, but since our games are probably going to be distributed via a browser, every little efficiency helps.My web programming professor would have had a right proper rant, but then he'd go as far as limiting how much blank space you use and such.
Which, thinking about it, wastes a lot of characters. Not that that's a problem in old RPG Makers, but since our games are probably going to be distributed via a browser, every little efficiency helps.


My web programming professor would have had a right proper rant, but then he'd go as far as limiting how much blank space you use and such.
I don't think filesize efficiency should be done by scripters.


These are things gamedevs should be doing on their end when they push their games to production.


I'd rather run a minifier or something to get rid of the extra fat afterwards.
Like Tsukihime said, it's better to just run a minifer afterwards and instead make the source scripts as readable as possible. Filesize is irrelevant all the way up to production, and a minifier will always be far more efficient than manually limiting your characters used.
Aye. Thinking about it, there's no reason for a published game to have comments in it at all.
function Game_AI() { this.initialize.apply(this,arguments);}Game_AI.prototype.initialize = function(ai_id) { this._id = ai_id; //wanted to call this just "id" but for some reason it's call a keyword in sublime >_>" this.personality = AI_Personality[this._id]; this.interaction_type = "normal"; this.check_message_type(); this.retrieve_dialogue_and_voices(); this.create_sprite();};Game_AI.prototype.check_message_type = function() {};Game_AI.prototype.retrieve_dialogue_and_voices = function() {};
What's the advantage in putting all the initialization stuff in a seperate method on the prototype, rather than just including it directly in the constructor? Is it just so you can call it later on an object to "reinitialize"?
I dont personally agree with being forced to use or even using a template as I found the way people did comments back in the old (still current) days of RGSS to be a night mare. The code with there:

//----------------------------------------------------------------------------// ○ new function: initialize//----------------------------------------------------------------------------Was horrible and hard to read. Some people had it down right where they knew what they were doing and used these types of comments sparingly, others had massive blocks of these types of comments at the top of there "scripts"

Personally I like, as others have pointed out - the more professional and more standard way of comments as seen in the below example:

/** * Short Description * * Slightly longer description */var ClassName = { /** * Description * * @return ... */ functionName: function() { ... }, /** * Description * * @param Sting|Int|Boolean - What I do ... * @return ... */ functionNametwo: function(param) { ... }};Much more readable ... 
More readable?

Not necessarily for an end user who is not a scripter. And if a script is made public, such a user needs to be considered, or why bother making it public?
Personally I like, as others have pointed out - the more professional and more standard way of comments as seen in the below example:

/** * Short Description * * Slightly longer description */var ClassName = { /** * Description * * @return ... */ functionName: function() { ... }, /** * Description * * @param Sting|Int|Boolean - What I do ... * @return ... */ functionNametwo: function(param) { ... }};Much more readable ...
I've never liked that style. It feels like it's written for machines rather than for humans.

//----------------------------------------------------------------------------// some description for humans to read//----------------------------------------------------------------------------This provides a clear section break that will indicate that whatever above this line is separate from whatever is below it.I would prefer seeing that over

Code:
/* * Some description for humans to read */
@ksjp17, but plugins are designed so non-scripter end-users CAN use them without having understand the code, or even SEE the code.


So in theory, the only people who should be looking at that are either programmers, or people who actually WANT to get in and look at what's going on behind the scenes.
What's the advantage in putting all the initialization stuff in a seperate method on the prototype, rather than just including it directly in the constructor? Is it just so you can call it later on an object to "reinitialize"?
it's a preference taste actually xD I do prefer doing that in this way

I dont personally agree with being forced to use or even using a template as I found the way people did comments back in the old (still current) days of RGSS to be a night mare. The code with there:

//----------------------------------------------------------------------------// ○ new function: initialize//----------------------------------------------------------------------------Was horrible and hard to read. Some people had it down right where they knew what they were doing and used these types of comments sparingly, others had massive blocks of these types of comments at the top of there "scripts"

Personally I like, as others have pointed out - the more professional and more standard way of comments as seen in the below example:

/** * Short Description * * Slightly longer description */var ClassName = { /** * Description * * @return ... */ functionName: function() { ... }, /** * Description * * @param Sting|Int|Boolean - What I do ... * @return ... */ functionNametwo: function(param) { ... }};Much more readable ... 
I can understand you maybe not likes the ways of certain of doing thing's but can you stay either polite and not insult the other ways of coding? it's ridiculous 

and it's getting a little on the patience nerve of people  mine specially 

you will have to know  to turn you tongue more than 7th times because now you getting more than annoying and without respect to other people works

I don't care you are those damn JS expert or any kind master of somewhat Learn either to respect the other people works and their ways to works or just shut up

I am sorry for the mods but I getting to much pissed off of the relevant arrogance of this guy

at this level I don't care to get a warn for anything's but my patience also have limits
@Shaz

Not having seen how these plug-ins work, I'm operating in the dark.  But do they mean that an end user never has to go in and do any configuring?

I'm thinking of such existing scripts as

Quest Journals, where each quest has to be carefully defined in terms of objectives etc.  

Lighting scripts which each 'light' has to be defined

More tricky ones like Iavra's Animate Everything script where the user has to define any additional classes.
Have you seen the videos with plugin previews that have been getting posted for the last few days? If not, go take a look at them. The end user will have to copy the .js file into the plugins folder, and that's the only time they'll see the code. To activate it, they select the plugin from the list in the plugin manager. Then they have a UI that contains anything that can be configured, with a name, description and default value. They do not have to touch the code at all.


There may well be plugins that are more like scripts, where the user has to go and add big lists of stuff. They would be a bit difficult to do in the UI because it doesn't allow multiple values or lists (unless the scripter took a single parameter and converted it to multiples or a list, but that's going to be confusing and ugly). But they would require more than basic commenting anyway.

Original thread: https://forums.rpgmakerweb.com/threads/template-for-mv-plugins-a-possible-recommendation.45521/