MZ Hud Maker Ultra Pro: Enemy Stats In Battle

Apologies for posting another thread for the same project, but I felt this warranted a separate thread as it regards something different from my previous request.

ka8xHU1.png


So in this game I'm making I want the player to be able to see the enemy's attack stat, will stat, and HP stat at all times. I'm using Hud Maker Ultra Pro. (At some point I'll also code it so the player can see their hero's attack, will and HP stats too, but this is much more straightforward so I'm not concerned about this.)
As you can see, the Goblin and the Imp appear to have the same stats - but in actuality they do not. The Imp has 1 Attack, 2 Will, and 6 HP. But I don't know what I should code in to get the Hud to identify which enemy it is on and display its attack, will, or HP.

I'll attach some extra pictures to show what I have done - and to make it more clear what needs doing or where I have gone wrong.

YKwz2f7.png

V1ZmuVQ.png

XYhIBgm.png

nKghzEW.png
You're going to have to have individual HUD elements for each member of the troop and reference them by their position, i.e. $gameTroop.members()[0].enemy().name
Okay - that in itself isn't a problem. I wondered if there was some way to do it but your solution would work too.

The problem is, I now have my UI doing this:
TnBzSdG.png


It's maybe not immediately obvious, but that's the HUD now giving Attack, Will and HP twice for each enemy - Goblin has both Goblin's and Imp's stats under it, and Imp also has both Goblin and Imp's stats under it. I've tried to tinker around a bit so it only displays once for each enemy - I guess I'm this is the part where I'm supposed to reference them by their position - but I'm not sure how I would go about that, and the options I've tried haven't been successful.

The only other solution I can think of would be to cut the component entirely and just do it manually - which would then mean my HUD wouldn't dynamically adjust if I have more or less enemies on the screen, so I really don't feel that's overall a good solution either.
Okay - that in itself isn't a problem. I wondered if there was some way to do it but your solution would work too.

The problem is, I now have my UI doing this:
TnBzSdG.png


It's maybe not immediately obvious, but that's the HUD now giving Attack, Will and HP twice for each enemy - Goblin has both Goblin's and Imp's stats under it, and Imp also has both Goblin and Imp's stats under it. I've tried to tinker around a bit so it only displays once for each enemy - I guess I'm this is the part where I'm supposed to reference them by their position - but I'm not sure how I would go about that, and the options I've tried haven't been successful.

The only other solution I can think of would be to cut the component entirely and just do it manually - which would then mean my HUD wouldn't dynamically adjust if I have more or less enemies on the screen, so I really don't feel that's overall a good solution either.
You're referencing the entire troop in your Enemy ID field. If you changed it please post what you have now.
Okay - I'll post what I think pertains to the issue here. Apologies if I'm missing the mark! I am very grateful for this help.

K25pb6z.png

As you can see in this, I have now 2 components which I've pasted on top of each other - exact same coordinates, just that the component "Enemy2Stats" refers to the stats in Troop Member 2's position, which works fine. (Except that it then sits over Troop Member 1's position as well, but that's why I'm here, haha!)
9flu2KX.png

Collection Properties for one of the components. I'll be honest, I barely understand what this part is supposed to do, I'm just going based off what makes any sort of sense - which probably isn't correct.
5X39n4W.png

I set the "Troop Member ID" parameter to a dynamic value, which is shown here. For the component for Enemy 2's stats, it's exactly the same except the value is set to $gameTroop.members()[1].enemy().name (as it's for the second troop member).
1UYbyow.png

uOStdW1.png


And these two bits are the actual components themselves and the setup of the custom parameter.

I think I'm in the right ballpark? But clearly I'm not since this isn't working as I'd anticipated.

If I haven't given you the information you need to help me further, I apologise - I hope what I've posted helps!
I set the "Troop Member ID" parameter to a dynamic value, which is shown here. For the component for Enemy 2's stats, it's exactly the same except the value is set to $gameTroop.members()[1].enemy().name (as it's for the second troop member).
It looks like you have the HUDMaker itself doing everything for you through your menu selections, because this code I gave you:
$gameTroop.members()[1].enemy().name
returns the name of the second enemy, or more specifically the enemy at position 1 of the gameTroop array. It does not show stats, unless you consider the name a stat. You can change where it says name to return different stats, which I thought you knew how to do, but maybe isn't needed anyway if the HUD Maker does it for you.

Could you show what you have under Attach Collection Properties: EnemyData, Enemy Properties, and Index?
Right. Apologies - I only have the faintest understanding of JS - I did note the .name and wondered if that would or would not do what I wanted, but I figured since I don't know JS particularly well it was best not to question your solution and try it out anyway.

WLG2vyZ.png

So this is a list of the compoments - EnemyData refers to the first enemy in the troop, while EnemyData2 refers to the second enemy in the troop.
ADrJ1FQ.png

I have the list data set to Indexes - I couldn't tell you the distinction between Indexes or IDs - I would presume one of those would do what I need to do?
QWM7LwY.png

And finally an example of the code for one of the pieces of the component. It's for EnemyData and it refers to the first enemy in the troop - you can see this is meant to bring up the first enemy troop's attack stat (which it does).

What I was originally hoping was that I could set "return $gameTroop.members()[0].atk" to be more dynamic - instead of referring to member 0, have it be able to identify which troop member it's on and bring forward that particular member's attack stat (or Will, or HP, depending on which part of the component we're working with). I have a feeling that's probably not doable though.

So this alternate solution I'm pursuing has multiple components - so far I have two, but I'd probably have up to 6 or 7 depending on the maximum number of enemies I expect the player to fight at any time. The problem is that each component overlays on each enemy - I only need it to do it once, so that it can show troop member 0's stats over Troop member 0, troop member 1's stats over troop member 1, and so on.
I have the list data set to Indexes - I couldn't tell you the distinction between Indexes or IDs - I would presume one of those would do what I need to do?
The index should be the enemy's position in the battle formation. The id should be the enemy id in the database. If you have a troop of two goblins they should have two different index values but the same id value. I say should because I don't have this program because I don't have MZ and can't confirm it, which is why I need you to show me the screens to help you. I have the MV version but it is not as advanced.

What I was originally hoping was that I could set "return $gameTroop.members()[0].atk" to be more dynamic - instead of referring to member 0, have it be able to identify which troop member it's on
If you want it to be dynamic and not always refer to enemy 0 you would replace 0 with a variable reference, (e.g $gameVariables.value(15), which would return the enemy in the position of the value of your control variable 15) but I don't know if that's a good idea because you want enemy 0's HUD element cluster to be around enemy 0, and subsequently enemy 1's HUD elements to be around enemy 1, etc.

I believe what you should be doing is under enemy 2 data, replace every instance of
$gameTroop.members()[0] with
$gameTroop.members()[1]
If each element is drawn on every troop member, maybe there is a way to reference the troop member calling the hud elements. Possibly the 'this' keyword.

The first thing I'd do to figure it out would be to set one of the values to:

console.log(this)

And see what it shows in the console during battle
Okay, I changed the 'attack' for enemy troop member 0 to console.log(this) - this is what I saw in the console:

  1. _data: {Code: "/*return $gameTroop.members()[0].atk↵*/↵return console.log(this)↵", Display: "ATK", Type: 7}
  2. _func: ƒ ()
  3. _parent: UltraDynamicText {onChange: UltraSignal, currentText: "", _data: {…}, _config: HUDMakerUltraProcessConfiguration, _type: 7, …}
  4. _valueType: 0
  5. __proto__: UltraDynamicValue_BaseComponent
Hopefully there's something discernable from that!

If you want it to be dynamic and not always refer to enemy 0 you would replace 0 with a variable reference, (e.g $gameVariables.value(15), which would return the enemy in the position of the value of your control variable 15) but I don't know if that's a good idea because you want enemy 0's HUD element cluster to be around enemy 0, and subsequently enemy 1's HUD elements to be around enemy 1, etc.

I believe what you should be doing is under enemy 2 data, replace every instance of
$gameTroop.members()[0] with
$gameTroop.members()[1]
Unfortunately I don't feel this solution would work because wouldn't the variable reference then be stuck to one number, whichever I set it to? And this wouldn't fix the issue of there being multiple components per enemy - which would lead me to the solution I don't want to have to use, the manual one, seeing as it wouldn't update dynamically based on how many enemies are present in the battle.

I also apologise - I didn't directly mention, but I felt it had been implied I had changed enemy 2 data to reflect members()[0] to members()[1]. So I'm not sure how to progress from here.
Is there any documentation for this beast of a plugin?

I didn't see anything the last time I took a quick look, when you purchase it do you get a help file?
The plugin has some documentation in as much as SumRanDev (used to be SumRndmDude, I think) posted a few YouTube video tutorials, though he tends to focus on the simple things you can do with it and very little on the actual complex stuff (like what I'm trying to do here).
The actual 'tutorial' bit doesn't even really look at the Pro features (aka components) and how to attach them dynamically to each enemy in a troop.
It's not great documentation, honestly - kind of flying blind here.
MZ_SalemHUD.png


@Salem Sage I'm not familiar with HUDMaker, but if you're open to doing this with a standalone plugin, this could get you started:

Code:
//=============================================================================
// MZ_EnemyAttributeIcons.js
//=============================================================================
/*:
 * @target MZ
 * @plugindesc Display attribute icons and numbers on enemy sprites.
 * 
 */
var MZ_EnemyAttributeIcons = MZ_EnemyAttributeIcons || {};
(() => {
'use strict';
const $ = MZ_EnemyAttributeIcons;
// config
//-----------------------------------------------------------------------------
// the icon indexes
$.icons = [97, 89, 84]
// adjustable space between the icons
$.margin = 15;
// the width and height of the attribute text
$.attwidth = 62;
$.attheight = 36;
//-----------------------------------------------------------------------------
// sprite_enemy
//-----------------------------------------------------------------------------
// hook setBattler() to create the additional attribute sprites
//-----------------------------------------------------------------------------
$.setBattler = Sprite_Enemy.prototype.setBattler;
Sprite_Enemy.prototype.setBattler = function(battler) {
    $.setBattler.call(this, battler);
    this.createAttributeSprites();
}
// hook setBattler() to create the additional attribute sprites
//-----------------------------------------------------------------------------
Sprite_Enemy.prototype.createAttributeSprites = function() {
    // an array to store the attribute sprites
    // this is unused, but could be useful in the future
    this._attributeSprites = [];
    const iconWidth = ImageManager.iconWidth;
    const iconHeight = ImageManager.iconHeight;
    // iterate through the icon indexes configured above
    $.icons.forEach((iconIndex, i) => {
        const icon = new Sprite_EnemyAttributeIcon(iconIndex);
        const label = new Sprite_EnemyAttribute();
        icon.anchor.x = 0.5;
        icon.anchor.y = 1;
        label.anchor.x = 0.5;
        label.anchor.y = 0.33;
        // specify the position of both the icons and labels as per the mockup
        // additionally, specify the attribute type
        switch (i) {
            // the first icon
            case 0:
                icon.x = 0 - iconWidth - $.margin
                icon.y = 0 - iconHeight / 2;
                label.x = 0 - iconWidth - $.margin;
                label.y = 0 - iconHeight / 2;
                label._statusType = 'atk'; // the 1st icon's statusType
                break;
            // the second icon
            case 1:
                label._statusType = 'def'; // the 2nd icon's statusType
                break;
            // the third icon
            case 2:
                icon.x = 0 + iconWidth + $.margin;
                icon.y = 0 - iconHeight / 2;
                label.x = 0 + iconWidth + $.margin;
                label.y = 0 - iconHeight / 2;
                label._statusType = 'hp'; // the 3rd icon's statusType
                break;
        }
        // assign a battler to the label
        label._battler = this._enemy;
        // add the sprites to the container array created above
        this._attributeSprites.push(icon);
        this._attributeSprites.push(label);
        // add the sprites to the enemy sprite
        this.addChild(icon);
        this.addChild(label);
    });
}
//-----------------------------------------------------------------------------
// sprite_enemyattribute
//-----------------------------------------------------------------------------
// a new sprite to display a specified enemy attirbute, e.g. attack or HP
function Sprite_EnemyAttribute() {
    this.initialize(...arguments);
}
Sprite_EnemyAttribute.prototype = Object.create(Sprite.prototype);
Sprite_EnemyAttribute.prototype.constructor = Sprite_EnemyAttribute;
Sprite_EnemyAttribute.prototype.initialize = function() {
    Sprite.prototype.initialize.call(this);
    this._battler = null;
    this._value = 0; // will be used to cache the battler's attribute value
    this.createBitmap();
}
Sprite_EnemyAttribute.prototype.createBitmap = function() {
    this.bitmap = new Bitmap($.attwidth, $.attheight);
}
// retrieve the battler's current attributes
Sprite_EnemyAttribute.prototype.currentValue = function() {
    if (this._battler) {
        switch (this._statusType) {
            case "atk":
                return this._battler.atk;
            case "def": // assume that 'will' is battler.def
                return this._battler.def;
            case "hp":
                return this._battler.hp;
        }
    }
}
// check if the cached attribute value doesn't match the actual value. if so, redraw the number.
Sprite_EnemyAttribute.prototype.update = function() {
    Sprite.prototype.update.apply(this);
    if (this.bitmap && this._value !== this.currentValue()) {
        this._value = this.currentValue(); // set the cached value to the actual value
        this.draw(); // draw/redraw the number label
    }
}
// draw the attribute number
Sprite_EnemyAttribute.prototype.draw = function() {
    if (this.bitmap && this._value) {
        this.bitmap.clear(); // erase the old number
        this.bitmap.outlineColor = ColorManager.outlineColor(); // the default outline color
        this.bitmap.outlineWidth = 3;
        this.bitmap.fontFace = $gameSystem.mainFontFace(); // the default font face
        this.bitmap.fontSize = $gameSystem.mainFontSize(); // the default font size
        this.bitmap.drawText(this._value, 0, 0, $.attwidth, $.attheight, "center");
    }
}
//-----------------------------------------------------------------------------
// sprite_attributeicon
//-----------------------------------------------------------------------------
// basic sprite used to display an icon specified by index.
// maybe customize further if needed
function Sprite_EnemyAttributeIcon() {
    this.initialize(...arguments);
}
Sprite_EnemyAttributeIcon.prototype = Object.create(Sprite.prototype);
Sprite_EnemyAttributeIcon.prototype.constructor = Sprite_EnemyAttributeIcon;
Sprite_EnemyAttributeIcon.prototype.initialize = function(iconIndex) {
    Sprite.prototype.initialize.call(this);
    this._iconIndex = iconIndex;
    this.createBitmap();
}
Sprite_EnemyAttributeIcon.prototype.createBitmap = function() {
    this.bitmap = ImageManager.loadSystem("IconSet");
    const pw = ImageManager.iconWidth;
    const ph = ImageManager.iconHeight;
    const sx = (this._iconIndex % 16) * pw;
    const sy = Math.floor(this._iconIndex / 16) * ph;
    this.setFrame(sx, sy, pw, ph);
}
// end plugin
//-----------------------------------------------------------------------------
})();

I'd just consider this a boilerplate. Depending on your needs, you can adjust it further. There are no plugin params since it's set up per your screenshots as opposed to being configurable, but I added comments and intentionally based it off of the default code in rmmz_sprites.js in case you want to refer to that for making tweaks.

Anyway, this isn't fully fleshed-out, but it should accomplish what you've shown in your screenshots.
Fantastic! This does just what I would like, thanks.
I do have a few extra requests if possible;
Firstly, if any of the stats (such as Will) equals 0, then there is no number displayed at all - I would prefer this to display '0'.
Secondly, I'm also intending to include non-hostile entities on the battlefield that heroes can interact with (called Objectives) - if an 'enemy' is given State 36 it is labelled as an Objective - would it be possible for 'enemies' with State 36 to have their Attack and Will hidden?

Again, this is absolutely what I needed - thank you! I'm so grateful!
@Salem Sage You're welcome! And sure. Give this a go and let me know if this is what you had in mind.

You can change the ID of the objective state at the top of the file, but I have it set to 36 for you.

1690510471937.png


JavaScript:
//=============================================================================
// MZ_EnemyAttributeIcons.js
//=============================================================================
/*:
 * @target MZ
 * @plugindesc Display attribute icons and numbers on enemy sprites.
 * Terms: free to use. No credit required.
 */
var MZ_EnemyAttributeIcons = MZ_EnemyAttributeIcons || {};
(() => {
'use strict';
const $ = MZ_EnemyAttributeIcons;
// config
//-----------------------------------------------------------------------------
// the icon indexes
$.icons = [97, 89, 84]
// adjustable space between the icons
$.margin = 15;
// the width and height of the attribute text
$.attwidth = 62;
$.attheight = 36;
// the id of the objective state
$.objectiveState = 36;
//-----------------------------------------------------------------------------
// sprite_enemy
//-----------------------------------------------------------------------------
// hook setBattler() to create the additional attribute sprites
//-----------------------------------------------------------------------------
$.setBattler = Sprite_Enemy.prototype.setBattler;
Sprite_Enemy.prototype.setBattler = function(battler) {
    $.setBattler.call(this, battler);
    this.createAttributeSprites();
}
// create the attribute sprites
//-----------------------------------------------------------------------------
Sprite_Enemy.prototype.createAttributeSprites = function() {
    this._attributeSprites = [];
    const iconWidth = ImageManager.iconWidth;
    const iconHeight = ImageManager.iconHeight;
    $.icons.forEach((iconIndex, i) => {
        const icon = new Sprite_EnemyAttributeIcon(iconIndex);
        const label = new Sprite_EnemyAttribute();
        icon.anchor.x = 0.5;
        icon.anchor.y = 1;
        label.anchor.x = 0.5;
        label.anchor.y = 0.33;
        switch (i) {
            case 0:
                icon.x = 0 - iconWidth - $.margin
                icon.y = 0 - iconHeight / 2;
                label.x = 0 - iconWidth - $.margin;
                label.y = 0 - iconHeight / 2;
                icon._statusType = 'atk';
                label._statusType = 'atk';
                break;
            case 1:
                icon._statusType = 'def';
                label._statusType = 'def';
                break;
            case 2:
                icon.x = 0 + iconWidth + $.margin;
                icon.y = 0 - iconHeight / 2;
                label.x = 0 + iconWidth + $.margin;
                label.y = 0 - iconHeight / 2;
                icon._statusType = 'hp';
                label._statusType = 'hp';
                break;
        }
        label._battler = this._enemy;
        icon._battler = this._enemy;
        this._attributeSprites.push(icon);
        this._attributeSprites.push(label);
        this.addChild(icon);
        this.addChild(label);
    });
}
//-----------------------------------------------------------------------------
// sprite_enemyattribute
//-----------------------------------------------------------------------------
function Sprite_EnemyAttribute() {
    this.initialize(...arguments);
}
Sprite_EnemyAttribute.prototype = Object.create(Sprite.prototype);
Sprite_EnemyAttribute.prototype.constructor = Sprite_EnemyAttribute;
Sprite_EnemyAttribute.prototype.initialize = function() {
    Sprite.prototype.initialize.call(this);
    this._battler = null;
    this._value = 0;
    this.createBitmap();
    this.drawLabel();
}
Sprite_EnemyAttribute.prototype.createBitmap = function() {
    this.bitmap = new Bitmap($.attwidth, $.attheight);
}
Sprite_EnemyAttribute.prototype.currentValue = function() {
    if (this._battler) {
        switch (this._statusType) {
            case "atk":
                return this._battler.atk;
            case "def": // Assuming that 'Will' is Defense?
                return this._battler.def;
            case "hp":
                return this._battler.hp;
        }
    }
}
Sprite_EnemyAttribute.prototype.update = function() {
    Sprite.prototype.update.apply(this);
    this.updateValue();
    this.updateAlpha();
}
Sprite_EnemyAttribute.prototype.updateValue = function() {
    if (this.bitmap && this._value !== this.currentValue()) {
        this._value = this.currentValue();
        this.drawLabel();
    }
}
Sprite_EnemyAttribute.prototype.updateAlpha = function() {
    if (this._statusType === 'hp') return;
    if (this._battler.isStateAffected($.objectiveState) && this.alpha) {
        this.alpha = 0;
    } else if (!this._battler.isStateAffected($.objectiveState) && !this.alpha) {
        this.alpha = 1;
    }
}
Sprite_EnemyAttribute.prototype.drawLabel = function() {
    this.bitmap.clear();
    if (this.bitmap) {
        const value = this._value || 0;
        this.bitmap.outlineColor = ColorManager.outlineColor(); // the default outline color
        this.bitmap.outlineWidth = 3;
        this.bitmap.fontFace = $gameSystem.mainFontFace(); // the default font face
        this.bitmap.fontSize = $gameSystem.mainFontSize(); // the default font size
        this.bitmap.drawText(value, 0, 0, $.attwidth, $.attheight, "center");
    }
}
//-----------------------------------------------------------------------------
// sprite_attributeicon
//-----------------------------------------------------------------------------
function Sprite_EnemyAttributeIcon() {
    this.initialize(...arguments);
}
Sprite_EnemyAttributeIcon.prototype = Object.create(Sprite.prototype);
Sprite_EnemyAttributeIcon.prototype.constructor = Sprite_EnemyAttributeIcon;
Sprite_EnemyAttributeIcon.prototype.initialize = function(iconIndex) {
    Sprite.prototype.initialize.call(this);
    this._iconIndex = iconIndex;
    this.createBitmap();
}
Sprite_EnemyAttributeIcon.prototype.update = function() {
    Sprite.prototype.update.call(this);
    this.updateAlpha();
}
Sprite_EnemyAttributeIcon.prototype.updateAlpha = function() {
    if (this._statusType === 'hp') return;
    if (this._battler.isStateAffected($.objectiveState) && this.alpha) {
        this.alpha = 0;
    } else if (!this._battler.isStateAffected($.objectiveState) && !this.alpha) {
        this.alpha = 1;
    }
}
Sprite_EnemyAttributeIcon.prototype.createBitmap = function() {
    this.bitmap = ImageManager.loadSystem("IconSet");
    const pw = ImageManager.iconWidth;
    const ph = ImageManager.iconHeight;
    const sx = (this._iconIndex % 16) * pw;
    const sy = Math.floor(this._iconIndex / 16) * ph;
    this.setFrame(sx, sy, pw, ph);
}
// end plugin
//-----------------------------------------------------------------------------
})();
Fantastic! Have been testing it out and it's just brilliant. Thank you so much!

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/hud-maker-ultra-pro-enemy-stats-in-battle.159718/