Cast Random Known Skill

I've been trying to create a skill that will cast a random skill from a specific skill type. Scripts like Yanfly's Random Skill Invoke isn't exactly what I need because it needs to be a random selection of skills that the actor already learned.


I.E.


Special skill 'Random' will randomly select a Magic skill from the Actor. If the actor knows the spells Cure, Fire, and Ice, it will randomly cast one of those spells and nothing else.
I've moved this thread to RGSS3 Script Requests . Please be sure to post your threads in the correct forum next time. Thank you.
I've moved this thread to RGSS3 Script Requests . Please be sure to post your threads in the correct forum next time. Thank you.

Damn, I'm normally good about that, I was tired when posting this. Thanks Archeia. >.>
This is a confusion snippet that someone made years ago:


class Game_Action
 
# Overwrite
 
def set_confusion
    possible_skills
= subject.usable_skills << $data_skills[subject.attack_skill_id]
    set_skill
(possible_skills.sample.id)
 
end
end


As far as I can tell, this first creates a new array based on your current skill array (subject.usable_skills) and << adds the attack skill to the end of that array (so you don't need that part).  The second line then sets the skill based on .sample which returns a random item from the array, so [ice, fireball, bolt].sample could return fireball...though set_skill seems to require an id number hence the fireball.id/[ice, fireball, bolt].sample.id.


Hm, it seems that set_skill pretty much prepares the skill for use, so what if we modified that so that when it selects your random skill, it will choose a random number at this point?  Let's try pasting this snippet in your empty space:

Code:
class Game_Action

def set_skill(skill_id)
  skill_id = subject.usable_skills.sample.id if skill_id == RANDOM # replace RANDOM with the id/database number of this skill

  @item.object = $data_skills[skill_id]
  self
end

end
@Harosata The script works pretty well and does what you predict, but I just need one or two more tweaks.


First is a bit more crucial, is it possible for the skill_ids in the snipped collected only be that of one type? For example, RPG Maker VX ace has Special skills and Magic skills by default, what would nedd to be changed so the skill_ids collected are only magic skills?


Second I can live without, but will ask anyways. Is there a condition that can be added that will only cast skills if the actor has the means to do so? For example, it won't select skills that require more MP than the actor currently has. If all skills require more mp than what the actor currently has, the nothing happens.


Edit: After additional testing, I learned it already has this. I was confusing it with the first issue, since the skill that calls the spells is a special skill type that does nothing, and has a chance to calling itself, which results with a failed skill.


Outside of that, I'm very thankful for this. I've been very frustrated trying to event this myself, but it's a relief to know most of what I want is answered in a couple lines.
Oh, I guess there was issue after all.  Hm, first, probably easier and safer to make a new array to hold your skills.  Second, I didn't consider the issue of the skill itself being selected.  Let's see, two ways to deal with this is either to loop the results if that id is selected or to drop the results from the current array.  Yanfly's Target Manager shows an array in the example of "everyone but user", so we might be looking at something like this:


random_skill_array = subject.usable_skills


random_skill_array -= $data_skills[RANDOM]


As for skill types, skill_type_sealed under Game_BattlerBase shows stype_id, and usable_skills under Game_Actor shows that the array comes from skills.select, meaning that it returns an array of qualifying items like how you would put all ice cream from your groceries into your freezer.  I also checked Window_SkillList.


Anyway, here's the correction, if I'm correct:


#Replace RANDOM with your skill id.
#Replace MAGICTYPE with your type id.

class Game_Actor < Game_Battler

def random_spell_id
random_spell = usable_skills
random_spell -= [$data_skill[RANDOM]] #You could remove this since you said RANDOM skill is a different skill type.
random_spell.select {|item| item.stype_id == MAGICTYPE}
return random_spell.sample.id
end

end

class Game_Action

def set_skill(skill_id)
skill_id = subject.random_spell_id if skill_id == RANDOM
@item.object = $data_skills[skill_id]
self
end

end


If removing the Random skill doesn't work, you could remove that line and try random_spell.select {|item| item.stype_id == MAGICTYPE and item.id != RANDOM} instead.  Or since you say RANDOM is a Special and you're only casting a random Magic type, you could just remove the -= data skills line.


Also, I haven't the opportunity to test this, but how would this affect MP?  Does the MP get removed based on Random's cost or the Magic's cost, and which cost are you looking for?
@HarosataFirst was an issue on my end. When the script searches for usable skills, it takes resource cost into account. However, the new array still works fine. When using skills, it adds the MP of the Random skill's cost and the Magic cost, I don't plan on having random cost anything, so it works fine in that aspect.


However, it doesn't quite solve the second issue. What's posted above results in an error, and the other two options does not bring any different results compared to the previous snippet you have posted. One thing I could do, however, is use Yanfly's Battle Command script. I could remove the Skill ID from the actor then use Yanfly's script to still make the skill accessible from the actor's command window. Since the skill is not considered to be learned by the actor, it won't call itself when used. I've tested this myself and it works fine. The obvious issue with this is that my actor can't learn any other skill outside of Magic, but it's a sacrifice I'm willing to make.


Regardless, the request is pretty much solved for the moment. So thank you for your help. 🙂


I'll leave the thread open for a bit longer in case anyone has an answer to my last issue, however I'll request that it will be closed before the end of the day.
This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/cast-random-known-skill.74085/