MZ Passing arguments to a function in my Libra/Scan plugin

I wrote a Libra/Scan plugin (my first foray into JavaScript). I have Skill which calls a Common Event which runs the Script Event Command:

Code:
// Get the target enemy
var targetIndex = BattleManager._action._targetIndex;
var targetEnemy = $gameTroop.members()[targetIndex];
// Call Libra
JSH_Libra.displayEnemyStatsAndWeaknesses(targetEnemy);

I'm pretty happy with the outcome. (Sidenote: the padEnds don't seem to account for icon widths, but I just played with the numbers until they aligned.)

[Screenshot]
1727762237601.png

[/Screenshot]

My question is, should I bother passing targetEnemy as an argument, or just put it directly into the plugin code? I'm used to considering coupling and cohesion. If I move code to determine the target into the plugin it becomes self-contained, but I'm used to passing arguments so I can get reuse out of code in other contexts. Your thoughts?

Spoiler
/*:
* @target MZ
* @plugindesc Scans enemy and returns their stats and elemental weaknesses
* @Author John Hunter
*
* @Help
* This plugin scans an enemy and displays formatted results.
*/

var JSH_Libra = JSH_Libra || {};

// Function to display the target enemy's name, stats, and weaknesses
JSH_Libra.displayEnemyStatsAndWeaknesses = function(targetEnemy) {
if (targetEnemy) {
// Get enemy stats
const targetName = targetEnemy.name();
const targetHP = targetEnemy.hp;
const targetMHP = targetEnemy.mhp;
const targetMP = targetEnemy.mp;
const targetMMP = targetEnemy.mmp;
const targetTP = targetEnemy.tp;
const targetMTP = 255;
const targetSTR = targetEnemy.str;
const targetDEF = targetEnemy.def;
const targetMAT = targetEnemy.mat;
const targetMDF = targetEnemy.mdf;
const targetAGI = targetEnemy.agi;
const targetLUK = targetEnemy.luk;

// Format enemy stats
const formatTargetName = `\\C[14]${targetName}\\C[0]`;
const formatHPX = `\\C[4]HP:\\C[0] ${targetHP}/${targetMHP}`.padEnd(27);
const formatMPX = `\\C[4]MP:\\C[0] ${targetMP}/${targetMMP}`.padEnd(27);
const formatTPX = `\\C[4]TP:\\C[0] ${targetTP}/${targetMTP}`.padEnd(27);
const formatSTR = `\\C[4]STR:\\C[0] ${targetSTR}`.padEnd(23);
const formatDEF = `\\C[4]DEF:\\C[0] ${targetDEF}`.padEnd(23);
const formatMAT = `\\C[4]MAT:\\C[0] ${targetMAT}`.padEnd(23);
const formatMDF = `\\C[4]MDF:\\C[0] ${targetMDF}`.padEnd(23);
const formatAGI = `\\C[4]AGI:\\C[0] ${targetAGI}`.padEnd(23);
const formatLUK = `\\C[4]LUK:\\C[0] ${targetLUK}`.padEnd(23);

// Get enemy weaknesses
// Custom mapping of element IDs to their icons
const elementDetails = {
1: { name: "Physical", icon: "\\I[76]" },
2: { name: "Fire", icon: "\\I[64]" },
3: { name: "Ice", icon: "\\I[65]" },
4: { name: "Thunder", icon: "\\I[66]" },
5: { name: "Water", icon: "\\I[67]" },
6: { name: "Earth", icon: "\\I[68]" },
7: { name: "Wind", icon: "\\I[69]" },
8: { name: "Light", icon: "\\I[70]" },
9: { name: "Darkness", icon: "\\I[71]" }
};

// Get enemy weaknesses
const elementRates = {};
const noteTag = targetEnemy.enemy().note;
for (const [id, details] of Object.entries(elementDetails)) {
let rate = (targetEnemy.elementRate(Number(id)) * 100) + "%";
if (rate === "0%") rate = "Immune";
if (noteTag.includes(`<Element Absorb: ${details.name}>`)) rate = "Absorb";
if (noteTag.includes(`<Element Reflect: ${details.name}>`)) rate = "Reflect";

//Format enemy weaknesses
elementRates[id] = `${details.icon}: ${rate}`.padEnd(17);
}

// Display enemy stats and weaknesses
$gameMessage.add(formatTargetName);
$gameMessage.add("\\>" + formatHPX + formatSTR + formatMAT + elementRates[2] + elementRates[5] + elementRates[8]);
$gameMessage.add("\\>" + formatMPX + formatDEF + formatMDF + elementRates[3] + elementRates[6] + elementRates[9]);
$gameMessage.add("\\>" + formatTPX + formatAGI + formatLUK + elementRates[4] + elementRates[7] + elementRates[1]);
}
}
I don't think there's a right and a wrong way for this example here. I would probably do both:

Code:
function libra(enemy) { //enemy could be undefined
  if (enemy) {
    showStatsOf(enemy);
  } else {
    const targetIndex = BattleManager._action._targetIndex;
    const targetEnemy = $gameTroop.members()[targetIndex];
    showStatsOf(targetEnemy);
  }
}

function showStatsOf(enemy) { // now, enemy cannot be undefined
  ..
}

Now, you can call libra() and libra(enemy) and both will work.

But again, it's probably a matter of taste.
I agree, I don't think either way is "better" than the other. Especially since you've already written your plugin and the skill, there's really zero reason to change it.

If you hadn't already done it, I would probably have said just put it in the plugin code, especially since it's already, like, 70% variable declarations 😉😛

Sidenote: the padEnds don't seem to account for icon widths
Correct, padEnd() is a string method. Icons are images, not any part of the string. The string contains the escape code you're typing to represent the icon, so padEnd() will go off of that - not the icon itself.

The escape code gets converted to an icon as the string contents of the message are parsed and drawn onto the screen.

Original thread: https://forums.rpgmakerweb.com/threads/passing-arguments-to-a-function-in-my-libra-scan-plugin.172277/