MZ MZ Random Levelup Effects

Howdy! I'm new to the message boards. Working on a game that I hope to make public or go commercial at some point.

I'm struggling to make something happen that will become a major mechanic in how players learn magic.

Using magic gems the players find, they can temporarily grant themselves magic abilities. The abilities fade in about 30 seconds or after a battle.

Setup: Each magic item grants a state based on that item. While in that state, the character gains access to magic, and a magic spell associated with that item. The items can also be used outside of battle to solve elemental puzzles and/or torment villagers.

If a character levels-up while in a state granted by the magic item, I want that item to have a small chance of teaching the character that magic spell associated with the item permanently. (random number 5% chance?).

To complicate things, my game has lots of playable characters, so doing conditional branches for each character will take months of work.

Many thanks for any input you might have on level events! I would prefer not to use plugins, but am open to them if needed.
To summarize what you want to accomplish:
- "chance to gain particular skill(dependent on state afflicted) if afflicted with a state when leveling"

Is that right? If so, I'd think a plugin might actually be easier to write for that functionality than trying to sort out a manageable means of eventing it.
In fact, I was like "I wonder if I can do that?" and wrote this for ya:
Spoiler: Mostly what you're lookin' for
JavaScript:
/**
* Allows the actor to potentially learn a new skill based on the collection of
* states that they are currently afflicted with.
*/
const _gameActor_levelup_learnSkillByStateChance = Game_Actor.prototype.levelUp;
Game_Actor.prototype.levelUp = function() {
  _gameActor_levelup_learnSkillByStateChance.call(this);

  // iterate over all states currently afflicted on this actor on-levelup.
  const structure = /<learnByStateChance:[ ]?(\d+)[ ]?:[ ]?(\d+)>/i;
  const states = this.states();
  states.forEach(state => {
    // iterate over the notes of each state afflicted.
    const notedata = state.note.split(/[\r\n]+/);
    notedata.forEach(note => {
      // if the note contains the regex structure we care about, proceed.
      if (note.match(structure)) {
        // don't bother if we already know the skill.
        if (this.isLearnedSkill(RegExp.$1)) return;

        // did we hit that lucky chance and learn dat skill?!
        const luckyLearning = (Math.random() * 100) > parseInt(RegExp.$2);
        if (luckyLearning) {
          this.learnSkill(RegExp.$1);
        }
      }
    });
  });
};

If you want an official plugin or something surrounding that, I can probably put one together, but this'll do the trick. I tested it locally and it seemed to work fine (in the context of an ABS mind you).

Oh, and I forgot to mention:
to make it work, you just add some text into a state's notebox:
<learnByStateChance:SKILL_ID:PERCENT_CHANCE>
Where SKILL_ID is the id of the skill to learn
Where PERCENT_CHANCE is the chance as a percent to learn the skill on-levelup.
The tag isn't case sensitive, but it is whitespace sensitive, so I'd recommend keeping it basically without any spaces in the tag.
Moved to: Plugin Requests

Double-post notice: @Jragyn
In fact, I was like "I wonder if I can do that?" and wrote this for ya:
Spoiler: Mostly what you're lookin' for
JavaScript:
/**
* Allows the actor to potentially learn a new skill based on the collection of
* states that they are currently afflicted with.
*/
const _gameActor_levelup_learnSkillByStateChance = Game_Actor.prototype.levelUp;
Game_Actor.prototype.levelUp = function() {
  _gameActor_levelup_learnSkillByStateChance.call(this);

  // iterate over all states currently afflicted on this actor on-levelup.
  const structure = /<learnByStateChance:[ ]?(\d+)[ ]?:[ ]?(\d+)>/i;
  const states = this.states();
  states.forEach(state => {
    // iterate over the notes of each state afflicted.
    const notedata = state.note.split(/[\r\n]+/);
    notedata.forEach(note => {
      // if the note contains the regex structure we care about, proceed.
      if (note.match(structure)) {
        // don't bother if we already know the skill.
        if (this.isLearnedSkill(RegExp.$1)) return;

        // did we hit that lucky chance and learn dat skill?!
        const luckyLearning = (Math.random() * 100) > parseInt(RegExp.$2);
        if (luckyLearning) {
          this.learnSkill(RegExp.$1);
        }
      }
    });
  });
};

If you want an official plugin or something surrounding that, I can probably put one together, but this'll do the trick. I tested it locally and it seemed to work fine (in the context of an ABS mind you).

Oh, and I forgot to mention:
to make it work, you just add some text into a state's notebox:
<learnByStateChance:SKILL_ID:PERCENT_CHANCE>
Where SKILL_ID is the id of the skill to learn
Where PERCENT_CHANCE is the chance as a percent to learn the skill on-levelup.
The tag isn't case sensitive, but it is whitespace sensitive, so I'd recommend keeping it basically without any spaces in the tag.


Hey Jragyn!

Thanks for your input! I'm playing around with this now.

I have a few quick questions for you:

1) Where do I need to put the javascript to get it to work? Does it need to be constantly running in a parallel common event?

2) Is the percent chance in the State Note supposed to be a decimal or an integer? I'm assuming "5" in this space means 5% or 5/100. Does this look accurate?
<learnByStateChance:99:5>

3) What would it take to put together a plugin to handle this a little easier for someone not so familiar with code?
Oh, sorry about that. I tried to edit the post with usage instructions previously, but that probably was glossed over (understandably so, I'm wordy when I type).

For your convenience regardless, I've bundled it together into a proper plugin, with plenty of comments so you can read it and tweak it if you want.

Like any other plugin, you just need to add it into your project, activate it, then follow the instructions. There are no plugin commands or anything. It just... a tiny bit of extra functionality 🙂
Very cool little piece of code!! (I also like the magic gems idea a lot.)

@DeadSchrodinger Whenever someone posts a full code snippet like that, all you need to do is copy-paste it into a text editor, save, add to your project's /js/plugins folder, and then go to the Plugin Manager and add it into the list (and make sure it's turned ON). With the full plugin he gave you, though, you can skip everything before adding it to your project's folder 🙂 And yes, your notetag is correct, I think - just put that in the Note box for a State that temporarily enables use of the Skill.

@Jragyn While I haven't tried it myself, the way I am reading the code, a skill with an intended 5% chance to learn will actually have a 95% chance. Is it possible the line should be this instead?
JavaScript:
const luckyLearning = (Math.random() * 100) < parseInt(RegExp.$2);
@Jragyn While I haven't tried it myself, the way I am reading the code, a skill with an intended 5% chance to learn will actually have a 95% chance. Is it possible the line should be this instead?
JavaScript:
const luckyLearning = (Math.random() * 100) < parseInt(RegExp.$2);

Oh woops, yeah sorry. I get the sign mixed up almost every time lol (and yet I do stuff like this every day). I have updated the attachment for his convenience on that post.

Thanks for the catch (sharp eye!).
(this is why I wish i had more code reviewers for my plugins...)
Very cool little piece of code!! (I also like the magic gems idea a lot.)

@DeadSchrodinger Whenever someone posts a full code snippet like that, all you need to do is copy-paste it into a text editor, save, add to your project's /js/plugins folder, and then go to the Plugin Manager and add it into the list (and make sure it's turned ON). With the full plugin he gave you, though, you can skip everything before adding it to your project's folder 🙂 And yes, your notetag is correct, I think - just put that in the Note box for a State that temporarily enables use of the Skill.

@Jragyn While I haven't tried it myself, the way I am reading the code, a skill with an intended 5% chance to learn will actually have a 95% chance. Is it possible the line should be this instead?
JavaScript:
const luckyLearning = (Math.random() * 100) < parseInt(RegExp.$2);
Does this mean I should insert the number 95 if I want it to be an actual 5% chance? Or do I need to edit the javascript?
Oh, sorry about that. I tried to edit the post with usage instructions previously, but that probably was glossed over (understandably so, I'm wordy when I type).

For your convenience regardless, I've bundled it together into a proper plugin, with plenty of comments so you can read it and tweak it if you want.

Like any other plugin, you just need to add it into your project, activate it, then follow the instructions. There are no plugin commands or anything. It just... a tiny bit of extra functionality 🙂

OMG thank you! I'm trying it now!
Does this mean I should insert the number 95 if I want it to be an actual 5% chance? Or do I need to edit the javascript?
Yes, you could do that for each state... OR you could open the plugin in any text editor, change the > to a < in the line that I described above, Save, and you can use the correct chances like 5 from now on.
I haven't gotten it to work yet. I don't know if I'm doing something wrong.

1616380620287.png


1616380656443.png
I haven't gotten it to work yet. I don't know if I'm doing something wrong.

View attachment 183597

View attachment 183598

If I had to guess, I'd assume its probably because in a standard turn-based battle like the default system is (or with visustella plugins or something), states that are marked as "remove at battle end" probably come off before levelups are calculated, resulting in the system never seeing the state to check when scanning at level up.

(as a sanity check, you may also want to try it with 100% chance to learn at first to ensure its working in any capacity for you)

Original thread: https://forums.rpgmakerweb.com/threads/mz-random-levelup-effects.134446/