MZ Battle Scene: understanding skill activation

Hi, I want to learn as much as I can about the battle scene code so that I can modify it for my goals.
For this reason I'm trying to replicate some parts of the code to see if I'm able to let it run properly.

I'm having a problem with my version of the skill activation function "onSkillOk". When I select a skill (with scope 1 enemy, not random) it just activate skill on a random enemy, and only after that it let me take a choice about the enemy.

The functions I'm using are the following. I can post all the plugin code if needed but it's a mess since I'm not a programmer at all.

Spoiler: Code
JavaScript:
// This one is the new onSkillOk
  Scene_Battle.prototype.onMaskSkillOk = function () {
        BattleManager.selectActor();
        const skill = this._MaskSkillWindow.item();
        const action = BattleManager.inputtingAction();
        action.setSkill(skill.id);
        BattleManager.actor().setLastBattleSkill(skill);


        this._MaskSkillWindow.close();
        this._helpWindow.hide();
        this.onSelectAction();
    };

Scene_Battle.prototype.onSelectAction = function() {
    const action = BattleManager.inputtingAction();
    if (!action.needsSelection()) {
        this.selectNextCommand();
    } else if (action.isForOpponent()) {
        this.startEnemySelection();
    } else {
        this.startActorSelection();
    }
};

I have noticed that "action.setSkill(skill.id);" is the line that execute the skill, while "this.onSelectAction();" the one that let's start enemy selection. Using "onSelectAction" before "action.setSkill" gives me the error: "Cannot read property 'scope' of null".

What could be the problem? My function are so similiar to the official ones.

Thanks
Can you elaborate on what your goal is here? If you're not changing anything, and you want it to work the same way as the default code, why would you rewrite anything? I would expect changing anything would make it...y'know, work differently.

If you're trying to learn, I think it makes more sense to read the existing code. Follow the function calls and see what everything does. There's not really a purpose to you typing the same code that's already there...if you want to replicate a part of it, you can copy and paste, no?

What is the purpose of adding the selectActor() call at the beginning of the function? For that matter, where is the code for that? It's not a part of MZ.

Aside from that, I'd look at how your "mask" window is integrated into the rest of the scene. Like, I see you have window handling in this method that would normally be handled by other parts of the scene, I suspect some other part of the scene is not correctly going to the window it needs to.
The idea is to have some masks (weapons), each of them has 4 skills.

So I have changed the partyCommandWindow to have just two options: Escape and Mask, when the player presses Mask then a new window is shown (i have called it ChangeWeaponWindow) with a different number of options (1 for each mask in the inventory, plus the equipped one). When the player choose a mask, a new window appears, the MaskSkillWindow I was talking about with only 4 options (one for each skill).

This is the reason why i have duplicated the "onSkillOk" function in a new one "onMaskSkillOk", just to have something that could work as the original skill function but showing different options based on the choosed mask.

The selectActor() function is just a modified SelectNextActor() function, that I used during the development because of an error I thought was depending by that function, but then I turned it back to the original code, so it's just the SelectNextActor function.

I'm posting a gif to help understanding what I'm trying to do, since my english explanation maybe is not very understandable. From the gif it can be also understable the problem. When I use the skill with fire icon it is executed on a random enemy while it's scope is one selected enemy, and the selection starts only after, so to don't get any error I have to press ESC so the partyCommandWindow is opened again. When I use the the skill with the yellow sphere icon there isn't any problem since the scope of that one is all enemies.

I'll try to give a look at the new windows as suggested, hoping to find where the problem is.



ezgif.com-optimize (2).gif
I'm going to suggest you can do this much more easily by primarily using the features built into RPG Maker.

Make the Masks be a skill type. Then selecting that option in the actor command menu will take you to the list of mask skills they currently have access to, just like it always does. There's no reason that this needs to be a new kind of window or anything like that.

So the only actual modification you need to do is to get rid of the Attack command - and there are several existing plugins to help you with that, or you'd copy the function from the default code and basically comment out one line.

The selectActor() function is just a modified SelectNextActor() function, that I used during the development because of an error I thought was depending by that function, but then I turned it back to the original code, so it's just the SelectNextActor function.
But that also doesn't belong in this function. Look at the original onSkillOk(), selectNextActor() isn't in it. That's very possibly the cause of your mess, because you're going to the next actor in the middle of when this one should be choosing a target.

I think the problem is that you're making this more complicated than it needs to be. Simplify it down, use as much of the existing framework as you can before you decide to rewrite and add on lots of new code.
Yea that seems to be easier than what i did. To achieve that I created a new weapon for each mask and used traits to give each of them four specific skills, so every time the player chooses a mask from the menu the choosen one is equipped and player's skills changes because of the traits.

To follow your suggestion I suppose I should use Yanfly multy-type skills plugin, and since I want the player to start with only 3 masks, unlocking some other throught the story, I guess I have to create a system of classes so that class 1 has access to the first 3 types of skills, class 2 has access to them plus a 4th type of skill and so on, and when the player reaches a point in the story then I switch his class to the next one. Is it ok?

By the way, following your advice i have solved the problem of the enemy selection phase right after the skill activation, so now I don't have to press ESC anymore. However, I still have the problem that the enemy is randomly selected...
To follow your suggestion I suppose I should use Yanfly multy-type skills plugin, and since I want the player to start with only 3 masks, unlocking some other throught the story, I guess I have to create a system of classes so that class 1 has access to the first 3 types of skills, class 2 has access to them plus a 4th type of skill and so on, and when the player reaches a point in the story then I switch his class to the next one. Is it ok?
I don't understand why you need multi-type anything - aren't they all the same skill type? Masks?

You just control which of the masks skills the player has. You can use the default method in the database to say they're at this class at this level; use events with the Change Skill command; put Traits on equipment...whatever you want.

Also, which maker are you using? Your profile and the forum you posted in say MZ, but Yanfly plugins are for MV.
I don't understand why you need multi-type anything - aren't they all the same skill type? Masks?
I would like to have them shown in different options. To see skills from mask 1 you have to select mask 1 and so on, just as in the gif posted before. If I put all the skills to the same type (magic for example) they will all be shown in the same window, right?

I use MZ, but I have read that with the Fossil plugin some MV plugins work for MZ as well.
That's true, but you should see if there's a native option for MZ first. VisuStella Skills and States has multi-type skills.

But I don't see how that helps with what you're talking about. Assigning a skill to multiple types won't make it be seen in a separate menu.

You're talking more about something like SRD's Skill Containers, which VisuStella also has a version of. That would make all of your masks be inside of your Masks skill type, and then you have to choose one to open its own category.
Yes exactly, that's what I'm trying to achieve with my code. A menu like that, with masks instead of elements. I didn't even know about this plugin.

Original thread: https://forums.rpgmakerweb.com/threads/battle-scene-understanding-skill-activation.160111/