Adding a Skill or Item by name?

Hello,

I am interested in adding skills to actors by means of the skill's name. What function(s) would allow me to figure out how to add a skill this way?

Example:
$gameActors.actor(1).learnSkill("Name of Skill to Learn");

Any ideas?
By default, the learnSkill function requires the ID number of the skill. A plugin is likely required to go by the name of the skill. Is there a reason why you can't use the skill ID to learn the skill?
Yes, the concept is many different string variables are collected through input. I can collect the strings and put them into the skill's name. I want to put that stored string variable into a skill index. Possibly using $dataSkills[].name?


var Slot1 = $dataItems[$gameVariables.value(1)].name

var Slot2 = $dataItems[$gameVariables.value(2)].name

The items you combine would be "Fire" and "Storm" to produce "Firestorm," which is a skill you can learn.
This type of thing may require a for loop. The issue is that $dataSkills is an array; therefore, each skill in the array has its own name. What may work is:

for (i = 1; i < $dataSkills.length; i++) {
var skill = $dataSkills;
if (skill.name === yourCustomStringVariable) {
$gameActors.actor(1).learnSkill(i);
break;
}
}

With the break command, the for loop will stop once the skill is learned and will continue with any code afterward. Under this system, keep in mind that you should avoid using the same name in two skills. It may also cause some lag if you're using a lot of skills.
Ah yes! Thank you very much 😀
The above should work fine, but you can also shorten that call a bit. $dataSkills is an Array, and Arrays in javascript have a built-in findIndex() function that does basically the same thing as the code shockra wrote above, but is a bit less to type. You pass it whatever condition you want, and it will return the index of the first item in the array that passes that condition (or -1 if no item was found). So you can write something like:

JavaScript:
var index = $dataSkills.findIndex((skill) => skill && skill.name === NameOfTheSkillYouWant);
if (index > -1) $gameActors.actor(1).learnSkill(index);

As above, if you have multiple skills with the same name, it'll just learn the first one in the database.

It's also worth mentioning that javascript string comparisons are case sensitive, so in this case "Fire" + "Storm" would not be equal to "Firestorm" (because you'd be searching for "FireStorm" instead). So in that case, both of the above methods would fail and you wouldn't actually learn the skill. If your game is entirely in English, you can just make them both uppercase (or lowercase) to get a case-insensitive match:

JavaScript:
var index = $dataSkills.findIndex((skill) => skill && skill.name.toUpperCase() === NameOfTheSkillYouWant.toUpperCase());
if (index > -1) $gameActors.actor(1).learnSkill(index);

This way, setting name you want as "Fire" + "Storm" would cause you to learn a skill called "Firestorm". If your game is only in English, then you can stop here. If you have any plan of supporting additional languages, then it gets a little more complicated:

JavaScript:
var index = $dataSkills.findIndex((skill) => skill && skill.name.localeCompare(NameOfTheSkillYouWant, undefined, { sensitivity: 'accent' }) === 0);
if (index > -1) $gameActors.actor(1).learnSkill(index);

That will always get you the correct skill, even if you translate your game into a language with special characters, like Russian or Japanese. It's probably more than you need in this case, but worth knowing nonetheless!
Oh, very nice! This is exactly what I was thinking. Thanks for going the extra mile to show case sensativity and language support. The way I was dealing with cases was much cruder than this, awesome! 😀

Original thread: https://forums.rpgmakerweb.com/threads/adding-a-skill-or-item-by-name.116397/