[SOLVED] Help Please with a function I'm Writing

Hi everyone. I'm wracking my brain trying to figure out why this function refuses to work.

I'm writing a fairly simple plugin that manipulates game variables to keep track of each actor's stat growth at each level up.
Basically I'm recreating the way that Final Fantasy 1 for NES handles level ups.

Initially I wrote out a large block of code with a switch for each of my four actor just to make sure my code worked, and it did.
Now I'm on step two. I'm refining my code to use additional functions.
So far everything works perfectly, except my HP and MP growth functions refuse to work.
All the individual functions are nearly identical, so I have no clue why these in particular won't work.

Here is one of my functions that works without issues:

growSTR(this._actorId); //function call

growSTR = function (idActor) //grow Strength stat
{ var idSTR = 0;
switch (idActor)
{ case 1: idSTR = 23; break;
case 2: idSTR = 43; break;
case 3: idSTR = 63; break;
case 4: idSTR = 83; break;
default: break;
};
$gameVariables.setValue(idSTR, $gameVariables.value(idSTR)+1);
};

At level up this works like a charm.
Each actor has a custom base stat.
That stat has the value of a specific game variable added to it.
And each class has a a bonus added on top of those.
This all works as it should, cumulatively gaining 1 at each level up.

Here is one of my functions that refuses to work no matter what I do to it.

s_growHP(this._actorId); //function call

s_growHP = function (idActor) //grow Health Points strongly
{ var idsHP = 0;
switch (idActor)
{ case 1: idsHP = 21; break;
case 2: idsHP = 41; break;
case 3: idsHP = 61; break;
case 4: idsHP = 81; break;
default: break;
};
var i = 0;
i = Math.floor(this.c_vit/4); //get vit based hp+
i += 20; //get strong hp+
i += Math.randomInt(6); //get strong hp+ varience
$gameVariables.setValue(idsHP, $gameVariables.value(idsHP)+i);
};

This code worked perfectly fine outside the function before.
I've tried changing the function names, the declared variable names, not using variables, nothing works.
Can any of you see anything wrong with this?
Are you getting any errors in your console? If you haven't tried that yet, open the console by pressing F8 while your game is running. If JavaScript throws any errors, they will show up there.
Thanks for replying. No. I'm not getting any errors in the console.

Edit: I took my code and put back in the main function.
This time it worked.
So I can only conclude that there is a problem with the function call?
Where/when are you calling it? From where? Has the context been set up at this time (is there a scope issue, have the variables not been created)?
This is the whole thing.
Spoiler
Code:
//=============================================================================
// VANYA Class Based Preset Level Up Effects
// by Vanya / Ivan Esteban-Garcia
// Date: 03/01/2018
//
// made with:
// Toms_Example Template
// by Faytless / Thomas Pham
// Date: 10/25/2015
//=============================================================================
/*:
 * @plugindesc v1.00 This plugin allows you to designate specific effects
 * for each level up of each of your classes.
 * @author Vanya / Ivan Esteban-Garcia
 *
 * @param ---Test 2 Parent---
 * @default
 *
 * @param Test Parameter
 * @parent ---Test 2 Parent---
 * @type number
 * @min 0
 * @desc A parameter for testing this plugin.
 * Default: 0
 * @default 0
 *
 * @help
 * ============================================================================
 * Introduction
 * ============================================================================
 *
 * Stuff
 *
 * ============================================================================
 * Explanation
 * ============================================================================
 *
 * Stuff
 *
 * ============================================================================
 * Changelog
 * ============================================================================
 *
 * Version 1.00:
 * - Initial Release
 *
 */

//=============================================================================
// CODE
//=============================================================================

var parameters = PluginManager.parameters('VANYA_Class_Preset_Level_Up');
var alias_levelUp = Game_Actor.prototype.levelUp;

Game_Actor.prototype.levelUp = function()
{   alias_levelUp.call(this);
   switch (this._classId)                               //check for class
   {   case 1:                                               //Warrior
           switch (this._level)                           //check for level
           {   case 2:                                       //level 2
               {   growSTR(this._actorId);
                   growAGI(this._actorId);
                   growVIT(this._actorId);
                   r_growINT(this._actorId);
                   r_growLCK(this._actorId);
                   growDEX(this._actorId,this.c_dgr);
                   growSPT(this._actorId,this.c_sgr);
                   HPup(this._actorId); //This refuses to work
               /*   var idActor = this._actorId; //These line work perfectly if I remove the comment tags
                   var idHP = 0;
                   switch (idActor)
                   {   case 1: idHP = 21; break;
                       case 2: idHP = 41; break;
                       case 3: idHP = 61; break;
                       case 4: idHP = 81; break;
                       default: break;
                   };
                   var i = 0;
                   i = Math.floor(this.c_vit/4);                   //get vit based hp+
                   $gameVariables.setValue(idHP, $gameVariables.value(idHP)+i);
              */
               };
               break;
            
               default:
               break;
           }
       break;
    
       case 2:                                               //Thief
           switch (this._level)                           //check for level
           {   case 2:                                       //level 2
               {   growSTR(this._actorId);
                   growAGI(this._actorId);
                   r_growVIT(this._actorId);
                   r_growINT(this._actorId);
                   growLCK(this._actorId);
                   growDEX(this._actorId,this.c_dgr);
                   growSPT(this._actorId,this.c_sgr);
                   s_HPup(this._actorId);
               };
               break;
            
               default:
               break;
           }
       break;
    
       case 3:                                               //Monk
           switch (this._level)                           //check for level
           {   case 2:                                       //level 2
               {   r_growSTR(this._actorId);
                   growAGI(this._actorId);
                   growVIT(this._actorId);
                   r_growINT(this._actorId);
                   growLCK(this._actorId);
                   growDEX(this._actorId,this.c_dgr);
                   growSPT(this._actorId,this.c_sgr);
                   s_HPup(this._actorId);
               };
               break;
            
               default:
               break;
           }
       break;
    
       case 4:                                               //Red Mage
           switch (this._level)                           //check for level
           {   case 2:                                       //level 2
               {   growSTR(this._actorId);
                   r_growAGI(this._actorId);
                   growVIT(this._actorId);
                   r_growINT(this._actorId);
                   growLCK(this._actorId);
                   growDEX(this._actorId,this.c_dgr);
                   growSPT(this._actorId,this.c_sgr);
                   s_HPup(this._actorId);
               };
               break;
            
               default:
               break;
           }
       break;
    
       case 5:                                               //White Mage
           switch (this._level)                           //check for level
           {   case 2:                                       //level 2
               {   growSTR(this._actorId);
                   growAGI(this._actorId);
                   r_growVIT(this._actorId);
                   growINT(this._actorId);
                   growLCK(this._actorId);
                   growDEX(this._actorId,this.c_dgr);
                   growSPT(this._actorId,this.c_sgr);
                   s_HPup(this._actorId);
               };
               break;
            
               default:
               break;
           }
       break;
    
       case 6:                                               //Black Mage
           switch (this._level)                           //check for level
           {   case 2:                                       //level 2
               {   r_growSTR(this._actorId);
                   r_growAGI(this._actorId);
                   r_growVIT(this._actorId);
                   growINT(this._actorId);
                   r_growLCK(this._actorId);
                   growDEX(this._actorId,this.c_dgr);
                   growSPT(this._actorId,this.c_sgr);
                   s_HPup(this._actorId);
               };
               break;
            
               default:
               break;
           }
       break;
    
       case 7:                                               //Paladin
           break;
    
       case 8:                                               //Ninja
           break;
    
       case 9:                                               //Mage Knight
           break;
    
       case 10:                                           //Green Mage
           break;
    
       case 11:                                           //Time Mage
           break;
    
       case 12:                                           //Evoker
           break;
    
       default:
       break;
   }
};

//=============================================================================
// Stat Growth Functions
//=============================================================================

growSTR = function (idActor) //grow Strength stat
{   var idSTR = 0;
   switch (idActor)
   {   case 1: idSTR = 23; break;
       case 2: idSTR = 43; break;
       case 3: idSTR = 63; break;
       case 4: idSTR = 83; break;
       default: break;
   };
   $gameVariables.setValue(idSTR, $gameVariables.value(idSTR)+1);
};

growAGI = function (idActor) //grow Agility stat
{   var idAGI = 0;
   switch (idActor)
   {   case 1: idAGI = 24; break;
       case 2: idAGI = 44; break;
       case 3: idAGI = 64; break;
       case 4: idAGI = 84; break;
       default: break;
   };
   $gameVariables.setValue(idAGI, $gameVariables.value(idAGI)+1);
};

growVIT = function (idActor) //grow Vitality stat
{   var idVIT = 0;
   switch (idActor)
   {   case 1: idVIT = 25; break;
       case 2: idVIT = 45; break;
       case 3: idVIT = 65; break;
       case 4: idVIT = 85; break;
       default: break;
   };
   $gameVariables.setValue(idVIT, $gameVariables.value(idVIT)+1);
};

growINT = function (idActor) //grow Inellect stat
{   var idINT = 0;
   switch (idActor)
   {   case 1: idINT = 26; break;
       case 2: idINT = 46; break;
       case 3: idINT = 66; break;
       case 4: idINT = 86; break;
       default: break;
   };
   $gameVariables.setValue(idINT, $gameVariables.value(idINT)+1);
};

growLCK = function (idActor) //grow Luck stat
{   var idLCK = 0;
   switch (idActor)
   {   case 1: idLCK = 27; break;
       case 2: idLCK = 47; break;
       case 3: idLCK = 67; break;
       case 4: idLCK = 87; break;
       default: break;
   };
   $gameVariables.setValue(idLCK, $gameVariables.value(idLCK)+1);
};

r_growSTR = function (idActor) //grow Strength stat
{   var idSTR = 0;
   switch (idActor)
   {   case 1: idSTR = 23; break;
       case 2: idSTR = 43; break;
       case 3: idSTR = 63; break;
       case 4: idSTR = 83; break;
       default: break;
   };
   if (Math.random() <= 0.25) {                                   //25% chance
       $gameVariables.setValue(idSTR, $gameVariables.value(idSTR)+1);
   };
};

r_growAGI = function (idActor) //grow Agility stat
{   var idAGI = 0;
   switch (idActor)
   {   case 1: idAGI = 24; break;
       case 2: idAGI = 44; break;
       case 3: idAGI = 64; break;
       case 4: idAGI = 84; break;
       default: break;
   };
   if (Math.random() <= 0.25) {                                   //25% chance
       $gameVariables.setValue(idAGI, $gameVariables.value(idAGI)+1);
   };
};

r_growVIT = function (idActor) //grow Vitality stat
{   var idVIT = 0;
   switch (idActor)
   {   case 1: idVIT = 25; break;
       case 2: idVIT = 45; break;
       case 3: idVIT = 65; break;
       case 4: idVIT = 85; break;
       default: break;
   };
   if (Math.random() <= 0.25) {                                   //25% chance
       $gameVariables.setValue(idVIT, $gameVariables.value(idVIT)+1);
   };
};

r_growINT = function (idActor) //grow Inellect stat
{   var idINT = 0;
   switch (idActor)
   {   case 1: idINT = 26; break;
       case 2: idINT = 46; break;
       case 3: idINT = 66; break;
       case 4: idINT = 86; break;
       default: break;
   };
   if (Math.random() <= 0.25) {                                   //25% chance
       $gameVariables.setValue(idINT, $gameVariables.value(idINT)+1);
   };
};

r_growLCK = function (idActor) //grow Luck stat
{   var idLCK = 0;
   switch (idActor)
   {   case 1: idLCK = 27; break;
       case 2: idLCK = 47; break;
       case 3: idLCK = 67; break;
       case 4: idLCK = 87; break;
       default: break;
   };
   if (Math.random() <= 0.25) {                                   //25% chance
       $gameVariables.setValue(idLCK, $gameVariables.value(idLCK)+1);
   };
};

growDEX = function (idActor,userDGR) //grow Dexterity stat
{   var idDEX = 0;
   switch (idActor)
   {   case 1: idDEX = 28; break;
       case 2: idDEX = 48; break;
       case 3: idDEX = 68; break;
       case 4: idDEX = 88; break;
       default: break;
   };
   $gameVariables.setValue(idDEX, $gameVariables.value(idDEX)+userDGR);
};

growSPT = function (idActor,userSGR) //grow Spirit stat
{   var idSPT = 0;
   switch (idActor)
   {   case 1: idSPT = 29; break;
       case 2: idSPT = 49; break;
       case 3: idSPT = 69; break;
       case 4: idSPT = 89; break;
       default: break;
   };
   $gameVariables.setValue(idSPT, $gameVariables.value(idSPT)+userSGR);
};

HPup = function (idActor) //grow Health Points
{   var idHP = 0;
   switch (idActor)
   {   case 1: idHP = 21; break;
       case 2: idHP = 41; break;
       case 3: idHP = 61; break;
       case 4: idHP = 81; break;
       default: break;
   };
   var i = 0;
   i = Math.floor(this.c_vit/4);                   //get vit based hp+
   $gameVariables.setValue(idHP, $gameVariables.value(idHP)+i);
};

s_HPup = function (idActor) //grow Health Points strongly
{   var idsHP = 0;
   switch (idActor)
   {   case 1: idsHP = 21; break;
       case 2: idsHP = 41; break;
       case 3: idsHP = 61; break;
       case 4: idsHP = 81; break;
       default: break;
   };
   var i = 0;
   i = Math.floor(this.c_vit/4);                   //get vit based hp+
   i += 20;                                       //get strong hp+
   i += Math.randomInt(6);                           //get strong hp+ varience
   $gameVariables.setValue(idsHP, $gameVariables.value(idsHP)+i);
};

MPup = function (idActor) //grow Mana Points
{   var idMP = 0;
   switch (idActor)
   {   case 1: idMP = 22; break;
       case 2: idMP = 42; break;
       case 3: idMP = 62; break;
       case 4: idMP = 82; break;
       default: break;
   };
   var i = 0;
   i = Math.floor(this.c_int/8);                   //get INT based mp+
   $gameVariables.setValue(idMP, $gameVariables.value(idMP)+i);
};

s_MPup = function (idActor) //grow Mana Points strongly
{   var idsMP = 0;

   switch (idActor)
   {   case 1: idsMP = 22; break;
       case 2: idsMP = 42; break;
       case 3: idsMP = 62; break;
       case 4: idsMP = 82; break;
       default: break;
   };
   var i = 0;
   i = Math.floor(this.c_int/8);                   //get INT based mp+
   i += 10;                                       //get strong mp+
   i += Math.randomInt(3);                           //get strong mp+ varience
   $gameVariables.setValue(idsMP, $gameVariables.value(idsMP)+i);
};

I'm experienced at C languages, but a novice at javascript.
So I'm sure there is plenty that I don't know and could be getting wrong.

Edit: It's only the last 4 functions that refuse to work.
The ones related to HP and MP.
I'm assuming that the variables c_dgr, c_sgr, c_int, and c_vit are defined somewhere else in Game_Actor. Those aren't standard variables that are part of the Game_Actor object as far as I can tell, but I don't see them anywhere else in your code sample.

That said, I believe your problem is that you are trying to use the variables c_int and c_vit within your stat growth functions, but those functions are not part of Game_Actor. You will need to pass those values to the functions, like you are passing this._actorId, this.c_dgr, and this.c_sgr to your other functions. Otherwise, their value is simply undefined, so your function isn't changing the attributes. Alternatively, you can make the functions part of Game_Actor.
c_int and c_vit are custom stats created using ICFSoft_ParamsCore.
Looking at the code for that plugin I do see that there is a function called Game_Actor.prototype.NParamBase.
The custom stats are defined there if I'm guessing correctly.

EDIT:
I passed the custom stats to my functions and they worked as expected. Thanks for that.
For future reference, how do I make my functions part of Game_Actor?
Making a function part of Game_Actor (and other classes) is easy and you've made it with level up function.

Here is an example with one of your functions:
Code:
Game_Actor.prototype.HPup = function () //grow Health Points
{   var idHP = 0;
   switch (this._actorId)
   {   case 1: idHP = 21; break;
       case 2: idHP = 41; break;
       case 3: idHP = 61; break;
       case 4: idHP = 81; break;
       default: break;
   };
   var i = 0;
   i = Math.floor(this.c_vit/4);                   //get vit based hp+
   $gameVariables.setValue(idHP, $gameVariables.value(idHP)+i);
};

You can call function inside Game_Actor class with this.HPup()
Thanks a lot for the explanation!
if this is solved, please report your first post and ask the mods to close the thread.
Done. Thanks again.
Thread closed IgnoreMe

Original thread: https://forums.rpgmakerweb.com/threads/solved-help-please-with-a-function-im-writing.92324/