How to remove/regain skills?

I've recently devised a way to allow the player to switch classes mid game. I realized there was an issue though, let's say the player is level 5 in class A. They've unlocked 3 skills, but now they want to try class B. They switch over and see that they still have the skills from class A. This is a problem, I want to make it so that whenever the player switches classes, they do not have any of the skills from the previous class.

I did some searching and found this code from a previous post on the forums here, here's the code I'm referring to. Full credit to the original poster Trihan for the code.
const actor = $gameActors.actor(1);
[I]const skills = actor.skills();[/I]
[I]for (let i = 0; i < skills.length; i++) {[/I]
[I] actor.forgetSkill(skills[i].id);[/I]
}
This code allows me to remove all learned skills, I plugged it in to the common event prior to switching classes so now... Whenever the player switches from class A to class B, they have no skills from the previous class. Problem solved right? Well no...

You see, I'd like to make it so that whenever the player returns to class A - that they regain the skills they've already learned without the hassle of learning all of them all over again. It would be a pain to have to watch the text say "So and so learned this skill!" every single time the player switched classes. On top of that, I have a system involved so that the player must choose which skills they wish to keep as they only have access to 8 at a time. If switching classes requires the player to reselect every skill over and over, that would be a serious problem. To top it all off, the player has access to skill cards which means they can teach a specific class a skill that isn't even available to learn from leveling up.

So my question is as the title suggests, is there a plugin or script that I could run to retain the skills that each class contains as to allow for a fresh start when switching classes and retaining skills when returning to a previously leveled class? Thanks for your time and I appreciate all help given!
I'm guessing you'd have to save actor.skills() in a new array so you could retrieve it later. You might end up having a lot of arrays though if you're doing this for every actor for every class, so you might need to save the data in an object instead.

Or you could just ask @Trihan since it's his script.
Hey,

the easiest way would probably be to give every class access to a different skill type. This way you would keep all the skills you learned, but would only be able to see/use the skills that are of the skill type visible for your current class (e.g. "Magic" skills would only be accessible while on the "Magician" class that has this skill type added in its traits).

If you want to re-learn skills through re-gaining exp, you can disable the "Learned Skill" messages in the event command itself.

If you still want to store the previously learned skills in a way where they don't get lost, you can save the list of skills (or better a list of their Ids) in a variable or in a new field of the actor itself.
Note that the list currently may contain temporary skills that were only gained through traits:
JavaScript:
const actor = $gameActors.actor(1);
const skills = actor.skills();

// Create a list of skill ids instead of whole skill entries:
let skillIds = skills.map(skill => skill.id);
// Save that list (in this case in a variable)
$gameVariables.setValue(1, skillIds);

// ...

// Relearn the stored skills later:
for (let skillId of $gameVariables.getValue(1)) {
    actor.learnSkill(skillId);
}

Edit: A bit slow
I greatly appreciate the replies! On top of that, I cannot thank you enough for thinking outside of the box. I would have never considered using unique skill types within the database to specify skill options. I'm going to give it a test to see how it goes, if it's to my liking; I'll stick with it. Otherwise I'll utilize the code you provided, either way I'm certain it will help others in the future who may run into this post.

Again, I really appreciate the help!
That should work for what you need, but if not I'll help you modify the original code.

Original thread: https://forums.rpgmakerweb.com/threads/how-to-remove-regain-skills.167782/