Adding a Skill or Item by name?

Ayoni

Villager
Member
Joined
Aug 15, 2017
Messages
10
Reaction score
1
First Language
English
Primarily Uses
RMMV
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?
 

shockra

Slightly Crazy Programmer
Veteran
Joined
Feb 16, 2016
Messages
418
Reaction score
185
First Language
English
Primarily Uses
RMMV
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?
 

Ayoni

Villager
Member
Joined
Aug 15, 2017
Messages
10
Reaction score
1
First Language
English
Primarily Uses
RMMV
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.
 

shockra

Slightly Crazy Programmer
Veteran
Joined
Feb 16, 2016
Messages
418
Reaction score
185
First Language
English
Primarily Uses
RMMV
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.
 

Ayoni

Villager
Member
Joined
Aug 15, 2017
Messages
10
Reaction score
1
First Language
English
Primarily Uses
RMMV
Ah yes! Thank you very much :D
 

Kaelan

Veteran
Veteran
Joined
May 14, 2012
Messages
767
Reaction score
462
First Language
Portuguese
Primarily Uses
N/A
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!
 

Ayoni

Villager
Member
Joined
Aug 15, 2017
Messages
10
Reaction score
1
First Language
English
Primarily Uses
RMMV
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! :D
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

I was doing forge runs on D2 with my wife last night, and singing like a leprechaun to annoy her XD. I was calling it 'The Farge'
Is i possible too make a weapon have a chance of using a skill?
Can the Eternal Dreamers finish off the final boss? We'll see!
Fekken kill me. I unknowingly swapped out two important events's codes and saved the game to test it. I'm just doing this for fun and the fun has now been replaced with excruciating torture.
Is this how game developers feel all the time?

EDIT:
WAIT HOLY S***
MY LAST POST ASKING FOR HELP ON AN EVENT ACTUALLY SAVED IT
OH MY GOD
Coffee + Silk Coconut Milk + Splenda Zero = Not ..... awful :/ .

Forum statistics

Threads
94,448
Messages
921,010
Members
124,272
Latest member
lymeish
Top