Referring to target in custom battle messages

How would one include the target’s name (or the caster’s name for that matter) in a custom battle message? Like “A casts %1 on B”. I tried a couple different javascript calls, but couldn’t figure out how to insert them properly in the markup. I’m pretty sure I could do a work around with action sequences, but I’m hoping there’s a shorthand.
The method:
String.prototype.format = function() {
return this.replace(/%([0-9]+)/g, (s, n) => arguments[Number(n) - 1]);
};

So you'd use something like:
const attacker = BattleManager._subject.name();
const skill = BattleManager._subject.currentAction().item().name
const defender = BattleManager._targets[0].name();
const text = "%1 casts %2 on %3".format(attacker, skill, defender);
have you come across this thread?
Ah yeah when the op said custom, I thought they meant they were just throwing their own messages there. But if you just want to have the display action show the target, then yeah there's the edit.
have you come across this thread?
I actually hadn't seen the earlier threads, must've been using search terms that were too generic.

ATT_turan's code here seems to do the job, and I was able to mod it so %3 returns the action-doer as well:
Code:
Window_BattleLog.prototype.displayAction = function(subject, item) {
    var numMethods = this._methods.length;
    if (DataManager.isSkill(item)) {
        if (item.message1) {
            this.push('addText', subject.name() + item.message1.format(item.name, BattleManager._targets[0].name(), BattleManager._subject.name()));
        }
        if (item.message2) {
            this.push('addText', item.message2.format(item.name, BattleManager._targets[0].name(), BattleManager._subject.name()));
        }
    } else {
        this.push('addText', TextManager.useItem.format(subject.name(), item.name));
    }
    if (this._methods.length === numMethods) {
        this.push('wait');
    }
};

So it can accommodate stuff like "Light begins to gather around %3" or "A spectral green thread appears between %3 and %2."

Double Edit: Found a conflict between ATT_turan's original code and Victor Engine - Unreachable Targets that crashes the game when an enemy doesn't have any valid targets. The way I'm currently planning to implement things, that shouldn't be possible in the final game, so not really concerned about it at the moment.
Spoiler: Error Message
Screenshot (364).png
Did some more testing, found that the crashing from the edit in my last post extended beyond that specific plugin/skill, so things like skills that have a scope of "none" because they're intended to play a common event or something also cause a crash.

I gave this goofy idea a shot (thanks Robro33 for the idea of checking a value as a boolean in the other thread) but it didn't seem to accept messing with parameters in this way, and %2 and %3 come out as undefined here:

Code:
Window_BattleLog.prototype.displayAction = function(subject, item) {
    var numMethods = this._methods.length;
    if (DataManager.isSkill(item)) {
        if (item.message1) {
            this.push('addText', subject.name() + item.message1.format(item.name, if (BattleManager._targets[0]){BattleManager._targets[0].name()}, BattleManager._subject.name()));
        }
        if (item.message2) {
            this.push('addText', item.message2.format(item.name, if (BattleManager._targets[0]){BattleManager._targets[0].name()}, BattleManager._subject.name()));
        }
    } else {
        this.push('addText', TextManager.useItem.format(subject.name(), item.name));
    }
    if (this._methods.length === numMethods) {
        this.push('wait');
    }
};
I gave this goofy idea a shot (thanks Robro33 for the idea of checking a value as a boolean in the other thread) but it didn't seem to accept messing with parameters in this way, and %2 and %3 come out as undefined here:
you shouldnt be able to place an IF there like that as an argument. you'd want to use a ternary operation for situations like that. IFs dont return anything while ternary operations explicitly have to


Did some more testing, found that the crashing from the edit in my last post extended beyond that specific plugin/skill, so things like skills that have a scope of "none" because they're intended to play a common event or something also cause a crash.
give this a shot then. it shouldnt crash if theres no target, but also if you use %2 on something without a target, it should use the user for the name

Code:
(() => {
Window_BattleLog.prototype.displayAction = function(subject, item) {
    var numMethods = this._methods.length;
    var target = BattleManager._targets[0] || subject;
    if (DataManager.isSkill(item)) {
        if (item.message1) {
            this.push('addText', subject.name() + item.message1.format(item.name, target.name(), subject.name()));
        }
        if (item.message2) {
            this.push('addText', item.message2.format(item.name, target.name(), subject.name()));
        }
    } else {
        this.push('addText', TextManager.useItem.format(subject.name(), item.name));
    }
    if (this._methods.length === numMethods) {
        this.push('wait');
    }
};
})();

Edit: fixed a "targets" typo
There was one typo of targets.name() instead of target.name(), but after fixing that it seems to be working properly.

Can I ask what the (() => { ... })(); is doing?
There was one typo of targets.name() instead of target.name(), but after fixing that it seems to be working properly.
yeah, i had caught it a few minutes ago myself and updated the last post, but you had already saw it

Can I ask what the (() => { ... })(); is doing?
i just pasted the function into an IIFE. Its not actually doing anything at all in this situation and can be removed if you want. I had pasted your code into a test plugin that already had it when I checked for syntax errors.
A couple more questions regarding battle log messages:

1. I'd previously been trying to use \. to delay messages that flash by too quickly, especially information about states that don't have an animation or sound effect attached to them. While it seems to cause a slight delay (I'm guessing just due to the interpreter having to read slightly more), after a bit of testing it definitely does not pause for the full 0.25 or 1 second it would in a regular message box, or at very least adding multiples does not lengthen the delay.

Messages from script calls also disappear almost instantly. I have solved this in common events using this.wait(), but it seems to not work in notetags. It might need a specific interpreter identified?

I'm not looking for a plugin that slows down every battle message, I just need to give a bit more time to the few that the engine clears too quickly.

2. The other issue that I haven't been able to find a solution for online is either changing or hiding the Action Failure message in specific situations. There's plenty of times I want the default message to display, like with status effect inducing skills, but I haven't found a way to disable them just for skills that run common events, since selecting the target as "none" prevents common events from running. There are also times when I want the failure message to match the skill better, like "X dodged out of the way", but haven't found a way to do that without just changing it at the system level.
Isn't the interpreter for reading event lists? Battle Log window has it's own mechanics. You can manipulate the ._waitCount but usually that stuff is done via pushing the wait method in there that manipulates that, with the this.push('wait');

Also using scope none doesn't stop common events from running?
Isn't the interpreter for reading event lists? Battle Log window has it's own mechanics. You can manipulate the ._waitCount but usually that stuff is done via pushing the wait method in there that manipulates that, with the this.push('wait');

Also using scope none doesn't stop common events from running?
So would the full line be something like Window_BattleLog._waitCount.push('wait()');? this.push('wait');on it's own doesn't seem to do anything.

I thought that was the case, and when I checked other common event skills they were working as intended with scope none, so I think I have run into a weird interaction between it and counter actions. It seems like if a counter attack has a scope of none it will be nullified? Changing the scope to "The User" has it working as intended, with the exception of the "There was no effect on The User!" message.
I think you can access it with either BattleManager._logWindow or SceneManager._scene._logWindow
So for example BattleManager._logWindow.push('wait');
I think you can access it with either BattleManager._logWindow or SceneManager._scene._logWindow
So for example BattleManager._logWindow.push('wait');
That seems to have done the trick, thanks!
I'm guessing that since state messages take markup, I need to look for a plugin that allows code to be evaluated in messages? This one from Yanfly seems to be paid only.
Still struggling a little bit with inserting waits in the battlelog, I found the Victor Engine had a free plugin to evaluate code in message windows, but my attempt with inserting \e['this.push(\'wait\')'] in a battle message ended up just printing "['this.push('wait')']" at the end. Looking at the page again, it might just be intended for code that returns strings, or only compatible with regular message windows or something.

I was able to get some progress with the state messages specifically by digging into the plugin route, like so:
Code:
Window_BattleLog.prototype.displayCurrentState = function(subject) {
    var stateText = subject.mostImportantStateText();
    if (stateText) {
        this.push('addText', "\\c[\\v[6]]" + subject.name() + "\\c[0]" + stateText);
        this.push('wait');
        this.push('wait'); // extra wait
        this.push('clear');
    }
};

But it's a bit lacking in finesse. There's still the matter of battle messages for skills that don't have an animation, which extends to every skill that has a scope of "None", which all tend to have unique and important functions.

For getting the "No effect on X" message to go away on common event skills that need a specified target to work properly, I tried a similar plugin approach but wasn't getting any results attempting to add extra conditions to Window_BattleLog.prototype.displayFailure. This might be because I was using the wrong call, the closest I could find to "the skill that is being evaluated right now" was $gameTemp.lastActionData(0).
HOWEVER I found an extremely dumb workaround.
Adding the remove state trait for a state that the user cannot possibly have removes the failure message with 0 consequences on the effect, so I'm okay with calling that portion solved for now.
Not 100% sure what you're trying to do, but if you want a longer wait without pushing wait 2 times, just make a plugin to add a method like (and push 'longWait'):
Window_BattleLog.prototype.longWait = function() {
this._waitCount = this.messageSpeed() * 2;
};

Also, that workaround is the standard way, add/remove an empty dummy state. If you want to do it with code, I think it's something like:

Game_Action.prototype.itemEffectCommonEvent = function(target, effect) {
//
this.makeSuccess(target);
};

EDIT: removed the comment out stuff from the arguments that the base code had
EDIT: actually the comment out was in MZ files, but since it's otherwise the same as MV, whatever

Original thread: https://forums.rpgmakerweb.com/threads/referring-to-target-in-custom-battle-messages.182200/