How to make skill-learning items be restricted by class?

If I put 2 Fire Books in my inventory, and then use them on 2 characters, it doesn't immediately close out of the character selection scene. I can't use more than I have, but shouldn't it go back to the main item menu once all of the items have been expended? Is that normal behavior for MZ or is it due to the script?
Isn't it really easy to test that by disabling the plugin and using some items? O__o

But no, default MZ does not close the window automatically when you run out of items.
Isn't it really easy to test that by disabling the plugin and using some items? O__o

But no, default MZ does not close the window automatically when you run out of items.

You raise a good point lol. But at least now I know that it's nothing wrong! 😛 Thanks again!
You can also do this with common events. Just make the item when used trigger a common event and then have a conditional branch in the common event triggered that requires the actor or whoever is using it to be a certain class and if they are it teaches them the skill and if they aren't well you can do whatever you want such as an error message or something
You can also do this with common events.
Only kind of. Common events trigger after the item is used and consumed, so the player of the game would select the target, the item goes away.

You could make that common event refund the item so it comes back, but it's still not the same experience in the interface as when you try to use, say, a potion on a target that can't receive healing, and it simply doesn't let you.

I'm not saying that's a terrible thing to do, but from a software design perspective it's better to have parts of the game (especially the UI) react in predictable ways.

If you wanted to event the whole system and it departs from the item menu entirely into its own interface, I think that would be better.
Okay, so this works in my testing.
Code:
Game_Action.prototype.hasItemAnyValidEffects = function(target) {
if ($dataItems[this.item().id].meta.notClass && $dataItems[this.item().id].meta.notClass.includes(target.currentClass().id.toString()))
    return false;

return this.item().effects.some(effect =>
        this.testItemEffect(target, effect)
    );
};

You can make the notetags exactly like before.

I still can't speak to your prior errors. It should already prevent them from using the item if they already have the skill learned. And nothing in here should be able to cause a crash. So if something like that comes up, there may be something else wrong in your project.


I have this code installed as a plugin, and when I put this on the notetag of a skill book item it blocks every single class not just the 3. What is the correct way to do this? Thanks bud.


DOES NOT WORK: <notClass: [13,24,35]>
I have also tried: <notClass: [id13, id24, id35]>


MZ project
I have this code installed as a plugin, and when I put this on the notetag of a skill book item it blocks every single class not just the 3.
I'm not sure what to tell you. As I said in the post you quoted, it works in my tests 😉

The first thing you did is the correct syntax, you would never type the letters "id."

I didn't test this with any other plugins, so if you're using anything that relates to items at all it could be screwing with it.

If you're not using any other plugins and it's not working for you in a vanilla project, then put up some screenshots of the item and your classes that the actors have.

Make sure any time you change anything in your project that you're starting a new play test, not loading a save.
I'm not sure what to tell you. As I said in the post you quoted, it works in my tests 😉

The first thing you did is the correct syntax, you would never type the letters "id."

I didn't test this with any other plugins, so if you're using anything that relates to items at all it could be screwing with it.

If you're not using any other plugins and it's not working for you in a vanilla project, then put up some screenshots of the item and your classes that the actors have.

Make sure any time you change anything in your project that you're starting a new play test, not loading a save.
Tested it in both a new game project and my project and here are the results:

It appears single digit numbers work fine for classes. However, when you get to double digits things get wonky.

My current class list is Warrior 2, Shaman 13, Rogue 24, Elementalist 35. I have noticed if I put: <notClass: [24]>

Not only does Rogue get blocked, but so does Warrior. This seems to happen because it's reading the 2 and the 24 together instead of just 24. I tried quotations and it does not work. I.E. <notClass: ["24"]>

I am separating class types so that's why the numbers are spread atm. I plan to add more classes based on those types.

EDIT: Btw, none of my plugins are interfering with it. I tested all of them each one by one. And the new project does the exact same thing, so it has to be the plugin script. Create some classes in the double digits that have similar numbers and you'll see what I mean. 1/2/4/24 is a good test using <notClass: [24]> - the only character that can accept the skill is 1.

1661916998682.png
I tried quotations and it does not work. I.E. <notClass: ["24"]>
That would never work, you should always follow notetag formats as written. Incidentally, you also didn't do this by omitting the spaces that I showed in my example notetag, but in this case that didn't hurt anything (in some plugins, it could).

Not only does Rogue get blocked, but so does Warrior. This seems to happen because it's reading the 2 and the 24 together instead of just 24.
Good call, that would be exactly the reason. So, modify the code as below:

Code:
Game_Action.prototype.hasItemAnyValidEffects = function(target) {
if ($dataItems[this.item().id].meta.notClass)
{
    var classArray=$dataItems[this.item().id].meta.notClass.split(',').map(element => element=Number(element));

    if (classArray.includes(target.currentClass().id))
        return false;
}

return this.item().effects.some(effect =>
        this.testItemEffect(target, effect)
    );
};

I haven't tested this with an in-game setup, but it works in my testing in the console. Let me know if you have any problems.

Please note this changes the format of the notetag from what I posted before: the class IDs must be separated by commas but no spaces, and the brackets are no longer used. e.g. <notClass: 2,24,35,64>
That would never work, you should always follow notetag formats as written. Incidentally, you also didn't do this by omitting the spaces that I showed in my example notetag, but in this case that didn't hurt anything (in some plugins, it could).


Good call, that would be exactly the reason. So, modify the code as below:

Code:
Game_Action.prototype.hasItemAnyValidEffects = function(target) {
if ($dataItems[this.item().id].meta.notClass)
{
    var classArray=$dataItems[this.item().id].meta.notClass.split(',').map(element => element=Number(element));

    if (classArray.includes(target.currentClass().id))
        return false;
}

return this.item().effects.some(effect =>
        this.testItemEffect(target, effect)
    );
};

I haven't tested this with an in-game setup, but it works in my testing in the console. Let me know if you have any problems.

Please note this changes the format of the notetag from what I posted before: the class IDs must be separated by commas but no spaces, and the brackets are no longer used. e.g. <notClass: 2,24,35,64>
That got it! Glad I could help with it and squish this bug with you. Thank you once again for your help!
I have the same problem but in RMMV. Should this fix work for MV as well?

Original thread: https://forums.rpgmakerweb.com/threads/how-to-make-skill-learning-items-be-restricted-by-class.148995/