MZ How to allow SE variance in animations?

As politely recommended by another user in a different thread, I'd like to know how to enable variance SE playback in combat animations.,

I didn't think it was possible, not even via plugin, but excellent @ATT_Turan , always on the spot, mentioned that this is not the case.

I have no clue how to make it work, so help is appreciated. I could (and did) make so for map events via my own plugin but on combat animations I have no idea, even how to make it work.
The last person who tried to explain it to you got a snide comment in return.
The last person who tried to explain it to you got a snide comment in return.
The last person who made a snide and rude remark at me first you mean? Because it was an entirely unnecessary comment and last I checked backseat moderation isn't allowed by forum rules.

I accept i may be easily upsettable today because someone attacked my mothers car, thankfully she wasn't in when it happened but even so.
I have no clue how to make it work,
redirect the call to the function.

when you call "play SE", you must give it a file to play, at pitch, at volume.
make an alias function that intercepts that, and instead of "one specific file", it chooses "one of a selection" (file, pitch, volume, etc)
redirect the call to the function.

when you call "play SE", you must give it a file to play, at pitch, at volume.
make an alias function that intercepts that, and instead of "one specific file", it chooses "one of a selection" (file, pitch, volume, etc)

So like, a plugin entry for every animation? Could I get an example please
I was confused by that reply, but now i see that theres no need to assume its meaning anymore...

The last person who made a snide and rude remark at me first you mean?
What part of what I said was rude? you asked a question, I answered it with *two* possible solutions, and then...
Because it was an entirely unnecessary comment and last I checked backseat moderation isn't allowed by forum rules.
suggested that you make a thread for the request because appending it to the end of someone else's unrelated thread will only limit your question's visibility and reduce the number of responses youd get from people who could provide an answer.

If youre just going to assume that my trying to be helpful to you is being rude, you wont have to worry about it after this.
What part of what I said was rude? you asked a question, I answered it with *two* possible solutions, and then...
And then you basically told me to go make my own thread after I was "derailing" the thread is solved in message two. So yes, you sounded rude.

suggested that you make a thread for the request because appending it to the end of someone else's unrelated thread will only limit your question's visibility and reduce the number of responses youd get from people who could provide an answer.
If this is what you meant, then I'm sorry, but you were a bit brief about it and came across differently than what you intended. Is this what you meant?
And then you basically told me to go make my own thread after I was "derailing" the thread is solved in message two. So yes, you sounded rude.
That's not rude, it's the forum rules.

1730294888927.png

1730294899554.png

I see. It happens all the time and well without me but sure, from now on a thread per question and vice versa.

Thank you both.
You should be able to make the changes to the AudioManager.playSe(timing.se) and save it as a new plugin (from the Sprite_Animation function in rmmz_sprites). Something like this might be a good start (hopefully this will do +/- 10% of the current pitch value stored in the database):

JavaScript:
Sprite_Animation.prototype.processSoundTimings = function() {
    for (const timing of this._animation.soundTimings) {
        if (timing.frame === this._frameIndex) {
            timing.se.pitch += (Math.random() < .5 ? 1 : -1) * Math.randomInt(11);
            AudioManager.playSe(timing.se);
        }
    }
};
I don't have MZ so I can't test this - I am mostly guessing based on the function - I am also assuming timing.se.pitch will allow you to edit the pitch value when it obtains the values from the database.
Thank you! I am testing something because effseeker handles animations a bit differently (well, a lot) than MZ but who knows.
Thank you! I am testing something because effseeker handles animations a bit differently (well, a lot) than MZ but who knows.
I see - I forget about Effekseer. Hmm. Not too sure, unfortunately. Hope it all works out somehow!
Well, one step at a time.
I'd try something like (untested):
Spoiler: plugin code
JavaScript:
/*:
 * @target MZ
 * @plugindesc Random variance for animation SEs.
 * @author Caethyril
 * @url https://forums.rpgmakerweb.com/threads/172916/
 * @help Free to use and/or modify for any project, no credit required.
 */
;void (() => {
"use strict";
  /** Toggled mid-game to apply variance when appropriate. */
  let doVariance = false;

  /** If `true`, `V` values are multipliers; else they are additive. */
  const VARY_MULT = true;

  /** Per-value variances. */
  const V = Object.freeze({
    volume: 0.05,  // +- 5%
    pitch:  0.10,
    pan:    0      // no variance
  });

  /**
   * @param {number} n Original value.
   * @param {number} v Variance.
   * @returns {number} `n` after applying binomial variance `v`.
   */
  const vary = VARY_MULT ? function(n, v) {
    return n * (1 + v * (Math.random() - Math.random()));
  } : function(n, v) {
    return n + v * (Math.random() - Math.random());
  };

  /**
   * @param {object} o SE object.
   * @returns {object} shallow-cloned input with varied values.
   */
  const varySe = function(o) {
    const r = Object.assign({}, o);  // avoid mutating original data
    for (const k in r)
      if (k in V)
        r[k] = vary(r[k], V[k]);
    return r;
  };

  // Patch - apply variance when appropriate.
  void (alias => {
    AudioManager.playSe = function(se, ...args) {
      if (doVariance)
        alias.call(this, varySe(se), ...args);
      else
        alias.apply(this, arguments);
    };
  })(AudioManager.playSe);

  // Patch - toggle `doVariance` flag for `playSe` patch.
  for (const [c, m] of [
    [Sprite_Animation, "processSoundTimings"],
    [Sprite_AnimationMV, "processTimingData"]
  ])
    void (alias => {
      c.prototype[m] = function() {
        doVariance = true;
        alias.apply(this, arguments);
        doVariance = false;
      };
    })(c.prototype[m]);

})();
The V values affect how much each value can be varied, you'll probably want to edit those. If you need some SEs to be exempt from the variance effect, it would be possible to add a check to varySe for, e.g., a certain pattern in the filename like "starts with !".
I'd try something like (untested):
Spoiler: plugin code
JavaScript:
/*:
 * @target MZ
 * @plugindesc Random variance for animation SEs.
 * @author Caethyril
 * @url https://forums.rpgmakerweb.com/threads/172916/
 * @help Free to use and/or modify for any project, no credit required.
 */
;void (() => {
"use strict";
  /** Toggled mid-game to apply variance when appropriate. */
  let doVariance = false;

  /** If `true`, `V` values are multipliers; else they are additive. */
  const VARY_MULT = true;

  /** Per-value variances. */
  const V = Object.freeze({
    volume: 0.05,  // +- 5%
    pitch:  0.10,
    pan:    0      // no variance
  });

  /**
   * @param {number} n Original value.
   * @param {number} v Variance.
   * @returns {number} `n` after applying binomial variance `v`.
   */
  const vary = VARY_MULT ? function(n, v) {
    return n * (1 + v * (Math.random() - Math.random()));
  } : function(n, v) {
    return n + v * (Math.random() - Math.random());
  };

  /**
   * @param {object} o SE object.
   * @returns {object} shallow-cloned input with varied values.
   */
  const varySe = function(o) {
    const r = Object.assign({}, o);  // avoid mutating original data
    for (const k in r)
      if (k in V)
        r[k] = vary(r[k], V[k]);
    return r;
  };

  // Patch - apply variance when appropriate.
  void (alias => {
    AudioManager.playSe = function(se, ...args) {
      if (doVariance)
        alias.call(this, varySe(se), ...args);
      else
        alias.apply(this, arguments);
    };
  })(AudioManager.playSe);

  // Patch - toggle `doVariance` flag for `playSe` patch.
  for (const [c, m] of [
    [Sprite_Animation, "processSoundTimings"],
    [Sprite_AnimationMV, "processTimingData"]
  ])
    void (alias => {
      c.prototype[m] = function() {
        doVariance = true;
        alias.apply(this, arguments);
        doVariance = false;
      };
    })(c.prototype[m]);

})();
The V values affect how much each value can be varied, you'll probably want to edit those. If you need some SEs to be exempt from the variance effect, it would be possible to add a check to varySe for, e.g., a certain pattern in the filename like "starts with !".

Thank you, that is, completely different than my own untested one, I'll test both and report findings here.

*helpful code, thanks*
OK, sorry for delay, I kinda got sidetracked by Veilguard!

So, unsurprisingly since you wrote it, your plugin works. It works flawlessly with vanilla MZ - since usually one animation plays per action, the pitch/volume are changed. With VS, however, I think only the SE of the first animation is changed (so basically the casting animation if it is on). I tried an interim change but, well, either my ears can't detect the changes or I made nothing, hopefully I did not make it worse
😅
Spoiler: spaghetti
JavaScript:
;void (() => {
"use strict";

  const VARY_MULT = true;
  const V = Object.freeze({
    volume: 0.05,
    pitch:  0.10,
    pan:    0
  });

  // Track animation for variance
  let varianceQueue = 0;

  const vary = VARY_MULT ? function(n, v) {
    return n * (1 + v * (Math.random() - Math.random()));
  } : function(n, v) {
    return n + v * (Math.random() - Math.random());
  };

  const varySe = function(o) {
    const r = Object.assign({}, o);
    for (const k in r)
      if (k in V)
        r[k] = vary(r[k], V[k]);
    return r;
  };

  void (alias => {
    AudioManager.playSe = function(se, ...args) {
      if (varianceQueue > 0) {
        alias.call(this, varySe(se), ...args);
      } else {
        alias.apply(this, arguments);
      }
    };
  })(AudioManager.playSe);

  for (const [c, m] of [
    [Sprite_Animation, "processSoundTimings"],
    [Sprite_AnimationMV, "processTimingData"]
  ]) void (alias => {
      c.prototype[m] = function() {
        varianceQueue++;  // increment
        alias.apply(this, arguments);
        varianceQueue--;  // decrement
      };
  })(c.prototype[m]);

})();

this is actually a fair bit trickier than I imagined, I wonder how significantly more difficult it will be to allow for random SE playback at frame.
Good to hear it works with the core scripts, at least! 😂

I guess VisuStella reorganises anim stuff. Is it only the casting animation that's affected, or the first animation per action? Odd that it works out like that, since in the core scripts these timing methods are called once per frame, per animation (update -> updateMain)...all in series, no complicated async or anything.

It may be worth testing with different casting animations, just to make sure it's not related to e.g. the frame when the SE is played. 👋

For testing, you could add a message to e.g. varySe:
JavaScript:
  const varySe = function(o) {
    const r = Object.assign({}, o);  // avoid mutating original data
    console.log("Varying SE params for " + r.name + ".");  // debug
This will log a message to the Console tab of the dev tools window (F8 or F12 during playtest) whenever the function hits that line.

If you want to randomise the SE that is played, that should be pretty simple: just change r.name in the varySe function before returning it. The harder part (as usual) would be design: how would the game know which name to use? Perhaps a lookup table, e.g.
Spoiler: name replacement idea
JavaScript:
/** random name replacements for specific SEs */
const NAMES = {
  Hit1: ["Hit1", "Hit2", "Hit3"],
  Heal1: ["Heal1", "Heal2"]
  // etc
};

const varySe = function(o) {
  const r = Object.assign({}, o);
  const a = NAMES[r.name];
  if (a)
    r.name = a[Math.randomInt(a.length)];
  // then the rest as usual
(Randomness in the timing would be more challenging: you'd probably want to change the plugin structure for that.)

My code probably makes this look more awkward than it actually is, since I've taken a roundabout route to avoid a direct override. A simpler, direct override like @bass2yang's might be a better option if you want to try to ensure this part plays out as intended...though if it does work, it might break related features from other plugin(s). The other method is Sprite_AnimationMV.prototype.processTimingData, found in rmmz_sprites.js.
Good to hear it works with the core scripts, at least! 😂

I guess VisuStella reorganises anim stuff. Is it only the casting animation that's affected, or the first animation per action? Odd that it works out like that, since in the core scripts these timing methods are called once per frame, per animation (update -> updateMain)...all in series, no complicated async or anything.

It may be worth testing with different casting animations, just to make sure it's not related to e.g. the frame when the SE is played. 👋

For testing, you could add a message to e.g. varySe:
JavaScript:
  const varySe = function(o) {
    const r = Object.assign({}, o);  // avoid mutating original data
    console.log("Varying SE params for " + r.name + ".");  // debug
This will log a message to the Console tab of the dev tools window (F8 or F12 during playtest) whenever the function hits that line.

If you want to randomise the SE that is played, that should be pretty simple: just change r.name in the varySe function before returning it. The harder part (as usual) would be design: how would the game know which name to use? Perhaps a lookup table, e.g.
Spoiler: name replacement idea
JavaScript:
/** random name replacements for specific SEs */
const NAMES = {
  Hit1: ["Hit1", "Hit2", "Hit3"],
  Heal1: ["Heal1", "Heal2"]
  // etc
};

const varySe = function(o) {
  const r = Object.assign({}, o);
  const a = NAMES[r.name];
  if (a)
    r.name = a[Math.randomInt(a.length)];
  // then the rest as usual
(Randomness in the timing would be more challenging: you'd probably want to change the plugin structure for that.)

My code probably makes this look more awkward than it actually is, since I've taken a roundabout route to avoid a direct override. A simpler, direct override like @bass2yang's might be a better option if you want to try to ensure this part plays out as intended...though if it does work, it might break related features from other plugin(s). The other method is Sprite_AnimationMV.prototype.processTimingData, found in rmmz_sprites.js.
Thank you 🙂 I'll try this later back from gym.

Ok so, I'm back to testing this but I think i am a bit lost - also a bit distracted by veilguard I admit, not entirely focused. I probably need another draft and test. Console logs always helpful.

(Randomness in the timing would be more challenging: you'd probably want to change the plugin structure for that.)

What do you mean, in timing?
Not given up on this, just got sidetracked by a number of things AND it is more complicated than it looks
What do you mean, in timing?
Just realised I didn't respond to this. I meant e.g. an SE set to play on frame 10 in the editor would have a chance to play slightly earlier or later instead, like frame 8~12. (Probably not what you wanted, hence parentheses.)
indeed, it sounds like the sort of thing that could instead be more easily rectified at effseeker level - if any.

Original thread: https://forums.rpgmakerweb.com/threads/how-to-allow-se-variance-in-animations.172916/