How to make actor learn random skill based on a certain skill pool?

Therrao

Villager
Member
Joined
May 7, 2017
Messages
20
Reaction score
27
First Language
English
Primarily Uses
RMMV
So in the game that I'm currently making, player is granted level up for the entire party at the end of dungeon run. and then each member will gain random skill.
For example, I have a pool of skill that consist of:
1. Fire dagger
2. Smoke bomb
3. Slide
4. Evasion
5. Backstab
so when the level up event is called, it will randomize the result and then pick 1 skill from pool and push it to actor 1. and then randomize again before picking for actor 2, and then actor 3. If the actor already learned the skill, then it will redo the randomize process to pick another skill.

I do have some idea creating it through common event, but I'm wondering if there is an easier way to do this.
I read few threads about creating skill array but still have no clue how to do it efficiently.
Thank you! (And sorry for poor English)
 
Last edited:

caethyril

^_^
Global Mod
Joined
Feb 21, 2018
Messages
2,748
Reaction score
2,100
First Language
EN
Primarily Uses
RMMZ
You could try something like this in a Script command (untested):
JavaScript:
var ids = [10, 11, 12, 13, 14, 20];
$gameParty.members().forEach(function(actor) {
  var s = ids.filter(function(id) {
    return !actor.isLearnedSkill(id);
  });
  if (s.length) {
    actor.learnSkill(s[Math.randomInt(s.length)]);
  }
});
Edit the first line to change which skills can be learned. As-is, this says:
  • Let ids be an array (a.k.a. "list") of skill IDs: 10, 11, 12, 13, 14, and 20.
  • For each actor in the party...
    • Let s be equal to ids, but only include IDs that...
      • ...this actor hasn't learned yet.
    • If there are any values in s, then...
      • ...make this actor learn one random skill from s.
In case you're interested, you can find more technical details on arrays over here:

[Edit: it is isLearnedSkill, not isSkillLearned.]
 
Last edited:

Therrao

Villager
Member
Joined
May 7, 2017
Messages
20
Reaction score
27
First Language
English
Primarily Uses
RMMV
You could try something like this in a Script command (untested):
JavaScript:
var ids = [10, 11, 12, 13, 14, 20];
$gameParty.members().forEach(function(actor) {
  var s = ids.filter(function(id) {
    return !actor.isSkillLearned(id);
  });
  if (s.length) {
    actor.learnSkill(s[Math.randomInt(s.length)]);
  }
});
Edit the first line to change which skills can be learned. As-is, this says:
  • Let ids be an array (a.k.a. "list") of skill IDs: 10, 11, 12, 13, 14, and 20.
  • For each actor in the party...
    • Let s be equal to ids, but only include IDs that...
      • ...this actor hasn't learned yet.
    • If there are any values in s, then...
      • ...make this actor learn one random skill from s.
In case you're interested, you can find more technical details on arrays over here:
thank you very much for responding!
EDIT: Fixed it by changing isSkillLearned to isLearnedSkill!

It works perfectly so far!
So what if i want to loop it several time, like say 4 times instead of re-running the script every time?

EDIT 2: Did the loop like this, seems to be working but im not sure if it's the correct way:

var value = 1;
for (var n = 1; n < 4; n++) {
value += 1;
var ids = [27, 28, 29, 30, 31, 32];
$gameParty.members().forEach(function(actor) {
var s = ids.filter(function(id) {
return !actor.isLearnedSkill(id);
});
if (s.length) {
actor.learnSkill(s[Math.randomInt(s.length)]);
}
});
}
 
Last edited:

caethyril

^_^
Global Mod
Joined
Feb 21, 2018
Messages
2,748
Reaction score
2,100
First Language
EN
Primarily Uses
RMMZ
EDIT: Fixed it by changing isSkillLearned to isLearnedSkill!
Oh whoops! Thanks for mentioning that, I'll correct it~ :kaoblush:

So what if i want to loop it several time, like say 4 times instead of re-running the script every time?
You have the right idea, but I think the value variable is unnecessary. I suggest you remove these lines:
  • var value = 1;
  • value += 1;
 

Therrao

Villager
Member
Joined
May 7, 2017
Messages
20
Reaction score
27
First Language
English
Primarily Uses
RMMV
Oh whoops! Thanks for mentioning that, I'll correct it~ :kaoblush:


You have the right idea, but I think the value variable is unnecessary. I suggest you remove these lines:
  • var value = 1;
  • value += 1;
awesome, thank you!
now i'm stuck at figuring out how to show the newly-learned skills into messagebox.
i tried with this script as the base:
$gameMessage.add($gameActors.actor(i).name() + " Learned " + $dataSkills[skillLearned].name );
but seems to be not working. tried assigning "s" into the [skillLearned] but error showing me cannot read property .name. is there any other way?
 

caethyril

^_^
Global Mod
Joined
Feb 21, 2018
Messages
2,748
Reaction score
2,100
First Language
EN
Primarily Uses
RMMZ
Ah, if you want to reference the skill ID in multiple places then you'll need to store it in a variable, e.g.
JavaScript:
var ids = [27, 28, 29, 30, 31, 32];
for (var n = 0; n < 4; n++) {
  $gameParty.members().forEach(function(actor) {
    var s = ids.filter(function(id) {
      return !actor.isLearnedSkill(id);
    });
    if (s.length) {
      var id = s[Math.randomInt(s.length)];
      actor.learnSkill(id);
      $gameMessage.add(TextManager.obtainSkill.format($dataSkills[id].name));
    }
  });
}
I've also changed the starting value on the for loop! (I didn't notice it before.) Now it goes 0, 1, 2, 3: that's 4 loops. This is because n < 4 is checked before the start of each loop, and n++ is applied at the end of each loop. :kaohi:
 

Therrao

Villager
Member
Joined
May 7, 2017
Messages
20
Reaction score
27
First Language
English
Primarily Uses
RMMV
Ah, if you want to reference the skill ID in multiple places then you'll need to store it in a variable, e.g.
JavaScript:
var ids = [27, 28, 29, 30, 31, 32];
for (var n = 0; n < 4; n++) {
  $gameParty.members().forEach(function(actor) {
    var s = ids.filter(function(id) {
      return !actor.isLearnedSkill(id);
    });
    if (s.length) {
      var id = s[Math.randomInt(s.length)];
      actor.learnSkill(id);
      $gameMessage.add(TextManager.obtainSkill.format($dataSkills[id].name));
    }
  });
}
I've also changed the starting value on the for loop! (I didn't notice it before.) Now it goes 0, 1, 2, 3: that's 4 loops. This is because n < 4 is checked before the start of each loop, and n++ is applied at the end of each loop. :kaohi:
haha yes i just noticed i put 1 instead of 0!
tried the updated script but showing error unexpected end of input on console log
 

caethyril

^_^
Global Mod
Joined
Feb 21, 2018
Messages
2,748
Reaction score
2,100
First Language
EN
Primarily Uses
RMMZ
tried the updated script but showing error unexpected end of input on console log
I just did a quick test with that code and it seems to work OK for me, no errors. :kaoswt:

Are you sure you copied it correctly? Did you make any edits?

Unexpected end of input can be caused by mismatched brackets or quote marks, e.g. check for any { without a corresponding }.
 

Therrao

Villager
Member
Joined
May 7, 2017
Messages
20
Reaction score
27
First Language
English
Primarily Uses
RMMV
I just did a quick test with that code and it seems to work OK for me, no errors. :kaoswt:

Are you sure you copied it correctly? Did you make any edits?

Unexpected end of input can be caused by mismatched brackets or quote marks, e.g. check for any { without a corresponding }.
OMG i didn't know script editor has input limit at the bottom, the close bracket didn't get copied! sorry it's all worked out flawlessly! Thank you soo much Caethyril!!!
 

Latest Threads

Latest Posts

Latest Profile Posts

Too bad the Boss Battle Build Bout isn't also for VXAce.
I might not participate in the Boss Battle contest after all... I have more important things to do, like Wishful Wanda. And of course the Dark Deception spinoff I plan to pitch, Demon Slayer.
Changed my avatar, goodbye Alan Sugar, hello George Carlin (one of my favorite human beings ever)
If you still don't subscribe our Polish channel please consider it :)
1.png
Who wants to see my review of the worst star wars movie? This movie has all the excitement of being on Jury Duty of the most boring case ever about trade negotiations.

Forum statistics

Threads
115,171
Messages
1,087,808
Members
149,723
Latest member
krinalle
Top