MV [SOLVED] Help with specific request involving YEP Enemy Levels

Is there a way to have it so that when a specific troop battle starts that all enemies have their levels changed to match that of a designated enemy that is also in said battle?



Basically say there is Enemy A, B, C, & D in a battle

Enemy A has their level set for them automatically by YEP Enemy Levels like normal, meanwhile Enemies B, C, & D then have their levels CHANGED to be the EXACT same level that was assigned to Enemy A. That is what im looking for if that makes sense?
I'm using the same plugin, not for this, but I am familiar enough with it to figure this out. I found that the plugin added _level in $gameTroop.

How to figure this out:
If you press F8 to open the console in battle, you can type $gameTroop to give you a ton of information on the enemy troop you're fighting. If you open the arrows you can read all the enemy data, this lead me to:
Code:
$gameTroop._enemies[1]._level

For my tests, the result is $gameTroop._enemies[1]._level = 9 because that's the current level of the enemy I'm fighting.

Yanfly has included the command enemy.changeLevel(x). Anyway, I'm going to vent my frustrations with how the community handles it's documentation on their own plugins because this is a clear example where you "should just know" what Yanfly means by this with no clear example on how to use it.

How to use it:
To use this for your needs, you would call this script:
Code:
$gameTroop._enemies[1].changeLevel($gameTroop._enemies[2]._level)
This will change Enemy 1 in the troop's level to whatever Enemy 2's level is. This doesn't really correlate with "enemy.changelevel" that we find in the documentation, which is why I'm explaining it the way I am.

How we make it work:
• In the troops section of the menu, select the troop you want to be effected by this.
• On the bottom right in battle events, create a new event page, unless this is the default page. RPG Maker reads the last page first, and we need this to run at the start of the battle.
• Click on the conditions setting and set it to turn 0 + 0.
• Make sure Span is set to Battle, because we only want it to ever run once.
• Add an event, go to page 3 and select script and input:
Code:
$gameTroop._enemies[2].changeLevel($gameTroop._enemies[1]._level)
$gameTroop._enemies[3].changeLevel($gameTroop._enemies[1]._level)
$gameTroop._enemies[4].changeLevel($gameTroop._enemies[1]._level)

Now, as soon as the fight start on turn 0 (before your attack menu opens) it will set enemies 2,3,4 (B,C,D in your example) to the level of Enemy 1 (A in your example).
@Khali You have the right idea, but some improvements can be made to the code you provided.

I found that the plugin added _level in $gameTroop.
I'm not sure why you needed to resort to the console for this - the first thing listed in the Script Calls section of the plugin documentation is the level property. As a general rule, you should avoid referencing any properties starting with an underscore. Those are intended to be accessed by getter functions or other properties in order to avoid conflicts with other code.

There are some that don't have any other way to access, or times you deliberately want to circumvent the normal getting code, but you shouldn't do that unless you know the difference.

For example:
For my tests, the result is $gameTroop._enemies[1]._level = 9
You should always use $gameTroop.members() and not access the _enemies property directly - again, to avoid errors. There's also another small mistake here I'll address below.

Yanfly has included the command enemy.changeLevel(x). Anyway, I'm going to vent my frustrations with how the community handles it's documentation on their own plugins because this is a clear example where you "should just know" what Yanfly means by this with no clear example on how to use it.
Can you explain what you mean? The only part of this that doesn't seem immediately clear is that you need to know how to provide a reference to an "enemy" - because there are so many ways you might do that, it does seem reasonable to me to expect the user to know or learn it, and it would be outside of the scope of the plugin documentation.

You might reference it as a member of the $gameTroop as in this example, you could be using b in a damage formula, target in a skill notetag, etc.

• Add an event, go to page 3 and select script and input:
Code:
$gameTroop._enemies[2].changeLevel($gameTroop._enemies[1]._level)
$gameTroop._enemies[3].changeLevel($gameTroop._enemies[1]._level)
$gameTroop._enemies[4].changeLevel($gameTroop._enemies[1]._level)

Now, as soon as the fight start on turn 0 (before your attack menu opens) it will set enemies 2,3,4 (B,C,D in your example) to the level of Enemy 1 (A in your example).
This is not correct. Arrays - including the list of enemies in a troop - start with 0, not 1. So a troop with 4 enemies in it would have indices of 0, 1, 2, and 3, not 1, 2, 3, and 4.

Accounting for that, as well as using the correct getter methods I mentioned above, a more accurate version would be
Code:
$gameTroop.members()[1].changeLevel($gameTroop.members()[0].level)
etc.

However, all of the lines can be condensed into one single-line JavaScript array method, which can then be copy-pasted into any troop (or used as a base troop event if desired) to work with any number of enemies.
Code:
$gameTroop.members().forEach(enemy => enemy.changeLevel($gameTroop.members()[0].level));
I was writing this at like 2 am while working on a plugin for something else. The array was an oversight on my part, I do realize it starts at 0, I kind of rushed that and that's my bad.

The only part of this that doesn't seem immediately clear is that you need to know how to provide a reference to an "enemy" - because there are so many ways you might do that, it does seem reasonable to me to expect the user to know or learn it, and it would be outside of the scope of the plugin documentation.
I so strongly disagree with this because there are so many plugins that I run into that are like this. The documentation takes the time to explain that you can change the entire group, but why not explain how to change one member with a clear example? Especially when it was a simple copy and paste?

Try to see this from someone's perspective that's trying to learn what all of the stock (and additional plugin) functions do. How are we supposed to know to use $gameTroop.members()[0], and what is the difference from using $gameTroop._enemies[0]? I'm genuinely asking because while I realize members() is a getter function, MV library states that both return array<Game_Enemy>. Diving into Game Troop also reveals:
Code:
Game_Troop.prototype.members = function() {
    return this._enemies;
};
As far as I can tell, it doesn't do anything special? I can understand this in C# where a var or bool may be private and you need to provide a public getter and setter access to it to be able to adjust it, but that isn't the case here.

An example of how I got here: I needed that information quickly last night, I type in $gameTroop to find it, there is only a reference to $gameTroop._enemies to edit this data. I didn't even know that function existed until you mentioned it, then went back to Yanfly's plugin page where it's listed, but again it in no way tells me that it can be used this way (outside of plugin commands) which was why I disregarded it. It just says enemy.changelevel. What does this mean? It doesn't mean anything. I can script call "enemy.changelevel 1" and nothing will happen. If I didn't have previous coding knowledge, how do I know how to select Enemy 1, 2, or 3? It doesn't tell me. lol

Sure, I can use $gameTroop.members()[0].changeLevel = x to change that value, but the documentation doesn't say that anywhere. Is this even the intended way to do it?

Code:
$gameTroop.members().forEach(enemy => enemy.changeLevel($gameTroop.members()[0].level));
This is absolutely the best way to do this, but I couldn't be bothered last night to figure out how to use a foreach effectively here, and in a lot of my cases, I only want to change 1 enemy or not the entire party, so my thought process was that it would be easier to just adjust them all individually as someone might require them.
@Khali You have the right idea, but some improvements can be made to the code you provided.


I'm not sure why you needed to resort to the console for this - the first thing listed in the Script Calls section of the plugin documentation is the level property. As a general rule, you should avoid referencing any properties starting with an underscore. Those are intended to be accessed by getter functions or other properties in order to avoid conflicts with other code.

There are some that don't have any other way to access, or times you deliberately want to circumvent the normal getting code, but you shouldn't do that unless you know the difference.

For example:

You should always use $gameTroop.members() and not access the _enemies property directly - again, to avoid errors. There's also another small mistake here I'll address below.


Can you explain what you mean? The only part of this that doesn't seem immediately clear is that you need to know how to provide a reference to an "enemy" - because there are so many ways you might do that, it does seem reasonable to me to expect the user to know or learn it, and it would be outside of the scope of the plugin documentation.

You might reference it as a member of the $gameTroop as in this example, you could be using b in a damage formula, target in a skill notetag, etc.


This is not correct. Arrays - including the list of enemies in a troop - start with 0, not 1. So a troop with 4 enemies in it would have indices of 0, 1, 2, and 3, not 1, 2, 3, and 4.

Accounting for that, as well as using the correct getter methods I mentioned above, a more accurate version would be
Code:
$gameTroop.members()[1].changeLevel($gameTroop.members()[0].level)
etc.

However, all of the lines can be condensed into one single-line JavaScript array method, which can then be copy-pasted into any troop (or used as a base troop event if desired) to work with any number of enemies.
Code:
$gameTroop.members().forEach(enemy => enemy.changeLevel($gameTroop.members()[0].level));

This works perfectly! Thank you!
@Khali thank you too for helping as well!
I so strongly disagree with this because there are so many plugins that I run into that are like this. The documentation takes the time to explain that you can change the entire group, but why not explain how to change one member with a clear example?
I understand where you're coming from, and I do think Yanfly could have been a bit clearer with a line like "where enemy is any reference to a Game_Enemy," but overall the clearly stated presumption is that anyone going directly to the script calls has a working knowledge of JavaScript, in which case they should be able to read the relevant information in the core engine's files.

Try to see this from someone's perspective that's trying to learn what all of the stock (and additional plugin) functions do. How are we supposed to know to use $gameTroop.members()[0], and what is the difference from using $gameTroop._enemies[0]?
By looking at the code - $gameTroop._enemies is used in precisely four lines in the entire codebase. Two are initializing or resetting it to an empty array, one is pushing the enemies belonging to the troop into that array when the troop is made at the beginning of battle, and the last is inside the getter function you cite below. There are zero places where an enemy is referenced during battle using the _enemies property.

And, honestly, the last bit should be the only part one needs to see - if one knows what a getter function is, then one ought to know proper OOP is to always use it. I'm not saying this in any negative way toward you, I'm just responding to your question of how one is supposed to know - it is expected that if you're trying to read/use/modify the code, one has the larger knowledge of the programming language to do so.

I realize many people in this hobby space don't and have tried to pick up bits and pieces of JavaScript without taking the time to do a proper course of some sort, but then those are the risks one runs choosing that approach.

I'm genuinely asking because while I realize members() is a getter function, MV library states that both return array<Game_Enemy>. Diving into Game Troop also reveals:
Code:
Game_Troop.prototype.members = function() {
    return this._enemies;
};
As far as I can tell, it doesn't do anything special?
MV library is not any kind of instructional document telling you what to use, it simply and literally lists all properties of all the data types. So yes, _enemies is the actual array of Game_Enemy and the members() method returns that array of Game_Enemy - that's true of any getter function you ever look at.

And in this case, there isn't anything natively done to the array in the getter function, but it's always best practice to always use one when provided. Plugins you might install could modify members() and there are certainly other getter functions that do perform operations before returning a value (see the actor version, Game_Party.prototype.members).

The larger point is that we don't pick and choose which getters we think matter or not, it's correct programming habits to always use them when provided.

If I didn't have previous coding knowledge, how do I know how to select Enemy 1, 2, or 3? It doesn't tell me.
Again, Yanfly's Lunatic Mode section is pretty clearly marked as for people who have JavaScript experience. You're not wrong, he could have included detailed instructions - either there or in another universal section of his Web site - but he chose not to, and simply provided the script calls for those who have previous coding knowledge.

This is better than many other authors do (see the number of threads having problems because VisuStella plugins provide almost zero of their internal script calls for reference).

So I guess the answer to your question is, "if you didn't have previous coding knowledge, how do you know...?" You either study JavaScript to gain that knowledge, or you ask someplace like here in a Learning JavaScript or Plugin Support thread to get some advice and examples of how the script calls can be practically used.

Sure, I can use $gameTroop.members()[0].changeLevel = x to change that value
Just as a small pedantic note, you can't - it would be $gameTroop.members()[0].changeLevel(x) - but I understand that's not your point 😉

but the documentation doesn't say that anywhere. Is this even the intended way to do it?
I don't think there's any "intent" behind a line of code. It's there, it tells you what it does, you use it in whatever way you find a use for in your game.

I hope that was somewhat helpful to your questions - I understand "take JavaScript classes or do tutorials" is not the answer many people want to hear when asking for what they think should be basic RPG Maker/plugin help. But the fact is you can do a ton with MV as a whole and plugins like this without ever touching script calls, so you're not going to find plugins that give you the functions and a little tutorial on how you can use them.

You either know how or you're very welcome to ask for help whenever you need.

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/solved-help-with-specific-request-involving-yep-enemy-levels.180971/