"Wait" script call not working how I want it to

Hello, all! I'm working on a skill that deals damage to random targets based on a state. The idea is you use a Lightning spell, and it will apply the Shock state to your target. Continuously using Lightning based skills reapplies the Shock state and increases its stacks, up to a max of 7. When a Lightning based skill hits a target with the max stacks of Shock, it causes a Chain Lightning effect that deals Lightning damage to random targets in the enemy troop, bouncing between targets 7 times. Repeat hits are possible, and targets that have Shock applied take extra damage, but do not gain more stacks of Shock nor set off further Chain Lightning reactions.

As it stands, I have the skill and state working functionally, but I am having a slight issue with how the animation actually plays out. I want a slight pause between lightning strikes to give it a "rhythm." Like a straight "1-2-3-4-5-6-7," if that makes any sense? But currently, whenever I test it, all of the hits come at once after the initial attack lands. I'm using the script call for Wait, but it doesn't seem to be doing anything. I've checked in the console, but there isn't anything undefined popping up, so my use of the call is correct, but its not functioning how I want it to. Or I may be making the call with the wrong interpreter?

Here's the relevant note tag and code:
Code:
<Post-Damage Eval>
//Get target's Shock stack count
var shockStack = target.getStateCounter(29);

//Variable used in damage calculation
var spellPower = 10;

if (shockStack >= 7) {
    //Get alive members of enemy troop
    let troop = $gameTroop.aliveMembers();

    //Loop until 7 random hits have occurred
    for (let i = 0; i < shockStack; i++) {
        //Target a random, living enemy
        let enemy = troop[Math.randomInt(troop.length)];

        //Calculate base damage dealt by Chain Lightning
        var baseDmg = (user.mat * spellPower) - (target.mdf * (spellPower / 4));

        //Initialize damage multiplier and final damage variables
        var multiplier = 1;
        var finalDmg = 0;

        //Enemies afflicted with shock take extra damage
        if (enemy.isStateAffected(29)) {
            multiplier = enemy.getStateCounter(29) / 4;
        }

        //Final damage is base damage multiplied by the multiplier
        finalDmg = Math.ceil(baseDmg * multiplier);

        //Play animation on target, take damage, and damage popup
        enemy.startAnimation(76);
        enemy.gainHp(-(finalDmg));
        enemy.startDamagePopup();
        enemy.clearResult();

        //If target dies, perform collapse and update list of living troop members
        if (enemy.isDead()) {
            enemy.performCollapse();
            troop = $gameTroop.aliveMembers();
        }

        //Wait 30 frames to give a pause between strikes
        $gameTroop._interpreter.wait(30);
    }
    //Remove Shock from initial target
    target.removeState(29);
}
//If Shock is not at max stacks, reapply Shock
else { target.addState(29); }
</Post-Damage Eval>
This can't do what you want.

Interpreters process events, not code within a skill.

You should be programming the skill using Yanfly's Action Sequences, there are action sequence commands to put wait periods between commands.
I'm using the script call for Wait, but it doesn't seem to be doing anything.
the wait only works for objects in the main game loop and tells that loop to skip processing that object for x loops/frames of processing.

for objects outside the main game loop, it does nothing.
if an event with an action trigger or touch trigger or the like has a wait, then that acts as a true wait for that event object.
if an event with parallel or autorun trigger (automatically looping) has a wait, then it skips processing that event object for x frames (at sixty frames per second). This is done to reduce processing and lag in those parallel events that do not need to update their data sixty times per second.

animations are outside the game interpreter's control. similar to move routes and show text and call common event the interpreter only creates a different object in the game loop to handle that other task.That other object, the one for animation updates, is the one that would need to get the wait command instead of the game interpreter.

But unfortunately I'm not deep enough into the code to tell you how to identiy the correct object, so your bet bet is to follow the tip above and use the action sequence equivalent.
This can't do what you want.

Interpreters process events, not code within a skill.

You should be programming the skill using Yanfly's Action Sequences, there are action sequence commands to put wait periods between commands.
Thank you for replying! I actually came across your "Action Sequence Targets" extension while I was looking for a solution, as well as an extension that adds while loops to Yanfly's action sequences. I do have one question about your extension, though. If use the following action sequence:
Code:
animation 76: randomopponent
animation wait: 4
action effect: randomopponent
Would the random opponent be different each time the target type is used?
EDIT: As in, would the animation play on one random target, but the effect occur on a different one?
Would the random opponent be different each time the target type is used?
EDIT: As in, would the animation play on one random target, but the effect occur on a different one?
Yes. If you repeatedly want to refer to the same target, I would use Yanfly's Target Core to make the original list of targets be a random selection, then use targetnum from my extension.

(although it's also a good idea to be able to refer to the same, random target for multiple commands...I'll consider a way to add that into the plugin)
Yes. If you repeatedly want to refer to the same target, I would use Yanfly's Target Core to make the original list of targets be a random selection, then use targetnum from my extension.

(although it's also a good idea to be able to refer to the same, random target for multiple commands...I'll consider a way to add that into the plugin)
Alrighty! Thank you!

Yes. If you repeatedly want to refer to the same target, I would use Yanfly's Target Core to make the original list of targets be a random selection, then use targetnum from my extension.

(although it's also a good idea to be able to refer to the same, random target for multiple commands...I'll consider a way to add that into the plugin)
I'm getting an error "TypeError: Cannot read property 'startAnimation' of undefined" when I use the skill on an enemy that is not afflicted with Shock. And when I use it on an enemy that does have 7 stacks of Shock, it hit a random target first, hit the original target more than seven times, then hit that first target several more times as well.

This is the current notetag for the skill:
Code:
<custom target eval>
if (target.getStateCounter(29) >= 7) {
    for (var i = 0; i < 7; i++) {
        var randomIndex = Math.randomInt(foes.aliveMembers().length);
        var member = foes.aliveMembers()[randomIndex];
        targets.push(member);
    }
}
</custom target eval>

<target action>
perform start
motion chant: user
motion wait: user
motion spell: user
motion wait: user
action animation: targetnum 0
animation wait: 5
action effect: targetnum 0
if (target.getStateCounter(29) >= 7)
    action animation: targetnum 1
    animation wait: 5
    action effect: targetnum 1
    action animation: targetnum 2
    animation wait: 5
    action effect: targetnum 2
    action animation: targetnum 3
    animation wait: 5
    action effect: targetnum 3
    action animation: targetnum 4
    animation wait: 5
    action effect: targetnum 4
    action animation: targetnum 5
    animation wait: 5
    action effect: targetnum 5
    action animation: targetnum 6
    animation wait: 5
    action effect: targetnum 6
    action animation: targetnum 7
    animation wait: 5
    action effect: targetnum 7
else if (!target.isStateAffected(29))
    add state: targetnum 0
end
</target action>

I'm assuming the issue is with the <custom target eval> notetag and how I set up the list.
The list should be: targets[0] is the target selected by the player, and targets[1 - 7] are the random targets added to the array if targets[0] is afflicted with 7 stacks of Shock.
Have you tried putting a console.log(targets) at the end of your custom target notetag to get a printout of exactly what the contents of your targets array is before the skill starts?
Have you tried putting a console.log(targets) at the end of your custom target notetag to get a printout of exactly what the contents of your targets array is before the skill starts?
I did not! Let me do that real quick...
It looks like you didn't do anything to prevent the same target from getting selected more than once.

And you also didn't use the action sequence command to make targets immortal.

So one possible bug is killing an enemy with the damage then trying to target them again.
It looks like you didn't do anything to prevent the same target from getting selected more than once.

And you also didn't use the action sequence command to make targets immortal.

So one possible bug is killing an enemy with the damage then trying to target them again.
So I put in the console log. When the selected target is not affected by Shock, targets is empty and the error comes up when it tries to play the animation on targetnum 0 cause it doesn't exist.

When I put 7 stacks of Shock on a target through the console and then attack it with the Lightning skill, the array came back how I'd expect for the most part. This time there was some more variance in the enemies randomly selected, and the initial target only appeared in the list once. But when it reached the target, I was able to count that it got hit 8 total times. So it's like its hitting the initial target with the total number of repeated hits before continuing the skill. I think the way I might solve this is by defining targets[0] before the if statement.
Would I be able to just define it as
Code:
targets[0] = target;
?
So I put in the console log. When the selected target is not affected by Shock, targets is empty and the error comes up when it tries to play the animation on targetnum 0 cause it doesn't exist.
That's correct - the initial, selected target is not automatically added; targets starts as an empty array.

So just put
Code:
targets.push(target);
as the first line of your custom target eval, then proceed to add 6 more random targets if they have the state counters.
That's correct - the initial, selected target is not automatically added; targets starts as an empty array.

So just put
Code:
targets.push(target);
as the first line of your custom target eval, then proceed to add 6 more random targets if they have the state counters.
So it's fixed the issue regarding attacking a target that has 0 stacks of Shock, but when it triggers the chain lightning reaction, it still hits targetnum 0 8 times. I even tried changing the tag to "target" instead of "targetnum 0," and it still hit it 8 times.
That's weird. targetnum is a pretty straightforward bit of code, and I recalled it working in my testing.

You can open the plugin, go to line 101, and add
Code:
console.log("index is ", index, ", target is ", this._targets[index]);

see what it says for each use.
That's weird. targetnum is a pretty straightforward bit of code, and I recalled it working in my testing.

You can open the plugin, go to line 101, and add
Code:
console.log("index is ", index, ", target is ", this._targets[index]);

see what it says for each use.
2024-09-30_screenshot.png
Okay. Before I go rooting around with the assumption that I screwed up the regex (which is possible), add one more bit of info - make sure the targets array is still intact.

Code:
console.log("targets length is ", this._targets.length, " index is ", index, ", target is ", this._targets[index]);
Okay. Before I go rooting around with the assumption that I screwed up the regex (which is possible), add one more bit of info - make sure the targets array is still intact.

Code:
console.log("targets length is ", this._targets.length, " index is ", index, ", target is ", this._targets[index]);
Here it is! Sorry it took a while, I just got back from work.
2024-09-30_screenshot02.png
Okay, so the problem is still with your targeting code - it's not resulting in a list of targets.

What does it look like now?
Okay, so the problem is still with your targeting code - it's not resulting in a list of targets.

What does it look like now?
This is the custom target eval for the skill:
Code:
<custom target eval>
targets.push(target);
if (targets[0].getStateCounter(29) >= 7) {
    var i = 0
    while (i < 7) {
        var randomIndex = Math.randomInt(foes.aliveMembers().length);
        var member = foes.aliveMembers()[randomIndex];
        if (member != target) {
            targets.push(member);
            i += 1;
        }
    }
}
console.log(targets);
</custom target eval>
It looks like Yanfly's Battle Engine modifies some parts of the BattleManager I never realized.

Change your console.log in my targeting plugin to this:
Code:
console.log("targets length is ", this._allTargets.length, " index is ", index, ", target is ", this._targets[index]);

I'm trying to figure out what happens to that target array after the action starts.
It looks like Yanfly's Battle Engine modifies some parts of the BattleManager I never realized.

Change your console.log in my targeting plugin to this:
Code:
console.log("targets length is ", this._allTargets.length, " index is ", index, ", target is ", this._targets[index]);

I'm trying to figure out what happens to that target array after the action starts.
2024-10-01_screenshot01.png

Original thread: https://forums.rpgmakerweb.com/threads/wait-script-call-not-working-how-i-want-it-to.172255/