MZ How to check when I'm selecting an ally with a skill or item

We keep working on making our own combat UI and we are thrilled with the result. But we hit a wall. I’m using RPG Maker MZ with some Visustella plugins and HUD Maker Ultra for the HUD.

When I target an ally to apply an effect (either a skill or an item), I need a line of code to know who I’m targeting so I can show the corresponding UI element.

The goal would be this:

1764335664014.png


The yellow outline indicates who is performing the action, and the blue outline shows who it’s directed at before confirming.

So far, I have tried:

Code:
return window._selectedAllyIndex >= 0;
return _allyTargetMode === true;
return _allyTargetMode;

But it only showed the UI element on all allies (even if it was an individual target).
With the base code I think it should be like (returns the actor object):

$gameParty.battleMembers()[SceneManager._scene._actorWindow.index()]
With the base code I think it should be like (returns the actor object):

$gameParty.battleMembers()[SceneManager._scene._actorWindow.index()]
It pops this error:
TypeError
Cannot read property 'index' of undefined
Yeah but where / at what stage are you checking it? And that was for base code.

Does it work if you put a conditional to check that the _actorWindow exists
if (SceneManager._scene._actorWindow)
Yeah but where / at what stage are you checking it? And that was for base code.

Does it work if you put a conditional to check that the _actorWindow exists
if (SceneManager._scene._actorWindow)
I'm putting it here:
1764349430201.png

The error pops up just when i start a battle. If i put the if a before the [SceneManager._scene._actorWindow.index()] it shows the picture all the battle but the error doesn't pop up.


Sorry if i'm not being clear, i'm very new to coding. Thanks for the response.
If it comes from a plugin where you give fixed coordinates, shouldn't you make 4 different versions with different x-coordinate for the actors?

And it seems like you can put multiple conditions, so first:
$gameParty.inBattle()
second:
SceneManager._scene._actorWindow
third (change index for battlers, 0, 1, 2, 3)
SceneManager._scene._actorWindow.index() === 0;

EDIT: This is assuming the plugin follows the logic where it doesn't check the following conditions if the previous ones fail. I guess you can also combine 2nd and 3rd with SceneManager._scene?._actorWindow.index() === 0;

EDIT2: Oh yeah, you also probably need to add a condition of
SceneManager._scene._actorWindow.active
If it comes from a plugin where you give fixed coordinates, shouldn't you make 4 different versions with different x-coordinate for the actors?
I set it up inside the HUD Maker ultra, so the x-coordinate is already fixed.
And it seems like you can put multiple conditions, so first:
$gameParty.inBattle()
second:
SceneManager._scene._actorWindow
third (change index for battlers, 0, 1, 2, 3)
SceneManager._scene._actorWindow.index() === 0;

EDIT: This is assuming the plugin follows the logic where it doesn't check the following conditions if the previous ones fail. I guess you can also combine 2nd and 3rd with SceneManager._scene?._actorWindow.index() === 0;
If the first condition fails (with code) then the error pops.
EDIT2: Oh yeah, you also probably need to add a condition of
SceneManager._scene._actorWindow.active
I have tried all the ways but keeps appearing the same error.
I have tried all the ways but keeps appearing the same error.
The easiest way to do this is to use the null chaining operator. This will only look at the property if it exists.

Note that @rpgLord69 is correct - HUDMaker doesn't know how to do anything with an actor object, you need to be checking for which specific actor is being targeted and turn the appropriate HUD element on/off with that information.

So you make your graphic that's highlighting the first party member's portion of the HUD, and give it the eval
Code:
return SceneManager._scene._actorWindow?.active && SceneManager._scene._actorWindow?.index() == 0;

then make another one highlighting the second party member using a 1 at the end, etc.
The easiest way to do this is to use the null chaining operator. This will only look at the property if it exists.

Note that @rpgLord69 is correct - HUDMaker doesn't know how to do anything with an actor object, you need to be checking for which specific actor is being targeted and turn the appropriate HUD element on/off with that information.

So you make your graphic that's highlighting the first party member's portion of the HUD, and give it the eval
Code:
return SceneManager._scene?._actorWindow.active() && SceneManager._scene?._actorWindow.index() == 0;

then make another one highlighting the second party member using a 1 at the end, etc.

I tried but gives me this error:

TypeError
Cannot read property 'active' of undefined
I tried but gives me this error:

TypeError
Cannot read property 'active' of undefined
When you get JavaScript errors, you need to press F8, go to the Console tab, and post a screenshot of the full error trace.

Note that I edited the code I had above for a typo - try the revised version.
I tested it just for "fun", and yeah, seems like you can't just chain the conditions to check if the first one isn't true. But putting it in one condition and using the optional chaining it seemed to work just fine for me. PS. You are adding it in the 'battle hud' side right?
When you get JavaScript errors, you need to press F8, go to the Console tab, and post a screenshot of the full error trace.

Note that I edited the code I had above for a typo - try the revised version.

I put the code with the correction of the typo and now works perfectly. Thank you!

Original thread: https://forums.rpgmakerweb.com/threads/how-to-check-when-im-selecting-an-ally-with-a-skill-or-item.181209/