MZ Custom Leader Plugin MZ would like review

I have been fiddling with plugin creation. Thanks to AutMouse Labs! I have noticed that people will often use "(() =>{code})();" in their code, but AutMouse doesn't. I am wondering about the pros and cons about using it. In any case I would love a review. It works and if it can pass a community review I will post it in the finished plugins section. I will post both as an attachment and a spoiler since it isn't very big.

If you're curious about some me background. I have been dabbling with rpg maker since the ps1, but never went full deep dive into it until this past couple of weeks. I do know Javascript and HTML5 fair amount, and a good bit of other languages.

Spoiler
///////////////////////////////////////////////////////////
// Woden's Custom Leader Plugin
///////////////////////////////////////////////////////////
/*:
* @target MZ
* @Author Woden Burison
* @description A plugin to set the party leader to someone other than a battler, like in Pokemon games.
*
* @param customLeader
* @text Custom Leader:
* @desc Pick a custom leader based on the actor ID.
* @type number
* @Default 1
*
* @Help
* Setting a custom party leader
*/
const WODEN_customLeader = {};
WODEN_customLeader.pluginName = "WODEN_customPartyLeader";
//getting params
WODEN_customLeader.parameters = PluginManager.parameters(
WODEN_customLeader.pluginName
);
WODEN_customLeader.customLeader = Number(
WODEN_customLeader.parameters["customLeader"] || 1
);
//override party leader function from rmmz_objects 5454
Game_Party.prototype.leader = function() {
let leader = WODEN_customLeader.customLeader;
return $gameActors.actor(leader);
};
The (() =>{code})() construct is called IIFE, which means that your code has some barrier to the outside code.

Every plugin creator should be careful when implementing their code so it works well with both the RPG Maker's core code and 3rd party plugins. Using an IIFE is one way to barrier your code. It's not always the best one, and an IIFE comes with pros and cons, and it may not magically solve all your problems; however, wrapping your code into it is probably the easiest way to have at least some safety net, and this is probably why so many devs are doing it this way.

That is a really short explanation because this topic is just too broad for a brief post. I found out that it takes some time and practice to fully understand this technique.

Looking at your plugin, it is already well-written, so an IIFE is not necessary (but it won't hurt either; the end result would be the same).

There's also an article that shows some cons of IIFEs: http://www.yanfly.moe/wiki/An_Argument_Against_Requiring_IIFEs

The only tip I have for you is that in the last function, you are using let instead of const.
The other common alternative to an IIFE is to create a property on the window.

So instead of
Code:
const WODEN_customLeader = {};
you could do something like
Code:
// Make a window object for all your plugins
window.WODEN ??= {};
// Then add this specific one to it
WODEN.customLeader ??= {};
// Add attributes as you like
WODEN.customLeader.version = 1.0;
WODEN.customLeader.parameters = PluginManager.parameters("WODEN_customPartyLeader");

If you make your customLeader parameter be type actor instead of type number, it does a few things. First, the user will get a dropdown list showing the actor's names, so they can see who they're choosing.

Second, it eliminates the need for your customLeader function that error-checks whether it's a valid actor - it has to be.

The other thing is a matter of style - it does not make your code run worse.

I am a proponent of not having "useless" variables - that is, a variable that's only used once and the value isn't manipulated at all. It may sometimes make a slightly longer line, but it's less typing and it's fewer lines of code.

You can see above where I changed your
Code:
WODEN_customLeader.pluginName = "WODEN_customPartyLeader";

WODEN_customLeader.parameters = PluginManager.parameters(
    WODEN_customLeader.pluginName
);
to
Code:
WODEN.customLeader.parameters = PluginManager.parameters("WODEN_customPartyLeader");
To me, that's easier to read because everything relevant is in one place. And since the plugin name is only ever being referenced in that one place, there's really no reason to have another global object floating around.

Similarly, I would change
Code:
Game_Party.prototype.leader = function() {
    let leader = WODEN_customLeader.customLeader;
    return $gameActors.actor(leader);
};
to
Code:
Game_Party.prototype.leader = function() {
    return $gameActors.actor(WODEN_customLeader.customLeader);
};

As a general rule, fewer lines of code means less stuff you have to read and check through when you're looking for errors.
The (() =>{code})() construct is called IIFE, which means that your code has some barrier to the outside code.

Every plugin creator should be careful when implementing their code so it works well with both the RPG Maker's core code and 3rd party plugins. Using an IIFE is one way to barrier your code. It's not always the best one, and an IIFE comes with pros and cons, and it may not magically solve all your problems; however, wrapping your code into it is probably the easiest way to have at least some safety net, and this is probably why so many devs are doing it this way.

That is a really short explanation because this topic is just too broad for a brief post. I found out that it takes some time and practice to fully understand this technique.

Looking at your plugin, it is already well-written, so an IIFE is not necessary (but it won't hurt either; the end result would be the same).

There's also an article that shows some cons of IIFEs: http://www.yanfly.moe/wiki/An_Argument_Against_Requiring_IIFEs

The only tip I have for you is that in the last function, you are using let instead of const.
I've read little about IIFE before my post. I tried it, but didn't know how to get rpg maker to read my code from inside it. I agree with Yanfly, especially when const and let, I see only special use cases for IIFE. I always try to remember to limit my variable scopes. Thanks for the article!

The other common alternative to an IIFE is to create a property on the window.

So instead of
Code:
const WODEN_customLeader = {};
you could do something like
Code:
// Make a window object for all your plugins
window.WODEN ??= {};
// Then add this specific one to it
WODEN.customLeader ??= {};
// Add attributes as you like
WODEN.customLeader.version = 1.0;
WODEN.customLeader.parameters = PluginManager.parameters("WODEN_customPartyLeader");

If you make your customLeader parameter be type actor instead of type number, it does a few things. First, the user will get a dropdown list showing the actor's names, so they can see who they're choosing.

Second, it eliminates the need for your customLeader function that error-checks whether it's a valid actor - it has to be.

The other thing is a matter of style - it does not make your code run worse.

I am a proponent of not having "useless" variables - that is, a variable that's only used once and the value isn't manipulated at all. It may sometimes make a slightly longer line, but it's less typing and it's fewer lines of code.

You can see above where I changed your
Code:
WODEN_customLeader.pluginName = "WODEN_customPartyLeader";

WODEN_customLeader.parameters = PluginManager.parameters(
    WODEN_customLeader.pluginName
);
to
Code:
WODEN.customLeader.parameters = PluginManager.parameters("WODEN_customPartyLeader");
To me, that's easier to read because everything relevant is in one place. And since the plugin name is only ever being referenced in that one place, there's really no reason to have another global object floating around.

Similarly, I would change
Code:
Game_Party.prototype.leader = function() {
    let leader = WODEN_customLeader.customLeader;
    return $gameActors.actor(leader);
};
to
Code:
Game_Party.prototype.leader = function() {
    return $gameActors.actor(WODEN_customLeader.customLeader);
};

As a general rule, fewer lines of code means less stuff you have to read and check through when you're looking for errors.

Very good point on the actor type thing. I like it and will try it.

On the style thing, I prefer the more lines because it offers better precision in pinpointing bugs, as my debugging skills are quite trash in comparison to the average programmer I think, I need all the help I can get (even after 20+ years I'm still a junior, lol). In your style I wouldn't know if the bug was from inside or outside the paren. In my style, sure the bug may pop up on both lines, but if it is both lines then I know more than likely the bug has to be the first. Sure fewer lines means fewer lines to look through, but in my experience I take way more time per line resulting in more time debugging than I otherwise would. So in that regards I do tend to segment more often. Thanks for the input though, I do like to hear/read about other styles to see if I can improve my own.

The other common alternative to an IIFE is to create a property on the window.

So instead of
Code:
const WODEN_customLeader = {};
you could do something like
Code:
// Make a window object for all your plugins
window.WODEN ??= {};
// Then add this specific one to it
WODEN.customLeader ??= {};
// Add attributes as you like
WODEN.customLeader.version = 1.0;
WODEN.customLeader.parameters = PluginManager.parameters("WODEN_customPartyLeader");

If you make your customLeader parameter be type actor instead of type number, it does a few things. First, the user will get a dropdown list showing the actor's names, so they can see who they're choosing.

Second, it eliminates the need for your customLeader function that error-checks whether it's a valid actor - it has to be.
Wait the dev will get a window of choice? But what if they haven't made those in the database yet? This is going for a custom leader like in monster tamers. I'm not getting the player to choose, but the dev to chose. Basically I'm replicating(ish) HimeWorks' custom party leader for mv
On the style thing, I prefer the more lines because it offers better precision in pinpointing bugs
To my logic, the more lines there are, the more potential points of failure. If you declare a variable and initialize it to a value, then misspell your variable someplace else (or pass it into a function as the wrong kind of argument or something), that's more you're trying to check than if you simply type that value in the one place it's being used.

In your style I wouldn't know if the bug was from inside or outside the paren.
I also don't know what that means. That line has one assignment, one function call containing one string. Whatever kind of error you got would tell you exactly what the problem is.

But, like I said - it's a matter of style and you can do what makes sense to you. I've just found over years of instruction it to be true for the majority of people that fewer extraneous variables leads to less confusion.

Wait the dev will get a window of choice? But what if they haven't made those in the database yet? This is going for a custom leader like in monster tamers. I'm not getting the player to choose, but the dev to chose.
I...don't know what you're asking.

In your existing method, the user can type any number they want into the plugin parameter, whether there's an actor with that ID or not. And it requires them to go back and forth between the Actors tab of the database and back to your plugin to remember which ID they want to use.

If you use the type actor that's supplied by MV, then you get a dropdown list of the actors in the database to choose from. You don't need to go back and forth between screens, and you can't choose an ID that doesn't have an actor.

That means, as I said before, you don't need to have error-checking in your plugin to see if the parameter is a valid actor ID.

Why would someone try to put an ID into a plugin parameter when they haven't made that actor yet?
I also don't know what that means. That line has one assignment, one function call containing one string. Whatever kind of error you got would tell you exactly what the problem is.
To me your one line is doing two things, not one. First its getting the info, Second its setting the leader. To me limiting the number of things per line is more important than limiting the lines, because for me debugging time is exponential per things to do on a line. For example if I have 3 things happening per line, it takes me 9 times longer to debug than just having 1 thing. My brain doesn't really count declaring a variable as a thing as its so extremely rare for me to mess up on them.

Its just the way my brain works. As far as miss spelling a variable, ever since autocomplete was a thing and I started using it, I have never had that problem, though on a rare occasion I have used the wrong variable, but with my naming scheme in my head that's a easy to spot and quick to fix. Though this particular piece isn't the greatest example of my definitely more than average variable declarations.

You may not like it, but other people styles are their own, and it doesn't make it wrong, just different. I have seen countless situations across life where someone did something one way, but it wasn't my way, but if the person did do it my way, it would take them twice, or more, as long and vice versa. That's simply just how the brain works, everyone is a little different.

This is why I haven't been saying your style is wrong in general, but it is wrong for me personally as it would cause more debugging time than usual. I have tried it. Its actually they way I started back in high school (roughly around 2000, I don't remember exactly when I started playing with code or what language for that matter). Always try new styles, but never ever believe someone if they say/imply that your style is objectively wrong, because the reality is that style is 100% subjective and never ever objective. Even when something is generally true, like your style, that doesn't mean its true 100% of the time.

If you use the type actor that's supplied by MV, then you get a dropdown list of the actors in the database to choose from. You don't need to go back and forth between screens, and you can't choose an ID that doesn't have an actor.
OH! ok that makes sense. The way I interpreted it before was you were describing the end player choosing the leader.
You may not like it, but other people styles are their own, and it doesn't make it wrong, just different.
Um...chill, dude.

I said zero times that you are wrong.

In fact, I did say
The other thing is a matter of style - it does not make your code run worse.
But, like I said - it's a matter of style and you can do what makes sense to you.

So I don't know what words you read or what you're ranting about, but - good luck with your project; I won't bother with any feedback in the future.
Um...chill, dude.

I said zero times that you are wrong.

In fact, I did say



So I don't know what words you read or what you're ranting about, but - good luck with your project; I won't bother with any feedback in the future.
Apologies. Your reply made it sound like to me that you were implying that fewer lines of code was objectively better.
For those who happen to read this. Is ATT_Turan one of those people that doesn't allow for apologies and corrections, then blocks you on the first bout wrong action? If so oh well, just want to know.
Double-post notice: @AgentWoden

Though, it’s also entirely possible that over the course of 45 minutes, that Turan has needed to tend to something else in his life.
Double-post notice: @AgentWoden

Though, it’s entirely possible that over the course of 45 minutes, Turan has needed to tend to something else in his life.
Ok got it on the double posting. Thanks for the correction.
For those who happen to read this. Is ATT_Turan one of those people...
I think this demonstrates a pretty poor attitude.

For one, I'm not obligated to respond to you just because you tagged me. For another, you should have no expectation that anyone's going to respond to you on a Web forum within 40 minutes.

Then to make it some public poll of my character, trying to guess whether I blocked you? That's completely unwarranted, given the amount of material I dumped into this thread trying to help you, and reeks of insecurity.

The answer is no, I didn't think you were worth blocking over that, but I will now so I don't have to see any more of this nonsense. You don't even have to ask anyone else's opinion 🤣
For those who happen to read this. Is ATT_Turan one of those people that doesn't allow for apologies and corrections, then blocks you on the first bout wrong action? If so oh well, just want to know.
What kind of drugs have you taken, and... do you mind sharing with me?
I think this demonstrates a pretty poor attitude.

For one, I'm not obligated to respond to you just because you tagged me. For another, you should have no expectation that anyone's going to respond to you on a Web forum within 40 minutes.

Then to make it some public poll of my character, trying to guess whether I blocked you? That's completely unwarranted, given the amount of material I dumped into this thread trying to help you, and reeks of insecurity.

The answer is no, I didn't think you were worth blocking over that, but I will now so I don't have to see any more of this nonsense. You don't even have to ask anyone else's opinion 🤣
I wasn't expecting a reply soon. I asked because you said.
I won't bother with any feedback in the future
I wasn't expecting any reply at all, which is why I ask others. Is it irrational to not expect a reply at all after you said you wouldn't? Sorry my autism is quite hefty and I don't understand these social norms.

You know I don't know if you will reply in the future because you said you would not, then you did, despite what you said.
What kind of drugs have you taken, and... do you mind sharing with me?
Unfortunately its autism. Can't be shared, caught, or cured.

crap forgot the double post thing when replying to multiple people
Unfortunately its autism. Can't be shared, caught, or cured.

I have autism too and I don't act like a raging crackhead towards someone who was just trying to help you. Don't pull out the Mental Illness Card when you get called out on bad behavior.

Especially since you keep trying to cause drama with Turan.
I have autism too and I don't act like a raging crackhead towards someone who was just trying to help you. Don't pull out the Mental Illness Card when you get called out on bad behavior.

Especially since you keep trying to cause drama with Turan.
You realize that autism is literally the most diverse condition in the world, right? One person's autism will definitely not look the same as another's. I'm not trying to cause drama and I apologized for interpreting his message in the wrong way.


From my point of view what happened was:

They provided two responses.
I positively reacted to the first.
I misinterpreted the second.
they respond to my misunderstanding
I misinterpret their message again and assume they were being negative.
They pointed out what they meant.
They tell me they wouldn't feedback anymore, which I assumed they wouldn't reply at all.
I apologized for my misunderstanding.
Due to believing they wouldn't reply at all, I asked others if this would be true. Sometimes it is and sometimes it isn't.
Then I get trashed on for attempting to straighten out the misunderstanding.
OOOOOOOOOOOOOK. Let’s talk about…whatever the thread used to be about?
Not going to overly unpack it, let’s just stop it. I guess block who all you need to. It’s healthy.

Original thread: https://forums.rpgmakerweb.com/threads/custom-leader-plugin-mz-would-like-review.172112/