MV Notetags Plugin (first timer)

Hello! I am writing a plugin that will take in two separate notetags, one with the additive chain (next in sequence), and one with a subtractive chain (previous in sequence).

This is for skills and states.

First example:
An actor uses skill "Double Team". Double Team causes the actor to increase its Evasion stat by one stage. This will be making use of a unique "State" stacking, where one state leads to another.

We will do this by using .addstate FORWARDS and .removestate (remove the current state id, if any.)

We will also check which state is present when removing state, and which state is active when adding state.

When the skill wears off, we will remove all the states.

On top of this, if the enemy uses a skill that decreases the same actor's evasion Stat, we will use addstate to go through the state id list BACKWARDS and remove the current state id, if any.

The notetags as follows:

for Skills:

Java:
<BUFF PROGRESSION>
(Id 2) State Lv. -5
(id 3) State Lv. -4
(id 4) State Lv. -3
(id 5) State Lv. -2
(id 6) State Lv. -1
(id Null) Neutral state (not an actual state, just a placeholder)
(id 7) State Lv. 1
(id 8) State Lv. 2
(id 9) State Lv. 3
(id 10) State Lv. 4
(id 11) State Lv. 5
(id 12) State Lv. 6
etc
</BUFF PROGRESSION>

<DEBUFF PROGRESSION>
(id 11) State Lv. 5
(id 10) State Lv. 4
(id 9) State Lv. 3
(id 8) State Lv. 2
(id 7) State Lv. 1
(id Null) Neutral state (not an actual state, just a placeholder)
(id 6) State Lv. -1
(id 5) State Lv. -2
(id 4) State Lv. -3
(id 3) State Lv. -4
(id 2) State Lv. -5
(id 1) State Lv. -6
etc
</DEBUFF PROGRESSION>

And this is the script.

JavaScript:
//=============================================================================
// BNTKids-Studios - Stackable States
// BNTK_StackableStates.js
//=============================================================================

var Imported = Imported || {};
Imported.BNTKids_StackableStates = true;

var BNTKids = BNTKids || {};
BNTKids.SS = BNTKids.SS || {};
BNTKids.SS.version = 1.01;

//=============================================================================
 /*:
 * @plugindesc v1.0.1 This plugin allows for buff and debuff skills
 * to process state stacking
 * @author BNTKids-Studios
 * @Credits Yanfly Engine Plugins for plugin setup.
 *
 * @param ---Basic---
 * @default
 *
 * @param Minimum Stage
 * @parent ---Basic---
 * @type number
 * @desc The minimum threshold of stack stages. This is used for
 * calculating from the debuff stage bottom number to the neutral
 * 'central' zero stage number.
 * @default 6
 *
 * @param Maximum Stage
 * @parent ---Basic---
 * @type number
 * @desc The maximum threshold of stack stages. This is used for
 * calculating from the debuff stage bottom number to the neutral
 * 'central' zero stage number.
 * @default 6
 *
 * @help
 *
 * ============================================================================
 * Introduction
 * ============================================================================
 *
 * This plugin allows for buff and debuff skills to process state stacking.
 * Buffing and debuffing can be done in sequential order, allowing for
 * ease of use.
 *
 * ============================================================================
 * Notetags
 * ============================================================================
 *
 * Use the following notetags below to allocate stages for buff and debuff skills.
 *
 * NOTE: As of version 1.0.0, this plugin only supports sequentially ordered and
 * and organized states!
 * This means that the lowest stage of the debuffs must be at the bottom of the state
 * list, followed immediately by the next lowest, up to neutral (no stage buff or debuff),
 * then to the buff list, starting with the lowest buff (1), up to the highest buff, with
 * NO gaps or other unrelated buffs, debuffs, or states between!
 * Another note, to get this to work, you'll need to add the corresponding state lowest stage
 * for buff, highest stage for debuff.
 *
 * Skill and Item Notetags:
 *
 *     NOTES:
 *     For a system with 3 debuff and 3 buff stages.
 *     States start with Health Lv. -3, which is state id 11.
 *     State id 14 is Health Lv. 1
 *
 *         <BUFF PROGRESSION>
 *         Note: In version 1.0.0, The system already assumes that with buff progression, you don't need the lowest buff tier, since you can't increase towards it.
 *         Health Lv. -2
 *         State 13
 *         Normal Health
 *         -State 14- Name or just -State 14- (with no '-', spaces, or 'Name or just'. For example, 'Health 1' or 'State 14'
 *         State 15
 *         Health 3
 *         </BUFF PROGRESSION>
 *       
 *         <DEBUFF PROGRESSION>
 *         Note: In version 1.0.0, The system already assumes that with debuff progression, you don't need the highest debuff tier, since you can't decrease towards it.
 *         Heath Lv. 2
 *         State 15
 *        Normal Health
 *         State 14
 *         State 13
 *         Health Lv. -3
 *         </DEBUFF PROGRESSION>
 *
*/
//=============================================================================
// Parameter Variables
//=============================================================================

BNTKids.Parameters = PluginManager.parameters('BNKids_StackingStates');
BNTKids.Param = BNTKids.Param || {};


 BNTKids.Param.MinimumStage  = Number(BNTKids.Parameters['Minimum Stage'] || 6);
 BNTKids.Param.MaximumStage  = Number(BNTKids.Parameters['Maximum Stage'] || 6);

//=============================================================================
// DataManager
//=============================================================================

BNTKids.SS.DataManager_isDatabaseLoaded = DataManager.isDatabaseLoaded;
DataManager.isDatabaseLoaded = function() {
  if (!BNTKids.SS.DataManager_isDatabaseLoaded.call(this)) return false;
  if (!BNTKids.SS._loaded_BNTKids_StackingStates) {
    this.processSSNotetags1($dataStates, BNTKids.Param.MinimumStage, BNTKids.Param.MaximumStage);
    BNTKids._loaded_BNTKids_StackingStates = true;
  }
  return true;
};


DataManager.processSSNotetags1 = function(group) {
  var note1 = /<(?:BUFF PROGRESSION)>/i;
  var note2 = /<\/(?:BUFF PROGRESSION)>/i;
  var note3 = /<(?:DEBUFF PROGRESSION)>/i;
  var note4 = /<\/(?:DEBUFF PROGRESSION)>/i;
  for (var n = 1; n < group.length; n++) {
        var obj = group[n];
        var notedata = obj.note.split(/[\r\n]+/);

    obj.customstates1 = [];
    obj.customstates2 = [];
    var BuffPatternFlag = false;
    var DebuffPatternFlag = false;
   
        for (var i = 0; i < notedata.length; ++i) {
            var line = notedata[i];
            if (line.match(note1)) {
                BuffPatternFlag = true;
            } else if (line.match(note2)) {
                BuffPatternFlag = false;
            } else if (line.match(note3)) {
                DebuffPatternFlag = true;
            } else if (line.match(note4)) {
                DebuffPatternFlag = false;
            } else if (BuffPatternFlag) {
                obj.customstates1.push(line);
            } else if (DebuffPatternFlag) {
                obj.customstates2.push(line);
      }
        }
    }
};

//=============================================================================
// Game_Battler
//=============================================================================

//Replace the original function's workings.
// M05) State progression.
void (() => {
'use strict';


//Retrieve the strings within BUFF PROGRESSION and DEBUFF PROGRESSION

    /** Name of notetag. */
    const TAG_NAME = 'nextStack';

    /** Anti-loop device. */
    const chain = [];

    /**
     * @param {string} 'tag' tag value from original state
     * @returns {number} 'ID' of next state to apply (0 = none).
     */
    const getNextState = function(tag) {
        return parseInt(tag, 10) || 0;
    };

    // Store current method for later reference.

//    const alias = Game_Battler.prototype.addState;
    // Redefine method.
    Game_Battler.prototype.addState = function(stateId) {
      //Make sure we check if adding the state was done by a skill.
        if (this.customstates1().length <= 0) {
            console.log("Line matches!")
            //First, check if we have no debuff
            for (var i = 0, s = stateId; i < this.customstates1().length; ++i, ++s) {
                if (this.isStateAffected(s)){
                    stateAffected = true;
                    console.log(stateAffected)                   
                }
            }
            if (!stateAffected) {
                //Start from level 1. We do this by calculating the number of debuffs and buffs together, then subtracting the buffs.
                const numstages =  BNTKids.Param.MinimumStage +  BNTKids.Param.MaximumStage;
                const stage1 = numstages - MaximumStage + 1;
               
                //apply the first buff from there.
                this.addState(stage1);
                console.log("Applied stage 1 state")
            }
            else if (stateAffected) {
                //figure out which stage we're on.
                for (var i = 0, s = stateId; i < this.customstates1().length; ++i, ++s) {
                    if (this.isStateAffected(s)) {
                        //if we've found a suitable stage, immediately put this state on and return.
                        const stagenum = s;
                        if (stagenum) {
                            this.addState(stagenum + 1);
                            this.removeState(stagenum);
                            console.log("Applied", stagenum + 1, "state")
                            return;
                        }                       
                    }
                }
            }
        }
        else if (this.customstates2().length <= 0) {
            console.log("Line matches!")
            //First, check if we have no debuff
            for (var i = 0, s = stateId; i < this.customstates2().length; ++i, --s) {
                if (this.isStateAffected(s)){
                    stateAffected = true;
                }
            }
            if (!stateAffected) {
                //Start from level 1. We do this by calculating the number of debuffs and buffs together, then subtracting the buffs.
                const numstages =  BNTKids.Param.MinimumStage +  BNTKids.Param.MaximumStage;
                const stage1 = numstages - MaximumStage - 1;
               
                //apply the first buff from there.
                this.addState(stage1);
                console.log("Applied stage -1 state")
            }
            else if (stateAffected) {
                //figure out which stage we're on.
                for (var i = 0, s = stateId; i < this.customstates2().length; ++i, --s) {
                    if (this.isStateAffected(s)) {
                        //if we've found a suitable stage, immediately put this state on and return.
                        const stagenum = s;
                        if (stagenum) {
                            this.addState(stagenum - 1);
                            console.log("Applied stage", stagenum - 1, "state")
                            this.removeState(stagenum);
                            return;
                        }                       
                    }
                }
            }
        }           
        else if (this.isStateAddable(stateId)) {
            if (!this.isStateAffected(stateId)) {
                this.addNewState(stateId);
                this.refresh();
            }
            this.resetStateCounts(stateId);
            this._result.pushAddedState(stateId);
        }
    };

   

})();

the problem I keep getting is trying to check the inputted notedata within the Game_Battler.

JavaScript:
    Game_Battler.prototype.addState = function(stateId) {
      //Make sure we check if adding the state was done by a skill.
        if (this.customstates1().length <= 0) {

Will result in

"This.customstates1 is not a function"

Or something along those lines.

I tried

JavaScript:
this.item().customstates1.length
this.battler().customstates1.length
this.states().customstates1.length

What am I doing wrong?
YEP buff/stateCore has a stacking I believe, which could be used.

base stack can be 3 and 0 for negative outcome.
as you can do state stacking and add what it does, if state is 0, give it a negative number, 3 to neutral.

so what is different between this and YEP's plugin that could achieve this?
"This.customstates1 is not a function"

obj.customstates1 = []; obj.customstates2 = [];

if (this.customstates1().length <= 0) {

Well - the error message is correct, you defned customstates1 as an array [], not as a function.
but then you tried to call it as a function ()
if (this.customstates1().length <= 0) {
another issue I see, while its not a function based on () is the following:

JavaScript:
if (this.customstates1().length <= 0) {

// too
    
if (this.customstates1.length - 1) {

as an array can NEVER be lower than 0 (mostly null/undefined)

so you check the length - 1 (to get empty as 0 = first index).

so you have to check carefully how you make function from array as it is 2 different things like Andar said.
YEP buff/stateCore has a stacking I believe, which could be used.

base stack can be 3 and 0 for negative outcome.
as you can do state stacking and add what it does, if state is 0, give it a negative number, 3 to neutral.

so what is different between this and YEP's plugin that could achieve this?
Yep's buff/state Core, to my knowledge, does not assign individual states for each stack. It only assigns 1 state, and thus, you cannot change the icon of the state this way. There's another flaw behind Yep's method, but I don't recall what it is at the moment (I believe it had to do with retrieving the state's buff stack value, which, technically can be done via a script call, but I don't think skills have script calls like that) This plugin will allow you to do just these!
Well - the error message is correct, you defned customstates1 as an array [], not as a function.
but then you tried to call it as a function ()
Yep, I just realized that. That part was my new code I forgot to switch back.

But, as I stated above, doing

JavaScript:
this.item().customstates1.length
this.battler().customstates1.length
this.states().customstates1.length

and even doing

JavaScript:
this.customstates1.length

results in an older error.

Code:
TypeError
Cannot read property 'length' of undefined.


I was under the impression that with

JavaScript:
obj.customstates1.push(line);

that would push each line to the next index of the array


JavaScript:
obj.customstates1 = [];

But when I try to retrieve length from customstates1, it returns the property 'length' is part of an undefined method (or maybe length is undefined?)
it returns the property 'length' is part of an undefined method (or maybe length is undefined?)
wrong - you need to be more precise in reading error messages.
Cannot read property 'length' of undefined.

The important part here is the OF undefined.
length does exist and is defined, what is undefined is the parent of length

it cannot read the property lenght of an undefined data object

which means in
this.customstates1.length

"this.customstates1" is the undefined data object.
wrong - you need to be more precise in reading error messages.


The important part here is the OF undefined.
length does exist and is defined, what is undefined is the parent of length

it cannot read the property lenght of an undefined data object

which means in


"this.customstates1" is the undefined data object.

So, how do I make it defined? Thought I defined it above at this part:

JavaScript:
DataManager.processSSNotetags1 = function(group) {
  var note1 = /<(?:BUFF PROGRESSION)>/i;
  var note2 = /<\/(?:BUFF PROGRESSION)>/i;
  var note3 = /<(?:DEBUFF PROGRESSION)>/i;
  var note4 = /<\/(?:DEBUFF PROGRESSION)>/i;
  for (var n = 1; n < group.length; n++) {
        var obj = group[n];
        var notedata = obj.note.split(/[\r\n]+/);

    obj.customstates1 = [];
    obj.customstates2 = [];
    var BuffPatternFlag = false;
    var DebuffPatternFlag = false;
    
        for (var i = 0; i < notedata.length; ++i) {
            var line = notedata[i];
            if (line.match(note1)) {
                BuffPatternFlag = true;
            } else if (line.match(note2)) {
                BuffPatternFlag = false;
            } else if (line.match(note3)) {
                DebuffPatternFlag = true;
            } else if (line.match(note4)) {
                DebuffPatternFlag = false;
            } else if (BuffPatternFlag) {
                obj.customstates1.push(line);
            } else if (DebuffPatternFlag) {
                obj.customstates2.push(line);
            }
        }
    }

I am trying a different method now.

For the following code, I assume "battler" is Game_Battler.


JavaScript:
Game_Battler.prototype.addState = function(stateId) {
      //Make sure we check if adding the state was done by a skill.
        var length = this.battler().length;
        for (var i = 0; i < length; ++i) {
          var obj = this.battler()[i];
        }
        var array1 = obj.customstates1;
        var array2 = obj.customstates2;
        var length1 = array1.length;
        var length1 = array2.length;
        if (length1 >= 1) {
            console.log("Line matches!")
            //First, check if we have no debuff
            for (var i = 0, s = stateId; i < length1; ++i, ++s) {
                if (this.isStateAffected(s)){
                    stateAffected = true;
                    console.log(stateAffected)                   
                }
            }

But then I get the error message

Cannot Read porperty 'customsates1' of undefined

Based on what you said above, it is telling me that 'battler' is undefined.

Isn't 'Battler' defined within Game_Battler?

If not, what is the correct term?
Based on what you said above, it is telling me that 'battler' is undefined.
again no

it tells you that this.battler() is undefined. That is something different than claiming battler() is undefined.

"this" is a context-dependent shortcut that gets replaced with whatever data object is defined as the context.

so even if Battler is defined in Game_Battler, if the "this" points to a context different from Game_Battler, then the result would still be that Battler is not defined under whatever context is described by "this".

Unfortunately I don't know the engine's code enough to tell you in which context Battler is defined or in which context you placed your definition of customstates1, but they are clearly different data objects based on your error message.
JavaScript:
// either
  var note1 = /<(?:BUFF[ ]PROGRESSION)>/i;
  var note2 = /<\/(?:BUFF[ ]PROGRESSION)>/i;
  var note3 = /<(?:DEBUFF[ ]PROGRESSION)>/i;
  var note4 = /<\/(?:DEBUFF[ ]PROGRESSION)>/i;

// or
  var note1 = /<(?:BUFF\s*PROGRESSION)>/i;
  var note2 = /<\/(?:BUFF\s*PROGRESSION)>/i;
  var note3 = /<(?:DEBUFF\s*PROGRESSION)>/i;
  var note4 = /<\/(?:DEBUFF\s*PROGRESSION)>/i;
is a save approach as direct space isn't there.

I would go for the second one as its more reliable if you ask me 🙂

you could also parse the note1/2/3/4 directly into the check

JavaScript:
if (line.match(/<(?:BUFF\s*PROGRESSION)>/i))

but that's up to you 🙂
Code:
this.processSSNotetags1($dataStates, BNTKids.Param.MinimumStage, BNTKids.Param.MaximumStage);

You are calling this method with 3 arguments, but only the first one seems to be used. I guess you plan to use the two other argumentsin the future.

Code:
obj.customstates1.push(line);

Here "obj" is a data item (a state).


Code:
Game_Battler.prototype.addState = function(stateId) {
      //Make sure we check if adding the state was done by a skill.
        if (this.customstates1().length <= 0) {

You are checking if the game object of the battler is assigned the custom property "customstates1".
In my opinion you should check something like

Code:
if ($dataStates[stateId].customstates1.length <= 0)

Original thread: https://forums.rpgmakerweb.com/threads/notetags-plugin-first-timer.177409/