MV Home-made plugin bug, can't find why :(

Hello, i wrote a little home-made plugin to create a really simple gacha pull system, here is the code (sorry, i'm a noob and this code probably looks absolutely barbaric) :

JavaScript:
/*:
 * @plugindesc Gacha-Style Recruitment System
 * @author MIDNIGHT89
 * @version 1.0
 * @help
 * Use "GSRSRecruit" as a plugin command to use
 * MIDNIGHT89
 */


(function() {

// PLUGIN COMMAND
Game_Interpreter.prototype.GSRSRecruit = function() {


// LIST OF POSSIBLE RECRUITS (ACTORS)
var liste_acteurs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];

// MESSAGE IF ALL ACTORS HAVE BEEN RECRUITED
if (liste_acteurs.length === 0) {
$gameMessage.add("Vous avez recruté tous les héros !");
}

else {
// VAR ACTOR ID
var choix_acteur_random;
// RARITY PULL ( 1-6 : Commun, 7-9 : Rare, 10 : Légendaire)
var choix_rarete_aleatoire = Math.floor(Math.random() * 10) + 1;


// ACTOR PULL
do {
    choix_acteur_random = liste_acteurs[Math.floor(Math.random() * liste_acteurs.length)];
} while ($gameParty.members().some(function(actor) {
    return actor.actorId() === choix_acteur_random;
}));

// LIST OF AVAILABLE CLASSES
var liste_classes = [2, 3, 4, 5];
// CLASS PULL
var choix_classe_aleatoire = liste_classes[Math.floor(Math.random() * liste_classes.length)];

// PASSIVE STATE BONUS IF RARE/LEGENDARY
var etat_passif_rarete = 0;

if (choix_rarete_aleatoire >= 7 && choix_rarete_aleatoire <= 9) {
    etat_passif_rarete = 10;
    $gameActors.actor(choix_acteur_random).addState(etat_passif_rarete);
} else if (choix_rarete_aleatoire === 10) {
    etat_passif_rarete = 11;
    $gameActors.actor(choix_acteur_random).addState(etat_passif_rarete);
}

var acteur = $gameActors.actor(choix_acteur_random);

// ADD RECRUIT TO TEAM
$gameParty.addActor(choix_acteur_random);
acteur.changeClass(choix_classe_aleatoire, false);

// WINDOW WITH PULL INFOS
var actorId = choix_acteur_random; // ID OF RECRUITED ACTOR
var actor = $gameActors.actor(actorId);

// ACTOR INFOS
var nomActeur = actor.name();
var classeActeur = actor.currentClass().name;
var choix_rarete_aleatoire = choix_rarete_aleatoire === 10 ? "Légendaire" : (choix_rarete_aleatoire >= 7 ? "Rare" : "Commun");

// ACTOR PULLED PORTRAIT
var portraitFilename = actor.faceName();
var portraitIndex = actor.faceIndex();


// DRAW INFO WINDOW
var message = nomActeur + " rejoint l'équipe ! Classe : " + classeActeur + " Rareté " + choix_rarete_aleatoire;
$gameMessage.setFaceImage(portraitFilename, portraitIndex);
$gameMessage.add(message);

//LIST OF POSSIBLE RECRUITS (ACTORS) UPDATE
liste_acteurs = liste_acteurs.filter(function(actorId) {
    return actorId !== choix_acteur_random;
});

}
};

})();


The pull system works fine, but as you can see, it is supposed to show a message saying something "you've recruited all the mercenaries" when the liste_acteur array is empty (because it's supposed to update and remove the recruited actor each time you make a pull). but unfortunately, when i do the 17th pull, at the exact moment where the messgae is supposed to be shown, the game instantly freezes. there is no error in the console or nothing, the game just dies.

I reached the limits of my JS knowledge, so if a pro sees something wrong that might provoke the freeze, i'll be very useful and i thank you in advance !
If it's freezing, I'm assuming you've introduced an infinite loop somehow. Where are you calling GSRSRecruit?
i'm actually using a script command on a simple "action on touch" (sorry don't know the exact name in english) event :

$gameMap._interpreter.GSRSRecruit();

Because the plugin commande GSRSRecruit doesn't work for some reason
i'm actually using a script command on a simple "action on touch" (sorry don't know the exact name in english) event :

$gameMap._interpreter.GSRSRecruit();

Because the plugin commande GSRSRecruit doesn't work for some reason
The reason it doesn't work as a plugin command is that you haven't inserted into your plugin the syntax required to register it as one. You've added your function to the Game_Interpreter class directly, which can only be accessed via the interpreter attached to the map, the troop, or the child interpreter attached to an event that's running a parallel process page.

If it's on event touch, I suspect what's happening is that once you have your 17th member, it's running the command again because you're still touching the event. It then runs the do-while loop, which ends up going infinitely because the while condition is always going to return true due to your party containing every possible actor in the array.

The fact that you're setting the array of possible actors in the command that chooses one and adds it to your party is flawed to begin with anyway. You're recreating that full array every time the command runs. That means that checking for its length being 0 right after that will never result in the message showing, and every time you run this you've removed at most one actor from the array, with that actor being re-added the following time it executes.
The reason it doesn't work as a plugin command is that you haven't inserted into your plugin the syntax required to register it as one. You've added your function to the Game_Interpreter class directly, which can only be accessed via the interpreter attached to the map, the troop, or the child interpreter attached to an event that's running a parallel process page.
I see, thanks for the info. I’ll look into that when I’ll have my freeze issue solved 🙂
I've edited to address that. It's because of how your do-while loop is set up.
I've edited to address that. It's because of how your do-while loop is set up.
Ok, because the array is refilled again every time I run the script, the if liste_acteurs.length === 0 is never true, so the script runs fully and falls in a infinite loop on the do/while, causing the freeze, am I right ? How should I handle the array then ?
Ok, because the array is refilled again every time I run the script, the if liste_acteurs.length === 0 is never true, so the script runs fully and falls in a infinite loop on the do/while, causing the freeze, am I right ? How should I handle the array then ?
Basically yes, but not quite. The message not showing and the infinite loop are two related but separate problems.

The way I would personally do it would be to store the possible actor array in a game variable, and initialise it when the game starts *outside* of this function. I would also use the splice() function to modify the array in place rather than using variable references, but what you have will function solidly enough for your needs. I don't think we're at the optimisation point yet. ^_^;;
I've edited to address that. It's because of how your do-while loop is set up.
Ok, because the array is refilled again every time I run the script, the if liste_acteurs.length === 0 is never true, so the script runs fully and falls in a infinite loop on the do/while, causing the freeze, am I right ? How should I handle the array then ?
Basically yes, but not quite. The message not showing and the infinite loop are two related but separate problems.

The way I would personally do it would be to store the possible actor array in a game variable, and initialise it when the game starts *outside* of this function. I would also use the splice() function to modify the array in place rather than using variable references, but what you have will function solidly enough for your needs. I don't think we're at the optimisation point yet. ^_^;;
lmao you said it. If I go into details, you advise to use :
$gameVariables.setValue(ID, [1, 2, 3…]);
At the start of the game to set the array of possible actors.
And to delete the selected actor at each pull, use
$gameVariables.value(ID).splice(SELECTED_ACTOR_ID, 1);
Ok, because the array is refilled again every time I run the script, the if liste_acteurs.length === 0 is never true, so the script runs fully and falls in a infinite loop on the do/while, causing the freeze, am I right ? How should I handle the array then ?

lmao you said it. If I go into details, you advise to use :
$gameVariables.setValue(ID, [1, 2, 3…]);
At the start of the game to set the array of possible actors.
And to delete the selected actor at each pull, use
$gameVariables.value(ID).splice(SELECTED_ACTOR_ID, 1);
Almost, but the first argument to splice is the start index, so you'd have to use indexOf to get where in the array the selected actor ID is.
I see, thank you for your answers, it's very helpful
Hello, i'm back with my problem. I updated and optimized (i think) the code, everything works fine except the freeze bug that is still there, even with your solution. The weird thing is that when i try to manually make the possible actors and/or classes recruitables empty (via : $gameVariables.setValue(1, []); ), it works, the game doesn't freeze and i got the message saying "no more recruits..". So i guess the problem is in the way the plugin automatically updates var_id_actors & var_id_classes when you make a pull, but...i'm stuck, i don't see what's causing the error... Here is the updated code :

JavaScript:
// =====================================
// GACHA-STYLE RECRUITMENT SYSTEM
// BY MIDNIGHT89 - 2023
// =====================================

/*:


 * @plugindesc Gacha-Style Recruitment System
 * @author MIDNIGHT89
 * @version 1.0
 *
 * @param --- CONFIG ---
 * @default
 *
 * @param Actors Variable ID
 * @parent --- CONFIG ---
 * @type variable
 * @desc Variable ID for the potential actors you chose on the setup phase. See the help text on the left.
 * @default 1
 *
 * @param Classes Variable ID
 * @parent --- CONFIG ---
 * @type variable
 * @desc Variable ID for the potential classes you chose on the setup phase. See the help text on the left.
 * @default 2
 *
 * @param --- TIER SYSTEM ---
 * @default
 *
 * @param Activate Tier System
 * @parent --- TIER SYSTEM ---
 * @type number
 * @desc Enable or disable the tier system.
 * 1 = ON / 0 = OFF
 * @min 0
 * @max 1
 * @default 1
 *
 * @param Newbie Tier Name
 * @parent --- TIER SYSTEM ---
 * @type string
 * @desc Custom name for the newbie tier.
 * @default Newbie
 *
 * @param Apprentice Tier Name
 * @parent --- TIER SYSTEM ---
 * @type string
 * @desc Custom name for the apprentice tier.
 * @default Apprentice
 *
 * @param Expert Tier Name
 * @parent --- TIER SYSTEM ---
 * @type string
 * @desc Custom name for the expert tier.
 * @default Expert
 *
 * @param Master Tier Name
 * @parent --- TIER SYSTEM ---
 * @type string
 * @desc Custom name for the master tier.
 * @default Master
 *
 * @param Newbie Passive State
 * @parent --- TIER SYSTEM ---
 * @type state
 * @desc State applied if recruit is a newbie.
 *
 * @param Apprentice Passive State
 * @parent --- TIER SYSTEM ---
 * @type state
 * @desc State applied if recruit is an apprentice.
 *
 * @param Expert Passive State
 * @parent --- TIER SYSTEM ---
 * @type state
 * @desc State applied if recruit is an expert.
 *
 * @param Master Passive State
 * @parent --- TIER SYSTEM ---
 * @type state
 * @desc State applied if recruit is a master.
 *
 * @param Apprentice RNG Trigger Value
 * @parent --- TIER SYSTEM ---
 * @type number
 * @desc If the Tier Choice RNG value is above this one, the recruit will be an apprentice.
 * @default 76
 *
 * @param Expert RNG Trigger Value
 * @parent --- TIER SYSTEM ---
 * @type number
 * @desc If the Tier Choice RNG value is above this one, the recruit will be an expert.
 * @default 90
 *
 * @param Master RNG Trigger Value
 * @parent --- TIER SYSTEM ---
 * @type number
 * @desc If the Tier Choice RNG value is above this one, the recruit will be an master.
 * @default 97
 *
 * @param --- MISC ---
 * @default
 *
 * @param Unique Classes Mode
 * @parent --- MISC ---
 * @type number
 * @desc If activated, a class can't be pulled more than once.
 * 1 = ON / 0 = OFF
 * @min 0
 * @max 1
 * @default 0
 *
 * @param No More Recruits Message
 * @parent --- MISC ---
 * @type text
 * @desc Custom message if recruiting is impossible.
 * See help on the left.
 * @default There is nobody left to recruit !
 *
 * @help
 *
 * ==========================================================================
 * WHAT THIS PLUGIN DOES ?
 * ==========================================================================
 *
 * This plugin allows you to implement a simple gacha-style recruitment system.
 * When you execute the plugin command, it randomly chooses an actor from a
 * list you configure in the setup phase, a class, an optional tier and it
 * generates a recruit with these parameters that immediately joins the player's
 * party.
 * There is several options that you can change. Find more details about these
 * in the HOW TO USE section below.
 *
 * Before using the plugin, you have to do some setup by following the steps
 * in the SETUP section below.
 *
 * ==========================================================================
 * SETUP
 * ==========================================================================
 *
 * Before using this plugin, you have to follow some simple steps :
 *
 * 1.Set up the initial list of recrutables actors by executing this code once
 * at the beginning of your game (via a script call) :
 *
 * $gameVariables.setValue(XX, [1, 2, 3]);
 *
 * Replace XX by the ID of the game's variable you want to use.
 * This variable will store the list of the actors that can be potentially
 * recruited.
 * Be sure to choose an unused variable ID, once it's set, it cannot be changed.
 * Replace 1, 2, 3...by the IDs of the actors you want to be recruitable.
 * Respect the syntax and formatting.
 *
 * Then, set up the list of recruitable classes by executing this code once
 * at the beginning of your game (via a script call) :
 *
 * $gameVariables.setValue(YY, [1, 2, 3]);
 *
 * Replace YY by the ID of the game variable you want to use.
 * This is the variable that will contains the list of the classes that can be
 * potentially attributed to recruited actors.
 * Be sure to choose an unused variable ID, different from the one you chose
 * for the actors. Once it's set, it cannot be changed.
 * Replace 1, 2, 3...by the IDs of the classes you want to be assigned to new
 * recruits.
 * Respect the syntax and formatting.
 *
 * In the plugin parameters window (on the right), select the variables you've
 * chosen for the actors list and the classes list in the "Actors Variable ID"
 * and "Classes Variable ID" parameters.
 *
 * The plugin is now ready to be used !
 *
 * ==========================================================================
 * HOW TO USE
 * ==========================================================================
 *
 * During the game, initiate a "pull" by using the plugin command :
 *
 * GSRSRecruit (!!!!!!!!!! NOT WORKING YET !!!!!!!!!!)
 *
 * Or by using the script call :
 *
 * $gameMap._interpreter.GSRSRecruit();
 *
 * When you do so, a recruit is generated : a random actor (from the list you've
 * configured) is chosen, a random class (from the configured list) and a random
 * tier is assigned to him/her.
 *
 * By default, you can have class duplicates, meaning that multiple recruits can
 * have the same class. You can make classes unique in the plugin parameters by
 * enabling the "Unique Classes Mode". If this mode is enabled, a class can be
 * pulled only if you don't already have a party member with this class.
 *
 * By default, there is 4 tiers possible for the new recruit :
 * Newbie, Apprentice, Expert, Master.
 * You can change the name of the tiers and change the proc probabilities of each
 * teir in the plugin parameters.
 * You can automatically apply states to recruits depending on their tier. It's
 * useful if you want to apply bonuses to recruits depending on their tier, and/or
 * display an icon on their profile showing their tier.
 * (You might need an external plugin if you want these states to be permanent).
 *
 * To decide which tier the recruit will inherit, the plugin uses RNG :
 * A random number between 1 and 100 is chosen and, depending of the result the
 * tier is assigned. By default, the values are :
 * 1-75 : Newbie / 76-90 : Apprentice / 91 - 96 : Expert / 97 - 100 : Master.
 * You can change these values, and even disable some tiers simply by entering
 * a trigger value superior to 100.
 * If you use custom trigger values, be sure to always put a value for a tier
 * superior to the previous tier trigger value, or it will  up everything :X
 * 30 / 87 / 95 : GOOD
 * 45 / 87 / 68 : NOT GOOD
 * You can also totally disable the tier system in the plugin parameters.
 *
 * After the pull, a message appears, informing the player of the results of
 * the pull. The list of recruitable actors (and classes if "Unique Classes
 * Mode" is enabled) is then automatically updated.
 * If you execute a new pull but there is no actors left to recruit (or any
 * classes available left if you activated the "Unique Classes Mode"), a message
 * saying so appears.
 * You can customize the message in the plugin parameters.
 *
 * ==========================================================================
 * USEFUL SCRIPTS
 * ==========================================================================
 *
 * If you want to manually add or re-add an actor to the list of potential
 * recruits, use the script call :
 *
 * var new_actor = XX;
 * if (!$gameVariables.value(var_id_actors).includes(new_actor)) {
 * }
 * else { $gameVariables.value(var_id_actors).push(new_actor);
 * }
 *
 * Replace XX by the ID of the actor you want to add to the potential recruit list.
 * If the actor is already in the list, nothing happens (to avoid duplicates).
 *
 * You can also remove an actor from the list :
 *
 * var remove_actor = XX;
 * if (!$gameVariables.value(var_id_actors).includes(remove_actor)) {
 * $gameVariables.value(var_id_actors).splice($gameVariables.value(var_id_actors)
 * .indexOf(remove_actor), 1);
 * }
 * else {
 * }
 *
 * Replace XX by the ID of the actor you want to remove from the potential recruit list.
 * If the actor is not in the list, nothing happens.
 *
 * You can do the same with classes, to add a new class :
 *
 * var new_class = XX;
 * if (!$gameVariables.value(var_id_classes).includes(new_class)) {
 * }
 * else { $gameVariables.value(var_id_classes).push(new_class);
 * }
 *
 * And to remove a class :
 *
 * var remove_class = XX;
 * if (!$gameVariables.value(var_id_classes).includes(remove_class)) {
 * $gameVariables.value(var_id_classes).splice($gameVariables.value(var_id_classes)
 * .indexOf(remove_class), 1);
 * }
 * else {
 * }
 *
 * Please note that if you manually add a class that has already been pulled, it
 * will be pullable again, making the "Unique Class Mode" obsolete if it's enabled.
 *
 * Developpers can check the list of the pullable actors and classes at any time
 * via the game's console (F12) :
 *
 * console.log(var_id_actors);
 * console.log(var_id_classes);
 *
 * A log is also systematically printed in the console at each pull.
 *
 *
 * ==========================================================================
 * HAPPY GAME MAKING :)
 * ==========================================================================
 */
// (PLUGIN PARAMETERS)

var params = PluginManager.parameters('GSRSRecruit');
var no_more_recruits_message = String(params['No More Recruits Message']);
var var_id_actors = Number(params['Actors Variable ID']);
var var_id_classes = Number(params['Classes Variable ID']);
var tier_system = Number(params['Activate Tier System']);
var newbie_tier_name = String(params['Newbie Tier Name']);
var apprentice_tier_name = String(params['Apprentice Tier Name']);
var expert_tier_name = String(params['Expert Tier Name']);
var master_tier_name = String(params['Master Tier Name']);
var newbie_passive_state = Number(params['Newbie Passive State']);
var apprentice_passive_state = Number(params['Apprentice Passive State']);
var expert_passive_state = Number(params['Expert Passive State']);
var master_passive_state = Number(params['Master Passive State']);
var apprentice_trigger_value = Number(params['Apprentice RNG Trigger Value']);
var expert_trigger_value = Number(params['Expert RNG Trigger Value']);
var master_trigger_value = Number(params['Master RNG Trigger Value']);
var unique_classes_mode = Number(params['Unique Classes Mode']);

// -----------------------------------------------------------

(function() {


    // (PLUGIN COMMAND)
Game_Interpreter.prototype.GSRSRecruit = function() {


// (NO RECRUIT LEFT MESSAGE)
if ($gameVariables.value(var_id_actors).length === 0) {
$gameMessage.add(no_more_recruits_message);
}
else if ($gameVariables.value(var_id_classes).length === 0) {
    $gameMessage.add(no_more_recruits_message);
}
else {

// (RANDOM ACTOR PICK)
var random_actor_pick;
do {
    random_actor_pick = $gameVariables.value(var_id_actors)[Math.floor(Math.random() * $gameVariables.value(var_id_actors).length)];
} while ($gameParty.members().some(function(actor) {
    return actor.actorId() === random_actor_pick;
}));

// (RANDOM CLASS PICK)
var random_class_pick;
if (unique_classes_mode == 0) {
    random_class_pick = $gameVariables.value(var_id_classes)[Math.floor(Math.random() * $gameVariables.value(var_id_classes).length)];
}
else {
var available_classes = $gameVariables.value(var_id_classes);
do {
    random_class_pick = available_classes[Math.floor(Math.random() * available_classes.length)];
}
while ($gameParty.members().some(function(actor) {
    return actor.currentClass().id === random_class_pick;
}));
}

// (APPLY BONUS STATE DEPENDING ON TIER)
if (tier_system == 1) {
    var tier_name = newbie_tier_name;
    $gameActors.actor(random_actor_pick).addState(newbie_passive_state);
    var random_tier_pick = Math.floor(Math.random() * 100) + 1;
    if (random_tier_pick >= apprentice_trigger_value && random_tier_pick < expert_trigger_value) {
        tier_name = apprentice_tier_name;
        $gameActors.actor(random_actor_pick).removeState(newbie_passive_state);
        $gameActors.actor(random_actor_pick).addState(apprentice_passive_state);
    } else if (random_tier_pick >= expert_trigger_value && random_tier_pick < master_trigger_value) {
        tier_name = expert_tier_name;
        $gameActors.actor(random_actor_pick).removeState(newbie_passive_state);
        $gameActors.actor(random_actor_pick).addState(expert_passive_state);
    } else if (random_tier_pick >= master_trigger_value) {
        tier_name = master_tier_name;
        $gameActors.actor(random_actor_pick).removeState(newbie_passive_state);
        $gameActors.actor(random_actor_pick).addState(master_passive_state);
    }
}

// (ADD THE GENERATED RECRUIT TO THE GROUP)
var acteur = $gameActors.actor(random_actor_pick);
$gameParty.addActor(random_actor_pick);
acteur.changeClass(random_class_pick, false);

// (SHOW A MESSAGE WITH A SUMMARY OF PULLED RECRUIT INFOS)
var actor = $gameActors.actor(random_actor_pick);
var actor_name = actor.name();
var class_name = actor.currentClass().name;
var portraitFilename = actor.faceName();
var portraitIndex = actor.faceIndex();

if (tier_system == 1) {
    var message = actor_name + " joins the party ! Class : " + class_name + " Tier : " + tier_name + ".";
    $gameMessage.setFaceImage(portraitFilename, portraitIndex);
    $gameMessage.add(message);
}
else {
    var message = actor_name + " joins the party ! Class : " + class_name + ".";
    $gameMessage.setFaceImage(portraitFilename, portraitIndex);
    $gameMessage.add(message);
}
// (UPDATE THE POTENTIAL ACTORS RECRUIT LIST)

$gameVariables.value(var_id_actors).splice($gameVariables.value(var_id_actors).indexOf(random_actor_pick), 1);

// (UPDATE THE POTENTIAL CLASS LIST IF UNIQUE CLASS MODE IS ACTIVATED)
if (unique_classes_mode == 1) {
    $gameVariables.value(var_id_classes).splice($gameVariables.value(var_id_classes).indexOf(random_class_pick), 1);
}

// (LOGS)
console.log('---------------------');
console.log('Chosen Actor ID : ' + random_actor_pick);
console.log('Chosen Class ID : ' + random_class_pick);
console.log('Tier RNG : ' + random_tier_pick);
console.log('Chosen Tier : ' + tier_name);
console.log('Updated Potential Actors List : ' + $gameVariables.value(var_id_actors));
console.log('Updated Potential Classes List : ' + $gameVariables.value(var_id_classes));
console.log('Unique Class Mode Enabled ? ' + unique_classes_mode);
console.log('Tier System Enabled ? ' + tier_system);
console.log('Custom Tier Names : ' + newbie_tier_name + ' ' + apprentice_tier_name + ' ' + expert_tier_name + ' ' + master_tier_name);
console.log('Custom Tier States : ' + newbie_passive_state + ' ' + apprentice_passive_state + ' ' + expert_passive_state + ' ' + master_passive_state);
console.log('---------------------');

}
};

})();

Original thread: https://forums.rpgmakerweb.com/threads/home-made-plugin-bug-cant-find-why.161934/