How can I store enemy ID for a talk system?

Okay, I'll break it down. 🙂

Code:
$gameTroop.members()[
  $gameParty.members().filter(function(member) {
    return member._lastBattleSkill._itemId == 10;
  } )[0].
  _lastTargetIndex]
  .enemyId()

So we're trying to get the enemy ID of a troop member. We know we can call enemyId, but first we need to know which member to get.

Game_Actor has a property called _lastBattleSkill, which contains details on the last skill they used in battle. This contains a property called _itemId, which is the skill ID of the skill they used.

.filter is a function you can call on an array which finds all elements that meet a given criteria. In this case, we're passing in a function, storing each party member being checked in a variable called "member". For each one, we'll return true if the _itemId of their _lastBattleSkill is equal to the ID of the Talk skill. If so, they're returned in the filtered array. If not, they're discarded.

Now obviously, once we've filtered it, we need the specific member who used the skill, so we take the [0]th element, which is the only one there will be. This gives us a Game_Actor, where we can once again access the _lastTargetIndex. Providing the result of that filter function to the index reference for $gameTroop.members() will give us the ID of the enemy the party member who used Talk targeted, and then enemyId() will tell us what kind of enemy that was.
Okay, I'll break it down. 🙂

Code:
$gameTroop.members()[
  $gameParty.members().filter(function(member) {
    return member._lastBattleSkill._itemId == 10;
  } )[0].
  _lastTargetIndex]
  .enemyId()

So we're trying to get the enemy ID of a troop member. We know we can call enemyId, but first we need to know which member to get.

Game_Actor has a property called _lastBattleSkill, which contains details on the last skill they used in battle. This contains a property called _itemId, which is the skill ID of the skill they used.

.filter is a function you can call on an array which finds all elements that meet a given criteria. In this case, we're passing in a function, storing each party member being checked in a variable called "member". For each one, we'll return true if the _itemId of their _lastBattleSkill is equal to the ID of the Talk skill. If so, they're returned in the filtered array. If not, they're discarded.

Now obviously, once we've filtered it, we need the specific member who used the skill, so we take the [0]th element, which is the only one there will be. This gives us a Game_Actor, where we can once again access the _lastTargetIndex. Providing the result of that filter function to the index reference for $gameTroop.members() will give us the ID of the enemy the party member who used Talk targeted, and then enemyId() will tell us what kind of enemy that was.

I think I got it, but I don't think I would ever be able to come up with that with what little knowledge I have right now. XD

Thanks for explaining. :3
Reading all this Code is interesting, all that flexibility.
Just saw a Workaround, so i thought i mention it also.

Create a Talk Skill.
You can use the damage formula to put the Enemy Database ID into a Variable and call a common Event with the same Skill.
Code:
v[1] = b.enemyId() ; 0
Left part is the Code, right part the damage or healing done to that Enemy. Thats the only downside to this solution, even if it deals 0 damage, the 0 appears to be shown and the Actor swings it Weapon.

This Thread helped finding this Solution:
- This Thread
- https://www.rpgmakercentral.com/topic/36290-damage-formulas-101-mv-edition/
- https://forums.rpgmakerweb.com/index.php?threads/rpg-maker-mv-script-call-list.46456/

But the Scripted way is more flexible and solves the need of damage formula for this and you got atleast 2 Scripters willing to help.
So i would use their Solution, just wanted to mention this approach.
Reading all this Code is interesting, all that flexibility.
Just saw a Workaround, so i thought i mention it also.

Create a Talk Skill.
You can use the damage formula to put the Enemy Database ID into a Variable and call a common Event with the same Skill.
Code:
v[1] = b.enemyId() ; 0
Left part is the Code, right part the damage or healing done to that Enemy. Thats the only downside to this solution, even if it deals 0 damage, the 0 appears to be shown and the Actor swings it Weapon.

This Thread helped finding this Solution:
- This Thread
- https://www.rpgmakercentral.com/topic/36290-damage-formulas-101-mv-edition/
- https://forums.rpgmakerweb.com/index.php?threads/rpg-maker-mv-script-call-list.46456/

But the Scripted way is more flexible and solves the need of damage formula for this and you got atleast 2 Scripters willing to help.
So i would use their Solution, just wanted to mention this approach.

I think there is a potential issue with that; won't it display a message saying the skill did no damage if you have a damage formula for it that returns 0?
Oh that is possible error, i did not look at the Battle Mesages while testplaying.
But yes this limits the professional look of it further down the road =).
But actualy i would skip this mentioned approach of mine.

Iam no Scripter but i allowed myself to shorten your Codeline, the Code seems still to work and is more readable, i believe it also still works like intended:
Code:
$gameVariables.setValue(1, $gameTroop.members()[$gameParty.members()[0]._lastTargetIndex].enemyId());
Edit:
To figure out which Actor is talking (In case its more than one),you could create 1 talk skill for each actor,
that would keep the overview.
Iam no Scripter but i allowed myself to shorten your Codeline, the Code seems still to work and is much more readable, i believe it also still works like intended:
Code:
$gameVariables.setValue(1, $gameTroop.members()[$gameParty.members()[0]._lastTargetIndex].enemyId());
First, this is a script call, not a script expression for Control Variable.

Second, it probably won't work - it's looking up the lead member, rather than a specific actor, so unless you've locked the character with this skill to the lead position...

That said, your approach is valid for when there's only one actor - this should work too:
JavaScript:
$gameTroop.members()[$gameActor.actor(10)._lastTargetIndex].enemyId()
Replacing 10 with the ID of the actor.

Personally I prefer Trihan's solution, since it's a little more versatile - it will work with multiple actors with that skill, as long as only one of them is in the party at any given time. But this perhaps isn't a situation where versatility is required.
It seems to work.
I gave Actor2 Therese the Talk Skill (while the other did not have it and therese was on party position2) in Battle Test with 4 Actors it gave no Error and the Textbox indicated the correct Enemy ID, starting with 1 not 0 in this case.


But yes if the longer Scriptcall also gives the Id number of the executioning Actor, that could be helpfull.

Edit: Yes you are right it somehow bugs around and takes the wrong id not the one therese attacked.
So now i understand why its needed.
Sorry for stealing your time.

Sadly we cant just put a -1 in the box for last actor or something like that.
So the Actor must be specified to get the correct enemy target id.
Edit2:
Thank you this was interesting.
It seems to work.
I gave Actor2 Therese the Talk Skill (while the other did not have it and therese was on party position2) in Battle Test with 4 Actors it gave no Error and the Textbox indicated the correct Enemy ID, starting with 1 not 0 in this case.

But yes if the longer Scriptcall also gives the Id number of the executioning Actor, that could be helpfull.

Did you target a different enemy with something with the party leader? And was it a different *type* of enemy?
It seems to work.
I gave Actor2 Therese the Talk Skill (while the other did not have it and therese was on party position2) in Battle Test with 4 Actors it gave no Error and the Textbox indicated the correct Enemy ID, starting with 1 not 0 in this case.
Did Harold also attack the same enemy?
Yeah it did not work properly^^ i edited the above allready.
The further i test the stranger the error gets... in playtest its giving me a 3 even when nr3 is never attacked.
EDIT3: It seems the Codeline gave me the Troop Index ID not the Enemy ID, it just happened that both were 3 =) LOL, its to late in the night for this kind of stuff.

I will test your other codeline now.

Edit:
Yourcodeline gives error message game actor not defined.

$gameVariables.setValue(1, $gameTroop.members()[$gameActor.actor(2)._lastTargetIndex].enemyId());

So my apologies for interrupting and thanks, because this was still interesting.
You can use BattleManager.actor()._lastTargetIndex for this.

Any idea why I might be getting this error message? error.png

Edit: For some strange reason I have to use BattleManager._action._targetIndex, that does work but not BattleManager.actor()._lastTargetIndex... weird.
Necro-post notice: Dagothe

You have also hijacked someone else's thread for a completely unrelated question. Please take a look at our forum rules (link above) so you are familiar with them.

Original thread: https://forums.rpgmakerweb.com/threads/how-can-i-store-enemy-id-for-a-talk-system.123374/