Spell books

I want to create spellbooks that allows characters to learn different spells with these features:

1)Certain books can only be used by certain characters. if a character is using fire magic, he can't learn water magic spells.

Note: there are 4 characters and everyone is using different magic, never the same, like:
A-Fire
B-Water
C-Earth
D-Wind

2)If a character already knows the spell, he can't use the corresponding spelltome again. This is made so that the player can sell unnecessary books.

I haven't found any way to even run the code while using the item. YEP_ItemCore has an option to run the code when creating an item, but, if I'm not mistaken, there is no option for code running per use.

The basic item options can't even change the variables/switches, there is only one option to "learn a spell" and it prevents learning one spell twice, which is half of what I need. But it can't forbid the fire mage to learn water spells.

Need help.
simply use a common event attached to the item.
depending on the number of actors it can be a long event to check for everything, but no need for any plugin that way.
and the item needs to be set to non-consumable but when the common event allows learning of the skill it removes the item.
simply use a common event attached to the item.
depending on the number of actors it can be a long event to check for everything, but no need for any plugin that way.
and the item needs to be set to non-consumable but when the common event allows learning of the skill it removes the item.
Is there an option to check what actor used a book?

I thought about common events, but that's exactly why I mentioned that items can't change variables, I'll have to create a different common event for each book, and I don't want to do it that way.

If items could change the value of the variable when using them, I'd just write down every spellbook to a certain values for one variable, and then do it like:
-Use spell tome(fireball)
-change variable "spell tome" to 3(1-earthquake 2-seastorm 3-fireball and so on)
-run common event that goes through many conditional branches that checks spelltome value for all the spells until it finds the value that was given to the variable and then gives you corresponding spell.
But I can't do it even that way.
Is there an option to check what actor used a book?
yes, although that is why the common event can become long.

make an invisible, no-effect state named "book-target" (no messages, no icon, no trait) and have that state applied as an effect of the book.
then in the common event, have one conditional branch for each actor checking if he has that state - if yes make the commands needed (either show text can't use, or learn skill and remove. After that remove the state so the next use of the book doesn't confuse the event.

I think that in MZ there was a function added to game data of control variable to make it easier, but MV is missing that option
yes, although that is why the common event can become long.

make an invisible, no-effect state named "book-target" (no messages, no icon, no trait) and have that state applied as an effect of the book.
then in the common event, have one conditional branch for each actor checking if he has that state - if yes make the commands needed (either show text can't use, or learn skill and remove. After that remove the state so the next use of the book doesn't confuse the event.

I think that in MZ there was a function added to game data of control variable to make it easier, but MV is missing that option
Okay, that seems pretty good, thank you.
yes, although that is why the common event can become long.

make an invisible, no-effect state named "book-target" (no messages, no icon, no trait) and have that state applied as an effect of the book.
then in the common event, have one conditional branch for each actor checking if he has that state - if yes make the commands needed (either show text can't use, or learn skill and remove. After that remove the state so the next use of the book doesn't confuse the event.

I think that in MZ there was a function added to game data of control variable to make it easier, but MV is missing that option
At the time of writing this reply I didn't really test this system, but now I realized that it's not so simple, namely: I can specify through a common event which hero to learn a spell, but I don't know how to make a hero learn a specific spell.
That is, for example, if hero 1 uses the book of spells "fireball", you can set which spells the hero can learn and which are not, you can't assign a specific spell to specific book, and the common event won't be able to give the hero specific spell, for the basic common event is common to all books. And you can't assign variables or switches to books with the basic item interface.
you can't assign a specific spell to specific book,
why not? off the hand I can think of two or three different ways to do that depending on what exactly you want to do and how you were to decide what a specific book would do to various actors.
after all, you already have a different branch for each different hero, so you can use that for different spell assignments or even further branching.

I think for more help, you need to give more details of how this is to work in your game, possibly with screenshots of how you made the item and the common event to check for possible errors there.
why not? off the hand I can think of two or three different ways to do that depending on what exactly you want to do and how you were to decide what a specific book would do to various actors.
after all, you already have a different branch for each different hero, so you can use that for different spell assignments or even further branching.

I think for more help, you need to give more details of how this is to work in your game, possibly with screenshots of how you made the item and the common event to check for possible errors there.
Unfortunately, I will not be able to attach screenshots for two reasons:
1) my interface is in Russian
2) I have nothing to attach because I just do not understand how it should work.

There is a lot of books. each contains a different spell. Spells are divided by groups (A,B,C,D) depending on the character that can learn them. Each book just runs a common event, it's not consumable.
Let's say a character 1 uses a book with a spell, it's a group A book, the one that should give the first character "flight" spell.
-The character that used a book gets the "book" state.
-The common event checks if character 1 has the "book" state. (it checks for all the characters after the first one).
-It's true.
and... that's it.
I can't just do "if character 1 doesn't know the flight spell - learn flight spell", because then the first character will always learn that same spell, regardless of the book.
I can branch this out and after checking for "book" status, make a check to see if the book with the spell is in the inventory and if it is, give the player that spell.
But then I would be using a book with a flight spell and learning a fireball, if I have 2 books in the inventory (If the fireball check is before the flight check in the common event). This system will work, but not correctly.

I don't know how to connect the specific book and specific spell, that should be learned after using it.
I don't know if I'm making myself clear, but I just see no way to learn a specific spell through a common event.
different books can call different common events
then you know which book has called the effect and can give a different skill depending on which book was used.
and then you can check each actor with conditional branch if that skill is learned or not, and use the change skill command for the actor to learn it if not.

there are other ways, but that one should be best for you.
You should check out Frogboy's MV plugins also. He has one that allows characters to learn new spells from scroll items.
different books can call different common events
then you know which book has called the effect and can give a different skill depending on which book was used.
and then you can check each actor with conditional branch if that skill is learned or not, and use the change skill command for the actor to learn it if not.

there are other ways, but that one should be best for you.
I really don't want to have 100+ common events just for the books, fore there will be a lot of spells.

After all, the point of common events is to combine repeated events. And if I create a common event for each book, it's like if I write every event on the map in a separate common event and call it later instead of just straightly put the code in the map event. It loses meaning.

So if you know the other ways of doing it, I'll be glad to hear it.
there still are several other ways, but no one can work miracles.
I simply don't want to waste the time to explain one of the other ways only for you to then pull another restriction out of nowhere why you can't do it that way.
So if you want more help, you need to give the full info on the game mechanik you want, before I start thinking on which way that can be done.
there still are several other ways, but no one can work miracles.
I simply don't want to waste the time to explain one of the other ways only for you to then pull another restriction out of nowhere why you can't do it that way.
So if you want more help, you need to give the full info on the game mechanik you want, before I start thinking on which way that can be done.
Okay, I will try to explain this in as much detail as I can.

Spells in the game consume only mana and take up spell slots provided by this plugin:
So each character may know a large number of spells, but at the same time can use only those that are active and are in the slots. The number of slots can be increased if your character gains expirience.

There are 7 types of elements and 4 characters, I'll be calling them a,b,c,d for simplicity

Character A can use Fire, Ice and Earth magic
Character B uses Wind and water.
Character C doesn't use any magic.
Character D can use Electro and Dark magic.

In the beginning, characters know only the basic spells of their school. Character A knows the "beginners flame" spell of the flame school. Character B knows the "Wind gust" spell of the wind school.

Additional spells characters can find in spell books in different parts of the world. Any character can learn any spell from their respective school, even the most difficult one. But they will not be able to use them effectively, because as long as the character does not have enough experience, the mana cost of complex spells will be too high.

There is more than one book of each spell, so you can find the same book in the dungeon, or buying from a merchant. Books can stack. You can sell the books and buy them.
It doesn't cost anything to learn the spell, you can do it at any time if you have the spell book, and if your character doesn't already know the spell.

Example: You found a spell book "waterfall" in the cave. In the description of the item you can see that this spell belongs to the water school (spell deals water damage). So If you try to use it on the character "A", who uses Fire,Ice and Earth spells, you will have a message "This character can't use water magic".
But if you use the book on the character "B", then this character will learn the spell "waterfall", for he can handle water magic, and will be able to equip it in his spell slot.

Restrictions: I don't want to do this by creating a common event for each spell, because in my combat system, characters can learn as many spells as they want and combine them by adding them to their active slots, so there will be plenty of spells, giving the player a lot of room for experimentation.
I don't mind creating a separate switch for each spell, or better yet, creating a database where each possible spell is assigned a number, and that way you can register all the spells in one variable that will determine which spell the character must learn depending on its value. The creation of hundreds of states for characters depending on the spell is not the last idea either.

This is ideal system. But given the complexity of the task, I think to do it the more easy way: The books will not affect all characters but will be applied to the whole party, that is, when you use the book, the A character (it doesn't matter what character, the only thing matter is the state) will be given a state depending on the spell. And then a common event will be triggered, with only state checks for the A character (just because all the books give the A character corresponding state).

If he's got "Waterfall" state on him, you'll be asked if you want the B character to learn Waterfall, for this spell can be learned only by the B character. If you use a "Stone wall" book, you'll be asked if you want the A character to learn the stone wall, for he uses Earth magic.

In fact, in the same way I can implement the primary system, that is, I can apply a state-spell on the character that uses the book, and then, in a common event, check whether this or that character has a certain state. So, the very big common event will check all possible states first for character A, then for B,C and D. Until the system finds out which of the characters has a state(used a book). And then, if character A is found to have the state "waterfall" - then there will be a message that he cannot learn such a spell. And if it were "fireball", then the system would ask if character A knows this spell and if not - if you want to learn it.

It's all a matter of using the system you suggested, but with hundreds of different states corresponding to the spells. And it seems to solve the problem, but I just want to know your opinion, would you do it that way, or is it too bulky and there are easier ways out?
I think this would be better handled with the built-in Learn Skill option, but using a plugin that adjusts its behaviour to what you need. I think one of my plugins does exactly this, but it's unreleased; I could look into polishing it up and posting it though. It's also a very small change, so you could probably do it yourself if you have any programming knowledge (or if not, ask someone to do it for you).
There is another approach you might want to consider:
Skill-Type preselection.

To use any given skill, the actor needs both the skill type and the skill itself.

You now make one skill type for each type of magic - fire, water, earth and so forth.
Those skill types will be assigned to theiractors as needed.

then your spell book "Waterfall" teaches that skill to everyone - but only the actor with skill type water magic will be able to use it, for the actors who are missing that skill type the skill will not even be displayed despite them having learned it.
Okay, I will try to explain this in as much detail as I can.

In the beginning, characters know only the basic spells of their school. Character A knows the "beginners flame" spell of the flame school. Character B knows the "Wind gust" spell of the wind school.

Additional spells characters can find in spell books in different parts of the world.
Did you look at the plugin I suggested above? It gives you different schools of magic and items to learn new spells from according to the schools you know. It sounds like exactly what you're talking about.

Example: You found a spell book "waterfall" in the cave. In the description of the item you can see that this spell belongs to the water school (spell deals water damage). So If you try to use it on the character "A", who uses Fire,Ice and Earth spells, you will have a message "This character can't use water magic".
But if you use the book on the character "B", then this character will learn the spell "waterfall", for he can handle water magic, and will be able to equip it in his spell slot.
This is, as far as I know, all handled by the Frogboy plugin.
Restrictions: I don't want to do this by creating a common event for each spell
There's no reason you would do that. For one thing, the events would be attached to the items that might teach the spell, not the spell itself (which might be what you meant). For another, even if the other plugin I already suggested you look into doesn't handle this the way you want for some reason, you can use something like Yanfly's...Item Categories or something, or futz around with the Skill Categories, or find some not too difficult code to handle this.
The creation of hundreds of states for characters depending on the spell is not the last idea either.
Where on earth did the idea of hundreds of states come from?
This is ideal system. But given the complexity of the task, I think to do it the more easy way: The books will not affect all characters but will be applied to the whole party, that is, when you use the book, the A character (it doesn't matter what character, the only thing matter is the state) will be given a state depending on the spell. And then a common event will be triggered, with only state checks for the A character (just because all the books give the A character corresponding state).

If he's got "Waterfall" state on him, you'll be asked if you want the B character to learn Waterfall, for this spell can be learned only by the B character. If you use a "Stone wall" book, you'll be asked if you want the A character to learn the stone wall, for he uses Earth magic.
This whole thing has me really super confused. I don't understand why you're putting states on people to use an item, and I don't understand how putting the Learn Waterfall state on one actor makes someone else learn it and why a Stone Wall book would look at Learn Waterfall and...I'm boggled. I can't imagine any reason to go through all this.
Did you look at the plugin I suggested above? It gives you different schools of magic and items to learn new spells from according to the schools you know. It sounds like exactly what you're talking about.
I did. But the reason I don't want to use it is its complexity and, I don't even know how to say it properly, its globality. Yanfly plugins are small tools that, in competent hands, can tune your game to any task. And the frogboy plugin is a global plugin that takes almost full control of your magic system. You have a huge number of settings to adjust, and you end up with a great magic system. But what if I want to do something that the plugin doesn't allow? Or it will be incompatible with other plugins.

I could be wrong because I haven't spent much time with the plugin, or I may be too dramatic, but as I said before, I don't want to put most of my game mechanics completely under the control of one plugin. Because I've already implemented almost all of the mechanics I need with yanfly plugins.

This whole thing has me really super confused. I don't understand why you're putting states on people to use an item, and I don't understand how putting the Learn Waterfall state on one actor makes someone else learn it and why a Stone Wall book would look at Learn Waterfall and...I'm boggled. I can't imagine any reason to go through all this.
The basic item interface does not support changing the variables/switches values. So you can't assign the spell you want to learn to a specific variable. There are only Common event option, where you can learn skill, and basic learn skill option.
But the problem with the basic option is that any character can learn any spells.
So there are a few options left, from which I can see creating a state for each character and then checking it in the common event. Or creating common event for each spell.

This is a bulky solution and I'm leaning towards using a separate simple tiny plugin for such purposes, since I don't think there's much to change in the code either, but for now, as I don't have such plugin, I need to do at least something that works, since deadlines are burning up.

Oh, gods of graceful and simple solutions, forgive me, for I have sinned again...
I did. But the reason I don't want to use it is its complexity and, I don't even know how to say it properly, its globality. Yanfly plugins are small tools that, in competent hands, can tune your game to any task. And the frogboy plugin is a global plugin that takes almost full control of your magic system. You have a huge number of settings to adjust, and you end up with a great magic system. But what if I want to do something that the plugin doesn't allow? Or it will be incompatible with other plugins.
You do whatever works best for you, but it seems to me that you're proposing a pretty sweeping change to how your magic and skills function. A plugin that is intentionally designed to do so much of that work for you seems...convenient, to me. I would certainly try it, if I were in your shoes, because it would handle so much of the stuff you're talking about designing.
The basic item interface does not support changing the variables/switches values. So you can't assign the spell you want to learn to a specific variable. There are only Common event option, where you can learn skill, and basic learn skill option.
But the problem with the basic option is that any character can learn any spells.
So there are a few options left, from which I can see creating a state for each character and then checking it in the common event. Or creating common event for each spell.
I mean, again, my understanding is that the plugin you don't want to use does that for you...so I'm confused as to why you don't want to try it. I have a hard time imagining that reading instructions and configuring the parameters would take longer than re-designing the system on your own.
But if you really don't want to, the other way you're proposing is still super convoluted compared to, say, a plugin like this that allows you to run the specified JavaScript when you use an item - no common events or states are necessary.
This is a bulky solution and I'm leaning towards using a separate simple tiny plugin for such purposes, since I don't think there's much to change in the code either,
Yeah, it is indeed simple:
JavaScript:
Window_ActorCommand.prototype.addSkillCommands = function() {
    var skillTypes = this._actor.addedSkillTypes();
    skillTypes.sort(function(a, b) {
        return a - b;
    });
    var knownTypes = new Set();
    this._actor.skills().forEach(function(skill) {
        if(!this._actor.isOccasionOk(skill)) {
            return;
        }
        knownTypes.add(skill.stypeId);
    });
    skillTypes = skillTypes.filter(knownTypes.has.bind(knownTypes));
    skillTypes.forEach(function(stypeId) {
        var name = $dataSystem.skillTypes[stypeId];
        this.addCommand(name, 'skill', true, stypeId);
    }, this);
};

That just hides skill types in the selection menu if the actor doesn't have that skill type enabled. I haven't tested this case, but I _think_ that'll allow you to teach the skill to everyone and it'll show up only for the person who has the correct skill type.

There's almost certainly a better way to do what you want, though. It should be possible to make the item system refuse to apply the item to an actor who doesn't have the skill type. If you want the item to be consumable and single-target, the above solution won't work very well and you would need to do this instead. But if the item is not used up when you learn the skill, and you don't mind it being treated as an item that targets the whole party, the above code should be sufficient.

(That code is copied from my plugin and adjusted to remove references to plugin paramaters, so there's a small chance that it doesn't work. If you try it, let me know.)
You do whatever works best for you, but it seems to me that you're proposing a pretty sweeping change to how your magic and skills function. A plugin that is intentionally designed to do so much of that work for you seems...convenient, to me. I would certainly try it, if I were in your shoes, because it would handle so much of the stuff you're talking about designing.

I mean, again, my understanding is that the plugin you don't want to use does that for you...so I'm confused as to why you don't want to try it. I have a hard time imagining that reading instructions and configuring the parameters would take longer than re-designing the system on your own.
But if you really don't want to, the other way you're proposing is still super convoluted compared to, say, a plugin like this that allows you to run the specified JavaScript when you use an item - no common events or states are necessary.
Well, umm...I've looked for such a plugin several times, but it looks like I've searched very badly because it's the solution to all my idiotic decisions. I just realize that all of this mess above could have been skipped, if I had known about this plugin before.
Now all that's left is to find the necessary commands to write the script.

Thanks. As always.

Original thread: https://forums.rpgmakerweb.com/threads/spell-books.140914/