Plugin to play SE automatically after variable change.

Sarry123

Villager
Member
Joined
Jun 5, 2020
Messages
10
Reaction score
3
First Language
French
Primarily Uses
RMMV
Hi, so, I am not really good at coding.
I am actually in the middle of creating a project and am making a relationship system.

That's why I decided to make a plugin that plays a sound effect upon variable change. I tried, but nothing works in the game.

I may be able to put a sound effect every time a variable changes in the editor.
But, I was wondering if there was an easier way to do so.

Is what I am asking for possible? Or should I stick with the editor for that purpose?

Here is my test plugin :
JavaScript:
/*:
* @plugindesc A plugin letting you put sound effects for variable changes.
* @author Sarry
*/

var check_change;

if (check_change != $gameVariables.value(1)) {
        $gameSystem.saveBgm();
    check_change = $gameVariables.value(1);
   
    }
(I put $gameSystem.saveBgm(); for testing.)

EDIT : So, I was able to do this using a parallel common event and it's working perfectly. But, I'm still interested in learning how to do this via javascript scripts/plugins.

Thanks.
 
Last edited:

Eliaquim

Hakuen Studio
Veteran
Joined
May 22, 2018
Messages
1,419
Reaction score
673
First Language
Portuguese - Br
Primarily Uses
RMMV
Do you want the sound effect play when changing a specific variable Id? Or any variable?
 

Aerosys

Veteran
Veteran
Joined
Apr 23, 2019
Messages
289
Reaction score
262
First Language
german
Primarily Uses
RMMV
What I recommend you to learn first is aliasing methods. This technique is similar to overriding, but instead of overriding, you extend a function. So we extend Game_Variables.set(id, value) in a way, that it plays a sound.

Code:
var alias = Game_Variables.prototype.setValue;

Game_Variables.prototype.setValue = function(id, value) {
    if (id == 1 // <-- your desired id to listen on
        && value != $gameVariables.value(1)) // <-- use this line if you want to play the SE only if the new value is different
    {
        // play sound
    }
    
    // call original function
    alias.call(this, id, value);
}
I don't know all commands by heart, so it may contain spelling errors.

Use this link to figure out how to play a SE ;)
 

Sarry123

Villager
Member
Joined
Jun 5, 2020
Messages
10
Reaction score
3
First Language
French
Primarily Uses
RMMV
Thank you for your fast answer. It was actually pretty easy. I'll see what I can do with this.
Thanks again.
 

Sarry123

Villager
Member
Joined
Jun 5, 2020
Messages
10
Reaction score
3
First Language
French
Primarily Uses
RMMV
@Eliaquim Hi and yes, I wanted to make it for at least 5 variables and some with more conditions like if the variable's value decreased, a specific se plays, if it increased, another would play, etc...

I managed to make this with a common event for two variables.
 

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
1,714
Reaction score
1,165
First Language
EN
Primarily Uses
RMMV
Don't worry too much about it, but in this case I think I'd go a little further. Ever been playing a game and suddenly a sound plays WAY louder than it should have? :kaoback: One way to help prevent that is to have the sound check happen only once per frame and use some kind of timer/counter to limit how frequently the sound can play, e.g. (untested):
JavaScript:
/*:
* @plugindesc Hi! I'm a plugin!
*/

(function() {   // to keep seTime, TIME, and SE private to this plugin
'use strict';

  let seTime = 0;
  const TIME = 30;   // play up to once per this many frames
  const SE = { name: "Coin", volume: 20, pitch: 100, pan: 0 };

  // After normal update stuff, check if seTime is equal to TIME and play sound if so
  (function(alias) {
    Game_Map.prototype.update = function() {
      alias.apply(this, arguments);
      if (seTime > 0) {
        if (seTime === TIME) AudioManager.playSe(SE);
        --seTime;   // count down by 1
      }
    };
  })(Game_Map.prototype.update);

  // If seTime is zero when a variable's value is changed, set it equal to TIME
  (function(alias) {
    Game_Variables.prototype.setValue = function(variableId, value) {
      let preVal = this.value(variableId);
      alias.apply(this, arguments);
      if (!seTime && preVal !== this.value(variableId)) seTime = TIME;
    };
  })(Game_Variables.prototype.setValue);

})();

Similar thread, for posterity~
 

Sarry123

Villager
Member
Joined
Jun 5, 2020
Messages
10
Reaction score
3
First Language
French
Primarily Uses
RMMV
Thanks for your help.
I'll see what I can do.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

And back. Trying to work online with no internet for 1.5 days was...interesting. I felt like a character in a old Sierra game trying to find internet due to all the hoops I had to jump through just to get online for work.
grief... if MV didn't have certain plugins already, I think I'd upgrade to MZ. seeing like 10 MV plugins in 1 MZ one is hilariously convenient lol.
Have you already checkes out my mapping tutorial on the blog? I would love to hear your feedback and things you'd like to see in the future!

one of character for my upcoming game idk how to design o_O

Designing patches that can 'augmented' to clothing based armor. Such good fun. A bit time consuming due to the need to create image displays, but the patches are limited so it's not too tiring.
Note: lol, posting this made me find a couple mistakes.

Forum statistics

Threads
100,737
Messages
978,947
Members
132,359
Latest member
PhilKeepItReal
Top