Perfect, thanks.
Here's another thing you might randomly know the answer to, that's been bugging me all morning:
Using yanfly's buffs/states core, I can play an animation on a battler when it becomes inflicted with a state, but if I do anything else in the same section, it all happens on the same frame, without waiting for the animation to finish playing.
I am calling an animation with: target.startAnimation(171, false, 0);
I can't seem to find a similar call for waiting for the animation to finish playing. The script call spreadsheet shows another set of calls to use, but they don't work in battle.
For example, I have a state very similar to the one I was talking about in my other post, if you have the state active, when you hit an opponent with a physical attack, it plays an animation and does extra damage.
<Custom Conclude Effect>
if (target.result().isHit() && this.isHpEffect() && this.isPhysical()) {
var input = Math.floor(target.lastHitDmg / 4)
target.startAnimation(171, false, 0);
var total = RamzaDoTMath(input, 8, 20, target)
target.gainHp(-total);
target.startDamagePopup();
if (target.isDead()){
target.performCollapse();
}
}
</Custom Conclude Effect>
While it works with no errors, the damage popup for the state happens at the same frame as the damage popup for the attack that caused the state, while the animation plays for several frames after the popup is gone.
Any ideas?
Yes, use setTimeout with a callback function. Example:
<Custom Conclude Effect>
var animId = 80;
// you can use this to add a delay so it doesn't so immediately after the animation. 60 frames = 1 second
var extraFramesToWaitFor = 15;
var animFrames = ($dataAnimations[animId].frames.length * 4) + 1 + extraFramesToWaitFor;
var waitSeconds = (animFrames/60) * 1000;
target.startAnimation(animId, false, 0);
setTimeout(function () {
target.gainHp(-100);
target.startDamagePopup();
if (target.isDead()) {
target.performCollapse();
}
}, waitSeconds);
// have battle wait for anim completion
BattleManager._logWindow._waitCount += animFrames;
</Custom Conclude Effect>
We're using setTimeout here. The first parameter is the callback function, the second is the time in milliseconds to wait for.
We calculate the frames by multiplying the number of frames in the animation by the default animation rate, which is 4, then add 1 and then some extra frames so it's a little more natural to the eye.
We calculate the seconds by converting to seconds (1 second = 60 frames) and convert to millseconds.
Anything within the callback function will wait for waitSeconds time to execute.
We also add a wait to the battle log so that the battle won't keep going on until the animation is done.